예제 #1
0
            public void CanMapToDomainInterface()
            {
                Mapper.CreateMap <DomainInterface, Dto>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Nested.Name))
                .ForMember(dest => dest.DecimalValue, opt => opt.MapFrom(src => src.Nested.DecimalValue));
                Mapper.CreateMap <Dto, DomainInterface>()
                .ForMember(dest => dest.Nested.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Nested.DecimalValue, opt => opt.MapFrom(src => src.DecimalValue));

                var domainInstance1 = new DomainImplA();
                var domainInstance2 = new DomainImplB();
                var domainInstance3 = new DomainImplA();

                var dtoCollection = new List <Dto>
                {
                    Mapper.Map <DomainInterface, Dto>(domainInstance1),
                    Mapper.Map <DomainInterface, Dto>(domainInstance2),
                    Mapper.Map <DomainInterface, Dto>(domainInstance3)
                };

                dtoCollection[0].Id           = Guid.NewGuid();
                dtoCollection[0].DecimalValue = 1M;
                dtoCollection[0].Name         = "Bob";
                dtoCollection[1].Id           = Guid.NewGuid();
                dtoCollection[1].DecimalValue = 0.1M;
                dtoCollection[1].Name         = "Frank";
                dtoCollection[2].Id           = Guid.NewGuid();
                dtoCollection[2].DecimalValue = 2.1M;
                dtoCollection[2].Name         = "Sam";

                Mapper.Map <Dto, DomainInterface>(dtoCollection[0], domainInstance1);
                Mapper.Map <Dto, DomainInterface>(dtoCollection[1], domainInstance2);
                Mapper.Map <Dto, DomainInterface>(dtoCollection[2], domainInstance3);

                dtoCollection[0].Id.ShouldBe(domainInstance1.Id);
                dtoCollection[1].Id.ShouldBe(domainInstance2.Id);
                dtoCollection[2].Id.ShouldBe(domainInstance3.Id);

                dtoCollection[0].DecimalValue.ShouldBe(domainInstance1.Nested.DecimalValue);
                dtoCollection[1].DecimalValue.ShouldBe(domainInstance2.Nested.DecimalValue);
                dtoCollection[2].DecimalValue.ShouldBe(domainInstance3.Nested.DecimalValue);

                dtoCollection[0].Name.ShouldBe(domainInstance1.Nested.Name);
                dtoCollection[1].Name.ShouldBe(domainInstance2.Nested.Name);
                dtoCollection[2].Name.ShouldBe(domainInstance3.Nested.Name);
            }
예제 #2
0
            public void CanMapToDomainInterface()
            {
                Mapper.CreateMap<DomainInterface, Dto>()
                    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Nested.Name))
                    .ForMember(dest => dest.DecimalValue, opt => opt.MapFrom(src => src.Nested.DecimalValue));
                Mapper.CreateMap<Dto, DomainInterface>()
                    .ForMember(dest => dest.Nested.Name, opt => opt.MapFrom(src => src.Name))
                    .ForMember(dest => dest.Nested.DecimalValue, opt => opt.MapFrom(src => src.DecimalValue));

                var domainInstance1 = new DomainImplA();
                var domainInstance2 = new DomainImplB();
                var domainInstance3 = new DomainImplA();

                var dtoCollection = new List<Dto>
                {
                    Mapper.Map<DomainInterface, Dto>(domainInstance1),
                    Mapper.Map<DomainInterface, Dto>(domainInstance2),
                    Mapper.Map<DomainInterface, Dto>(domainInstance3)
                };

                dtoCollection[0].Id = Guid.NewGuid();
                dtoCollection[0].DecimalValue = 1M;
                dtoCollection[0].Name = "Bob";
                dtoCollection[1].Id = Guid.NewGuid();
                dtoCollection[1].DecimalValue = 0.1M;
                dtoCollection[1].Name = "Frank";
                dtoCollection[2].Id = Guid.NewGuid();
                dtoCollection[2].DecimalValue = 2.1M;
                dtoCollection[2].Name = "Sam";

                Mapper.Map<Dto, DomainInterface>(dtoCollection[0], domainInstance1);
                Mapper.Map<Dto, DomainInterface>(dtoCollection[1], domainInstance2);
                Mapper.Map<Dto, DomainInterface>(dtoCollection[2], domainInstance3);

                Assert.AreEqual(dtoCollection[0].Id, domainInstance1.Id);
                Assert.AreEqual(dtoCollection[1].Id, domainInstance2.Id);
                Assert.AreEqual(dtoCollection[2].Id, domainInstance3.Id);

                Assert.AreEqual(dtoCollection[0].DecimalValue, domainInstance1.Nested.DecimalValue);
                Assert.AreEqual(dtoCollection[1].DecimalValue, domainInstance2.Nested.DecimalValue);
                Assert.AreEqual(dtoCollection[2].DecimalValue, domainInstance3.Nested.DecimalValue);

                Assert.AreEqual(dtoCollection[0].DecimalValue, domainInstance1.Nested.Name);
                Assert.AreEqual(dtoCollection[1].DecimalValue, domainInstance2.Nested.Name);
                Assert.AreEqual(dtoCollection[2].DecimalValue, domainInstance3.Nested.Name);
            }