Exemplo n.º 1
0
        public ImmutableDictionary <TypePair, CodeFile> CreateCodeFilesDictionary(
            ImmutableDictionary <TypePair, CodeFile> parentFiles)
        {
            var files = new Dictionary <TypePair, CodeFile>();
            var cv    = NameConventionsStorage.Map;

            foreach (var kvp in ExplicitTypeMaps)
            {
                TypePair typePair = kvp.Key;

                var mapCodeFile = parentFiles[typePair];

                string srcType  = typePair.SourceType.FullName.NormalizeTypeName();
                string destType = typePair.DestinationType.FullName.NormalizeTypeName();

                string arg1 = $"{cv.SrcParam} as {srcType}";
                string arg2 = StatementTemplates.New(destType);

                var methodCall = StatementTemplates.MethodCall(mapCodeFile.GetClassAndMethodName(), arg1, arg2);

                string methodCode = StatementTemplates.Method(string.Empty,
                                                              new MethodDeclarationContext(cv.Method,
                                                                                           new VariableContext(destType, methodCall),
                                                                                           new VariableContext(typeof(object).Name, cv.SrcParam)));

                var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method);

                files.Add(typePair, file);
            }

            return(files.ToImmutableDictionary());
        }
        public ImmutableDictionary <TypePair, CodeFile> CreateCodeFilesDictionary(
            ImmutableDictionary <TypePair, CodeFile> parentFiles)
        {
            var files = new Dictionary <TypePair, CodeFile>();
            var cv    = NameConventionsStorage.MapCollection;

            foreach (var kvp in ExplicitTypeMaps)
            {
                TypePair typePair = kvp.Key;

                var mapCodeFile = parentFiles[typePair];

                string srcCollType  = cv.GetCollectionType(typePair.SourceType.FullName);
                string destCollType = cv.GetCollectionType(typePair.DestinationType.FullName);

                //string filler = CreationTemplates.NewObject(typePair.DestinationType);
                var builder = new StringBuilder();
                builder.AppendLine($"var {cv.SrcParam} = {cv.SrcCollection} as {srcCollType};");
                builder.AppendLine($"var {cv.DestParam} = {cv.DestCollection} as {destCollType};");
                //builder.AppendLine($"{cv.DestParam}.Add({cv.SrcParam}.Count, () => {filler});");
                builder.AppendLine(CreationTemplates.Add(cv.DestParam, $"{cv.SrcParam}.Count", typePair.DestinationType));

                string methodCall = StatementTemplates.MethodCall(
                    mapCodeFile.GetClassAndMethodName(), cv.SrcParam, cv.DestParam);

                builder.AppendLine(methodCall);

                string methodCode = StatementTemplates.Method(builder.ToString(),
                                                              new MethodDeclarationContext(cv.Method,
                                                                                           null,
                                                                                           new VariableContext(typeof(object).Name, cv.SrcCollection),
                                                                                           new VariableContext(typeof(object).Name, cv.DestCollection))
                                                              );

                var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method);

                files.Add(typePair, file);
            }

            return(files.ToImmutableDictionary());
        }