示例#1
0
        public void PropertyMapInvalidOperationExistingMapTest()
        {
            var mapper = new ObjectMapper();

            mapper.CreateMap <User, UserDto>();

            Assert.Throws <InvalidOperationException>(() => {
                mapper.CreateMap <User, UserDto>();
            });
        }
示例#2
0
        public void MapDuplicated_ThrowsInvalidOperationException()
        {
            var mapper = new ObjectMapper();

            mapper.CreateMap <User, UserDto>();

            Assert.Throws <InvalidOperationException>(() =>
            {
                mapper.CreateMap <User, UserDto>();
            });
        }
示例#3
0
 public void RemoveyMapDestinationExceptionTest()
 {
     Assert.Throws <Exception>(() => {
         var mapper = new ObjectMapper();
         mapper.CreateMap <User, UserDto>().RemoveMapProperty(t => t);
     });
 }
示例#4
0
 public void PropertyMapSourceExceptionTest()
 {
     Assert.Throws <Exception>(() => {
         var mapper = new ObjectMapper();
         mapper.CreateMap <User, UserDto>().MapProperty(t => t.Role, s => s);
     });
 }
示例#5
0
 public void PropertyMapDestinationExceptionTest()
 {
     Assert.Throws <Exception>(() => {
         var mapper = new ObjectMapper();
         mapper.CreateMap <User, UserDto>().MapProperty(t => t, s => s.Role.Name);
     });
 }
示例#6
0
        public void PropertyMapInvalidOperationTypesNotMatchTest()
        {
            var mapper = new ObjectMapper();

            Assert.Throws <InvalidOperationException>(() => {
                mapper.CreateMap <User, AdminDto>();
            });
        }
示例#7
0
        public void RemoveyMapTest()
        {
            var mapper = new ObjectMapper();

            mapper.CreateMap <User, UserDto>().RemoveMapProperty(t => t.Email);

            var destination = mapper.Map <UserDto>(_sourceUser);

            Assert.IsNotNull(destination);
            Assert.AreEqual(_sourceUser.Name, destination.Name);
            Assert.IsNull(destination.Email);
            Assert.IsNull(destination.Role);
        }
示例#8
0
        public void PropertiesAreEquals_ReturnsTrue()
        {
            var mapper = new ObjectMapper();

            mapper.CreateMap <User, UserDto>().MapProperty(t => t.Role, s => s.Role.Name);

            var destination = mapper.Map <UserDto>(SourceUser);

            Assert.IsNotNull(destination);
            Assert.AreEqual(SourceUser.Name, destination.Name);
            Assert.AreEqual(SourceUser.Email, destination.Email);
            Assert.AreEqual(SourceUser.Role.Name, destination.Role);
        }
示例#9
0
        /// <summary>
        /// Finds by the specified Id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="includeAssociations">The include associations.</param>
        /// <returns></returns>
        public virtual TModel FindBy(Expression <Func <TModel, bool> > predicate, List <string> includeAssociations)
        {
            DbQuery <TContextEntity> dbSet = Context.Set <TContextEntity>();

            if (dbSet != null)
            {
                includeAssociations.ForEach(p => dbSet = dbSet.Include(p));
            }
            ObjectMapper.CreateMap <Expression <Func <TModel, bool> >, Expression <Func <TContextEntity, bool> > >();
            Expression <Func <TContextEntity, bool> > pred = ObjectMapper.Map <Expression <Func <TModel, bool> >, Expression <Func <TContextEntity, bool> > >(predicate);
            TContextEntity entity = dbSet.FirstOrDefault(pred);

            return(ObjectMapper.Map <TContextEntity, TModel>(entity));
        }
示例#10
0
        public void TestTimeMappingBasicListOfEntities(IFixture fixture)
        {
            const int ItemCount = 1000000;

            var entities = new List <EntityA>(ItemCount);

            for (var i = 0; i < ItemCount; i++)
            {
                entities.Add(new EntityA
                {
                    P0 = "1",
                    P1 = "2",
                    P2 = "2",
                    P3 = "3",
                    P4 = "4",
                    P5 = "5",
                    P6 = "6",
                    P7 = "7",
                    P8 = "8",
                    P9 = "9"
                });
            }

            var mapper = new ObjectMapper();

            mapper.CreateMap <EntityA, EntityB>();
            mapper.Configuration.Scanner.Enabled = false;
            mapper.Configuration.Initialize();

            var timerSimpleMapper = new Stopwatch();
            var timerManual       = new Stopwatch();
            var timerAutoMapper   = new Stopwatch();

            timerSimpleMapper.Start();

            var autoMappedItems = entities.MapTo <EntityB>().ToList();

            timerSimpleMapper.Stop();

            timerManual.Start();

            var manuallyMapped = entities.Select(e => new EntityB
            {
                P0 = e.P0,
                P1 = e.P1,
                P2 = e.P2,
                P3 = e.P3,
                P4 = e.P4,
                P5 = e.P5,
                P6 = e.P6,
                P7 = e.P7,
                P8 = e.P8,
                P9 = e.P9
            }).ToList();

            timerManual.Stop();

            AutoMapper.Mapper.CreateMap <EntityA, EntityB>();

            timerAutoMapper.Start();

            var result = AutoMapper.Mapper.Map <IEnumerable <EntityB> >(entities);

            timerSimpleMapper.Stop();

            Console.WriteLine("Mapped {3} entities, SimpleMapper: {0}ms TimeManual: {1}ms AutoMapper: {2}ms",
                              timerSimpleMapper.ElapsedMilliseconds, timerManual.ElapsedMilliseconds,
                              timerAutoMapper.ElapsedMilliseconds, ItemCount);
        }