diff --git a/src/AutoMapper.Collection.Tests/InheritanceWithCollectionTests.cs b/src/AutoMapper.Collection.Tests/InheritanceWithCollectionTests.cs index b411a60..d85ad25 100644 --- a/src/AutoMapper.Collection.Tests/InheritanceWithCollectionTests.cs +++ b/src/AutoMapper.Collection.Tests/InheritanceWithCollectionTests.cs @@ -9,52 +9,12 @@ namespace AutoMapper.Collection { - public class InheritanceWithCollectionTests + public abstract class InheritanceWithCollectionTests { + protected abstract void Create(IMapperConfigurationExpression cfg); + private IMapper CreateMapper() { - void Create(IMapperConfigurationExpression cfg) - { - cfg.ShouldMapProperty = propertyInfo => propertyInfo.GetMethod.IsPublic || propertyInfo.GetMethod.IsAssembly || propertyInfo.GetMethod.IsFamily || propertyInfo.GetMethod.IsPrivate; - cfg.AddCollectionMappers(); - - //DOMAIN --> EF - cfg.CreateMap() - .ForMember(rootEf => rootEf.Orders, opt => opt.ResolveUsing()) - ; - - //collection type - cfg.CreateMap() - .EqualityComparison((oo, dto) => BaseEquals(oo, dto)) - ; - - cfg.CreateMap() - .EqualityComparison((ood, ooe) => DerivedEquals(ood, ooe)) - .IncludeBase() - ; - - cfg.CreateMap() - .IncludeBase() - ; - - //EF --> DOMAIN - cfg.CreateMap() - .ForMember(rootDomain => rootDomain.OnlineOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(OnlineOrderEf)))) - .ForMember(rootDomain => rootDomain.MailOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(MailOrderEf)))) - ; - - cfg.CreateMap() - ; - - cfg.CreateMap() - .IncludeBase() - ; - - cfg.CreateMap() - .IncludeBase() - ; - } - var map = new MapperConfiguration(Create); map.AssertConfigurationIsValid(); map.CompileMappings(); @@ -62,6 +22,31 @@ void Create(IMapperConfigurationExpression cfg) return map.CreateMapper(); } + [Fact] + public void TypeMap_Should_include_base_types() + { + var mapper = CreateMapper(); + var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(typeof(MailOrderDomain), typeof(OrderEf)); + + var typePairs = new[]{ + new TypePair(typeof(OrderDomain), typeof(OrderEf)) + }; + typeMap.IncludedBaseTypes.ShouldBeEquivalentTo(typePairs); + } + + [Fact] + public void TypeMap_Should_include_derivied_types() + { + var mapper = CreateMapper(); + var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(typeof(OrderDomain), typeof(OrderEf)); + + var typePairs = new[]{ + new TypePair(typeof(OnlineOrderDomain), typeof(OnlineOrderEf)), + new TypePair(typeof(MailOrderDomain), typeof(MailOrderEf)) + }; + typeMap.IncludedDerivedTypes.ShouldBeEquivalentTo(typePairs); + } + [Fact] public void Map_Should_ReturnOnlineOrderEf_When_ListIsOfTypeOrderEf() { @@ -205,5 +190,95 @@ public List Resolve(RootDomain source, RootEf destination, List propertyInfo.GetMethod.IsPublic || propertyInfo.GetMethod.IsAssembly || propertyInfo.GetMethod.IsFamily || propertyInfo.GetMethod.IsPrivate; + cfg.AddCollectionMappers(); + + //DOMAIN --> EF + cfg.CreateMap() + .ForMember(rootEf => rootEf.Orders, opt => opt.ResolveUsing()) + ; + + //collection type + cfg.CreateMap() + .EqualityComparison((oo, dto) => BaseEquals(oo, dto)) + .Include() + .Include() + ; + + cfg.CreateMap() + .EqualityComparison((ood, ooe) => DerivedEquals(ood, ooe)) + ; + + cfg.CreateMap() + ; + + //EF --> DOMAIN + cfg.CreateMap() + .ForMember(rootDomain => rootDomain.OnlineOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(OnlineOrderEf)))) + .ForMember(rootDomain => rootDomain.MailOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(MailOrderEf)))) + ; + + cfg.CreateMap() + .Include() + .Include() + ; + + cfg.CreateMap() + ; + + cfg.CreateMap() + ; + } + } + + public class IncludeBase : InheritanceWithCollectionTests + { + protected override void Create(IMapperConfigurationExpression cfg) + { + cfg.ShouldMapProperty = propertyInfo => propertyInfo.GetMethod.IsPublic || propertyInfo.GetMethod.IsAssembly || propertyInfo.GetMethod.IsFamily || propertyInfo.GetMethod.IsPrivate; + cfg.AddCollectionMappers(); + + //DOMAIN --> EF + cfg.CreateMap() + .ForMember(rootEf => rootEf.Orders, opt => opt.ResolveUsing()) + ; + + //collection type + cfg.CreateMap() + .EqualityComparison((oo, dto) => BaseEquals(oo, dto)) + ; + + cfg.CreateMap() + .EqualityComparison((ood, ooe) => DerivedEquals(ood, ooe)) + .IncludeBase() + ; + + cfg.CreateMap() + .IncludeBase() + ; + + //EF --> DOMAIN + cfg.CreateMap() + .ForMember(rootDomain => rootDomain.OnlineOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(OnlineOrderEf)))) + .ForMember(rootDomain => rootDomain.MailOrders, opt => opt.MapFrom(rootEf => rootEf.Orders.Where(orderEf => orderEf.GetType() == typeof(MailOrderEf)))) + ; + + cfg.CreateMap() + ; + + cfg.CreateMap() + .IncludeBase() + ; + + cfg.CreateMap() + .IncludeBase() + ; + } + } } }