コード例 #1
0
        private string AssignReferenceTypes(PropertyNameContext ctx)
        {
            Recorder recorder = new Recorder();

            //has parameterless ctor
            if (ctx.DestType.HasParameterlessCtor())
            {
                recorder.AppendLine($"{{1}} = {StatementTemplates.New(ctx.DestTypeFullName)};");
            }
            else
            {
                throw new HappyMapperException(ErrorMessages.NoParameterlessCtor(ctx.DestType));
            }

            string template;

            //typepair isn't in template cache
            if (!TemplateCache.TryGetValue(ctx.TypePair, out template))
            {
                var nodeMap = GetTypeMap(ctx.TypePair);

                var assignment = ProcessTypeMap(nodeMap);

                template = assignment.RelativeTemplate;
            }

            recorder.AppendLine(template);

            return(recorder.ToAssignment().RelativeTemplate);
        }
コード例 #2
0
        private void ProcessPropertyTypePair(Coder coder, PropertyNameContext context, PropertyMap propertyMap)
        {
            var typePair = propertyMap.GetTypePair();

            //typepair already in template cache
            string text;

            if (TemplateCache.TryGetValue(typePair, out text))
            {
                coder.ApplyTemplate(context, text);
                return;
            }

            TypeMap nodeMap;

            //typepair explicitly mapped by user
            ExplicitTypeMaps.TryGetValue(typePair, out nodeMap);

            if (nodeMap == null)
            {
                if (!ImplicitTypeMaps.TryGetValue(typePair, out nodeMap))
                {
                    //create implicit map
                    nodeMap = TypeMapFactory.CreateTypeMap(propertyMap.SrcType, propertyMap.DestType, Options);
                    ImplicitTypeMaps.AddIfNotExist(nodeMap);
                }
            }

            var propAssignment = ProcessTypeMap(nodeMap, context.SrcFullMemberName, context.DestFullMemberName);

            coder.AttachPropertyAssignment(propAssignment, propertyMap);
            return;
        }
コード例 #3
0
            /// <summary>
            /// Retrieve a Template record with the name "Title" and related Entity
            /// </summary>
            /// <param name="service">The organization service</param>
            /// <param name="tracingService">The tracing service</param>
            /// <param name="templateId">The id of the template to retrieve</param>
            /// <param name="ignoreCache">A value indicating whether to ignore cache</param>
            /// <returns>The template</returns>
            public static Template GetTemplate(IOrganizationService service, ITracingService tracingService, Guid templateId, bool ignoreCache = false)
            {
                lock (LockTemplateCache)
                {
                    GetTemplates(service, tracingService, ignoreCache);

                    if (TemplateCache.TryGetValue(templateId, out Template template))
                    {
                        return(template);
                    }
                    else
                    {
                        throw new KeyNotFoundException($"The template {templateId} could not be found.");
                    }
                }
            }
コード例 #4
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);
        }