Esempio n. 1
0
        public static Expression GetInlineMappingBlock(
            IObjectMappingData mappingData,
            MappingValues mappingValues,
            Expression createMappingDataCall)
        {
            var mapper = mappingData.GetOrCreateMapper();

            if (mapper == null)
            {
                if (mappingData.HasSameTypedConfiguredDataSource())
                {
                    // Configured data source for an otherwise-unconstructable complex type:
                    return(mappingValues.SourceValue);
                }

                return(Constants.EmptyExpression);
            }

            if (mapper.MapperData.Context.UsesMappingDataObject)
            {
                return(UseLocalValueVariable(
                           mapper.MapperData.MappingDataObject,
                           createMappingDataCall,
                           mapper.MappingExpression));
            }

            return(GetDirectAccessMapping(
                       mapper.MappingLambda.Body,
                       mapper.MapperData,
                       mappingValues,
                       createMappingDataCall));
        }
        private static bool MarkUnconstructableMemberAsReadOnly(IObjectMappingData mappingData)
        {
            if (mappingData.HasSameTypedConfiguredDataSource())
            {
                // Configured data source for an otherwise-unconstructable complex type:
                return(false);
            }

            // Don't set an non-readonly abstract member to readonly as we'll try to map
            // it from derived types:
            return(!mappingData.MapperData.TargetMember.Type.IsAbstract());
        }
Esempio n. 3
0
        public static Expression GetObjectResolution(
            Func <IObjectMappingData, IList <Expression>, Expression> constructionFactory,
            IObjectMappingData mappingData,
            IList <Expression> memberPopulations,
            bool assignCreatedObject = false,
            bool assignTargetObject  = false)
        {
            var mapperData = mappingData.MapperData;

            if (mapperData.TargetMember.IsReadOnly || mapperData.TargetIsDefinitelyPopulated())
            {
                return(mapperData.TargetObject);
            }

            var objectValue = constructionFactory.Invoke(mappingData, memberPopulations);

            if (objectValue == null)
            {
                if (!mappingData.HasSameTypedConfiguredDataSource())
                {
                    mapperData.TargetMember.IsReadOnly = true;
                }

                // Use the existing target object if it might have a value and
                // the mapper can't create an instance:
                return(mapperData.TargetCouldBePopulated()
                    ? mapperData.TargetObject
                    : mapperData.GetTargetMemberDefault());
            }

            if (UseNullFallbackValue(mapperData, objectValue, memberPopulations))
            {
                objectValue = mapperData.GetTargetMemberDefault();
                mapperData.Context.UsesMappingDataObjectAsParameter = false;
            }
            else
            {
                if (assignCreatedObject)
                {
                    mapperData.Context.UsesMappingDataObjectAsParameter = true;
                    objectValue = mapperData.CreatedObject.AssignTo(objectValue);
                }

                if (assignTargetObject || mapperData.Context.UsesMappingDataObjectAsParameter)
                {
                    objectValue = mapperData.TargetObject.AssignTo(objectValue);
                }
            }

            objectValue = AddExistingTargetCheckIfAppropriate(objectValue, mappingData);

            return(objectValue);
        }