public string CreateUniqueMapperMethodNameWithGuid(TypePair typePair)
        {
            string guid = NamingTools.NewGuid(MaxGuidLength);

            string normSrcName  = NamingTools.ToAlphanumericOnly(typePair.SourceType.Name);
            string normDestName = NamingTools.ToAlphanumericOnly(typePair.DestinationType.Name);

            return($"Mapper_{normSrcName}_{normDestName}_{guid}");
        }
Ejemplo n.º 2
0
        public static string For(string innerCode, ForDeclarationContext ctx)
        {
            var builder = new StringBuilder();

            string index = "index_" + NamingTools.NewGuid(4);

            builder.AppendLine($"           for (int {index} = 0; {index} < {ctx.SrcCollection}.Count; {index}++)");
            builder.AppendLine("            {");
            builder.AppendLine($"                var {ctx.SrcVariable} = {ctx.SrcCollection}.ElementAt({index});");
            builder.AppendLine($"                var {ctx.DestVariable} = {ctx.DestCollection}.ElementAt({index});");
            builder.AppendLine(innerCode);

            builder.AppendLine("            }");

            string forCode = builder.ToString();

            return(forCode);
        }
Ejemplo n.º 3
0
        private string AssignCollections(IPropertyNameContext ctx)
        {
            var recorder = new Recorder();

            var itemSrcType  = ctx.SrcType.GenericTypeArguments[0];
            var itemDestType = ctx.DestType.GenericTypeArguments[0];

            using (var block = new Block(recorder, "if", "{1} == null"))
            {
                string newCollection = CreationTemplates.NewCollection(ctx.DestType, "{0}.Count");
                recorder.AppendLine($"{{1}} = {newCollection};");
            }
            using (var block = new Block(recorder, "else"))
            {
                recorder.AppendLine("{1}.Clear();");
            }

            //fill new (or cleared) collection with the new set of items
            recorder.AppendLine(CreationTemplates.Add("{1}", "{0}.Count", itemDestType));

            //inner cycle variables (on each iteration itemSrcName is mapped to itemDestName).
            string itemSrcName  = "src_" + NamingTools.NewGuid(4);
            string itemDestName = "dest_" + NamingTools.NewGuid(4);

            var typePair = new TypePair(itemSrcType, itemDestType);

            Assignment itemAssignment = new Assignment();

            string cachedTemplate;

            if (TemplateCache.TryGetValue(typePair, out cachedTemplate))
            {
                itemAssignment.RelativeTemplate = cachedTemplate;
            }
            else if (itemSrcType.IsCollectionType() && itemDestType.IsCollectionType())
            {
                var innerContext = PropertyNameContextFactory.CreateWithoutPropertyMap(
                    itemSrcType, itemDestType, itemSrcName, itemDestName);

                string innerTemplate = AssignCollections(innerContext);

                itemAssignment.RelativeTemplate = innerTemplate;
            }
            else
            {
                var nodeMap = GetTypeMap(typePair);

                itemAssignment = ProcessTypeMap(nodeMap);
            }

            string iterationCode = itemAssignment.GetCode(itemSrcName, itemDestName);

            string forCode = StatementTemplates.For(iterationCode,
                                                    new ForDeclarationContext("{0}", "{1}", itemSrcName, itemDestName));

            recorder.AppendLine(forCode);

            string template = recorder.ToAssignment().RelativeTemplate;

            return(template);
        }