Ejemplo n.º 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 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 + "]");
                }
            }
        }