Ejemplo n.º 1
0
 /// <summary>
 /// Initialize a new instance of <see cref="MapperBuilder{TSource, TDestiny}"/>
 /// </summary>
 /// <param name="parent">The parent of this mapper</param>
 /// <param name="service">The <see cref="IServiceCollection"/> to register type. </param>
 /// <param name="hashCodeFactoryGenerator">The <see cref="HashCodeFactoryGenerator"/> is used to create GenHashCode.</param>
 public MapperBuilder(IMapperBuilder parent, IServiceCollection service, HashCodeFactoryGenerator hashCodeFactoryGenerator)
 {
     _parent = parent ?? throw new ArgumentNullException(nameof(parent));
     _service = service ?? throw new ArgumentNullException(nameof(service));
     _hashCodeFactoryGenerator = hashCodeFactoryGenerator;
     _generator = new MapperGenerator(typeof(TSource), typeof(TDestiny), _rules, hashCodeFactoryGenerator);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize a new instance of <see cref="DifferentTypeRule"/>.
        /// </summary>
        /// <param name="source">The source member.</param>
        /// <param name="destiny">The source member.</param>
        /// <param name="hashCodeFactory">The <see cref="HashCodeFactoryGenerator"/>.</param>
        public DifferentTypeRule(MemberInfo source, MemberInfo destiny, HashCodeFactoryGenerator hashCodeFactory)
        {
            _hashCodeFactory = hashCodeFactory;
            SourceProperty   = source as PropertyInfo ?? throw new ArgumentNullException(nameof(source));
            DestinyProperty  = destiny as PropertyInfo ?? throw new ArgumentNullException(nameof(destiny));

            _mapName = $"_map{SourceProperty.Name}To{DestinyProperty.Name}";
            var fields     = new LinkedList <(string, Type)>();
            var sourceType = SourceProperty !.PropertyType;

            if (sourceType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>)))
            {
                sourceType = typeof(IEnumerable <>).MakeGenericType(sourceType.GetGenericArguments()[0]);
            }

            fields.AddLast((_mapName, typeof(IMap <,>).MakeGenericType(sourceType, DestinyProperty.PropertyType)));
            fields.AddLast(("_counter", typeof(Counter)));
            Fields = fields;
        }
Ejemplo n.º 3
0
        public static IRuleMapper CreateRule(PropertyInfo destiny, PropertyInfo source, HashCodeFactoryGenerator hashCodeFactory)
        {
            if (source.PropertyType == destiny.PropertyType)
            {
                return(new SameTypeRule(source, destiny));
            }

            if (source.PropertyType.IsNetType() && destiny.PropertyType.IsNetType())
            {
                return(new DifferentNetTypeRule(source, destiny));
            }

            if ((source.PropertyType.IsNullable() && !destiny.PropertyType.IsNullable() && destiny.PropertyType.IsNetType()) ||
                (!source.PropertyType.IsNullable() && destiny.PropertyType.IsNullable() && source.PropertyType.IsNetType()))
            {
                var sourceType  = source.PropertyType.GetUnderlyingType();
                var destinyType = destiny.PropertyType.GetUnderlyingType();
                if (sourceType == destinyType)
                {
                    return(new NullableRuleForSameTypeWhenOneIsNullable(source, destiny));
                }

                return(new NullableRuleForDifferentTypeWhenOneIsNullable(source, destiny));
            }

            if (source.PropertyType.IsNullable() && destiny.PropertyType.IsNullable() &&
                source.PropertyType.GetUnderlyingType().IsNetType() && destiny.PropertyType.GetUnderlyingType().IsNetType())
            {
                return(new NullableRuleForDifferentType(source, destiny));
            }

            return(new DifferentTypeRule(source, destiny, hashCodeFactory));
        }