Beispiel #1
0
        public TOut Map <TFrom, TOut>(ClassMappingConfiguration classConfig, TFrom fromGeneric) where TOut : new()
        {
            var fromType = ((TFrom)Activator.CreateInstance(typeof(TFrom))).GetType();
            var toObject = (TOut)Activator.CreateInstance(typeof(TOut));
            var toType   = toObject.GetType();

            //run pre processing checks
            _rules.ToList().ForEach(a => a.Run(classConfig, fromType, toType, fromGeneric));

            //at this point if TIn was null and the RejectNullReference rule was on, it would have thrown an exception
            //if it didn't, then null inputs are valid... return null
            if (fromGeneric == null)
            {
                return(default(TOut));
            }

            var fromProps = _getProps.Get(fromType);
            var toProps   = _getProps.Get(toType);

            foreach (var toProp in toProps)
            {
                var propMapStrategy = _propertyMappingStrategyFactory.Create(toProp);
                var fromProp        = propMapStrategy.Match(fromProps, toProp);

                if (fromProp == null)
                {
                    continue; //default behavior, props that don't line up are ignored. pre processing checks handle instances where props should line up but don't
                }
                var toPropConfig = GetPropertyMappingConfiguration(toProp);
                var strategy     = _copyStrategyFactory.GetStrategy(classConfig, toPropConfig);
                strategy.Copy(fromGeneric, toObject, toProp, fromProp, toPropConfig);
            }

            return(toObject);
        }
 public ICopyStrategy GetStrategy(ClassMappingConfiguration classConfig, PropertyMappingConfiguration propConfig)
 {
     if (classConfig.ConvertPrimitives)
     {
         if (propConfig.RejectNullReferences)
         {
             return(new ConvertCopyStrategy(new List <IPropertyLevelRule>()
             {
                 new RejectNullReferencesPropertyLevelRule()
             }));
         }
         else
         {
             return(new ConvertCopyStrategy(null));
         }
     }
     else
     {
         if (propConfig.RejectNullReferences)
         {
             return(new DefaultCopyStrategy(new List <IPropertyLevelRule>()
             {
                 new RejectNullReferencesPropertyLevelRule()
             }));
         }
         else
         {
             return(new DefaultCopyStrategy(null));
         }
     }
 }
        public void Run(ClassMappingConfiguration config, Type from, Type to, object inObject = null)
        {
            if (config.RejectNullReferences && (inObject is null))
            {
                throw new NullReferenceException(@"A null reference was passed into the mapper. If this is not desired behavior please remove 
the class level attribute [RejectNullReferences] from class: [" + to.Name + "]");
            }
        }
Beispiel #4
0
 private static IClassMapper CreateClassMapper <TSource>(TSource source, ClassMappingConfiguration config)
 {
     return(new ClassMapper(
                _classLevelRuleFactory.CreateRules(config),
                _getProps,
                new CopyStrategyFactory(),
                new PropertyMappingStrategyFactory()));
 }
        public ClassMappingConfiguration Create(Type t)
        {
            var config     = new ClassMappingConfiguration();
            var attributes = t.GetCustomAttributes(true).ToList();

            config.ConvertPrimitives    = attributes.Any(a => a is ImplicitConversionAttribute);
            config.RejectNullReferences = attributes.Any(a => a is RejectNullReferencesAttribute);
            config.RequireAllProperties = attributes.Any(a => a is RequireAllPropertiesAttribute);

            return(config);
        }
        public void Run(ClassMappingConfiguration config, Type from, Type to, object inObject = null)
        {
            if (config.RequireAllProperties)
            {
                var fromProps = _getProps.Get(from).Select(a => a.Name).ToList();
                var toProps   = _getProps.Get(to).Select(a => a.Name).ToList();

                if (toProps.Except(fromProps).Any())
                {
                    throw new MissingMemberException(@"There are missing properties in one of the mapping targets. If this is not desired behavior please remove 
the class level attribute entitled [RequireAllPropertiesAttribute] from class: [" + to.Name + "]");
                }
            }
        }
        public ClassMappingConfiguration Create(Type source, Type destination)
        {
            var config                = new ClassMappingConfiguration();
            var sourceAttributes      = source.GetCustomAttributes(true).ToList();
            var destinationAttributes = destination.GetCustomAttributes(true).ToList();
            var attributes            = new List <object>();

            attributes.AddRange(sourceAttributes);
            attributes.AddRange(destinationAttributes);

            config.ConvertPrimitives    = attributes.Any(a => a is ImplicitConversionAttribute);
            config.RejectNullReferences = destinationAttributes.Any(a => a is RejectNullReferencesAttribute);
            config.RequireAllProperties = destinationAttributes.Any(a => a is RequireAllPropertiesAttribute);

            return(config);
        }
        public IEnumerable <IClassLevelRule> CreateRules(ClassMappingConfiguration config)
        {
            var list = new List <IClassLevelRule>();

            if (config.RejectNullReferences)
            {
                list.Add(new RejectNullReferencesClassLevelRule());
            }

            if (config.RequireAllProperties)
            {
                list.Add(new RequireAllPropertiesRule(_getProps));
            }

            return(list);
        }