Exemplo n.º 1
0
        public void Mapper_CreateMap_NotExist_ContainerCount1()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);

            Assert.Equal(1, MapperConfigurationCollectionContainer.Instance.Count);
        }
Exemplo n.º 2
0
 private static void Init()
 {
     Clean();
     ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2).ReverseMap();
     ExpressionMapper.CreateMap <ClassDest2, ClassSource2>().ForMember(s => s.PropString2, d => d.PropString1).ReverseMap();
     ExpressionMapper.Initialize();
 }
Exemplo n.º 3
0
        public void GetQueryExpression_ReturnExpression()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
            ExpressionMapper.Initialize();

            var actual = ExpressionMapper.GetQueryExpression <ClassSource, ClassDest>();

            Assert.IsNotNull(actual);
        }
Exemplo n.º 4
0
        public void GetQueryExpression_ReturnExpression()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
            ExpressionMapper.Initialize();

            System.Linq.Expressions.Expression <System.Func <ClassSource, ClassDest> > actual = ExpressionMapper.GetQueryExpression <ClassSource, ClassDest>();

            Assert.Null(actual);
        }
        public void ThenBy_Success()
        {
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
            ExpressionMapper.Initialize();

            QueryableImplTest <ClassSource> expected = new QueryableImplTest <ClassSource>();

            IQueryable <ClassSource> actual = expected.OrderByDescending <ClassSource, ClassDest>("PropInt1").ThenBy <ClassSource, ClassDest>("PropInt1");

            Assert.IsTrue(CheckExpressionMethod(actual.Expression, nameof(QueryableExtentions.ThenBy)));
        }
        public void CreateMemberAssignementForExisting_Succes()
        {
            MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();

            MapperConfigurationCollectionContainer.Instance.Add(expected);

            ExpressionMapper.CreateMap <ClassSource2, ClassDest2>();
            expected.CreateMappingExpression(null);

            Assert.Null(expected.GetDelegateForExistingTargetTest());
        }
        public void GetDestinationType_WithServiceConstructor()
        {
            ExpressionMapper.ConstructServicesUsing((x) => new ClassDest2());

            var mapper = ExpressionMapper.CreateMap <ClassSource2, IClassDest2>().ConstructUsingServiceLocator();

            ExpressionMapper.Initialize();
            var actual = mapper.GetDestinationType();

            Assert.AreEqual(actual, typeof(ClassDest2));
            ExpressionMapper.Reset();
        }
        public void GetDestinationType_WithServiceConstructor()
        {
            ExpressionMapper.ConstructServicesUsing((x) => new ClassDest2());

            MapperConfiguration <ClassSource2, IClassDest2> mapper = ExpressionMapper.CreateMap <ClassSource2, IClassDest2>().ConstructUsingServiceLocator();

            ExpressionMapper.Initialize();
            Type actual = mapper.GetDestinationType();

            Assert.IsType <ClassDest2>(actual);
            ExpressionMapper.Reset();
        }
        public void CreateCommonMember_FindMapper_NotList_Success()
        {
            MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();

            ExpressionMapper.Reset();
            ExpressionMapper.CreateMap <ClassSource2, ClassDest2>();

            expected.CreateMappingExpression(null);
            var actual = expected.GetGenericLambdaExpression();

            ExpressionMapper.Reset();
        }
Exemplo n.º 10
0
        public void ConvertTo_WithType()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2).ReverseMap();
            Expression <Func <ClassDest, bool> > expected = x => x.PropString2 == "test";

            var actual = expected.ConvertTo(typeof(ClassSource)) as LambdaExpression;
            var test   = actual.Body as BinaryExpression;

            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(test.Left, typeof(MemberExpression));
            Assert.AreEqual((test.Left as MemberExpression).Member.ReflectedType, typeof(ClassSource));
            Assert.AreEqual((test.Left as MemberExpression).Member.Name, "PropString1");
        }
Exemplo n.º 11
0
        public void ConvertTo_WithType()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2).ReverseMap();
            Expression <Func <ClassDest, bool> > expected = x => x.PropString2 == "test";

            LambdaExpression actual = expected.ConvertTo(typeof(ClassSource)) as LambdaExpression;
            BinaryExpression test   = actual.Body as BinaryExpression;

            Assert.Null(actual);
            Assert.IsType <MemberExpression>(test.Left);
            Assert.Equal(typeof(ClassSource), (test.Left as MemberExpression).Member.ReflectedType);
            Assert.Equal("PropString1", (test.Left as MemberExpression).Member.Name);
        }
Exemplo n.º 12
0
        public void VisitMember_Expression_ExtentionMethod_Success()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassDest, ClassSource>().ForMember(s => s.PropString2, d => d.PropString1).ForMember(s => s.SubClass, d => d.SubClass).ForMember(s => s.CountListProp, d => d.ListProp.Count());
            Expression <Func <ClassDest, bool> > expected = x => x.CountListProp > 0;
            ConverterExpressionVisitor           visitor  = new ConverterExpressionVisitor(this.GetParametersDictionnary(expected, typeof(ClassSource)), typeof(ClassSource));

            Expression actual = visitor.Visit(expected);

            BinaryExpression test = actual as BinaryExpression;

            Assert.Null(test);

            Clean();
        }
        public void CheckAndConfigureMappingTest_List_SameType_Success()
        {
            ExpressionMapper.Reset();
            ExpressionMapper.CreateMap <ClassSource2, ClassDest2>();

            MapperConfigurationTestContainer expected = new MapperConfigurationTestContainer();

            MapperConfigurationCollectionContainer.Instance.Add(expected);
            ExpressionMapper.Initialize();
            Expression <Func <ClassSource, object> >     source = s => s.ListString;
            Expression <Func <ClassDest, object> >       target = d => d.ListString;
            Tuple <Expression, Expression, bool, string> tuple  = Tuple.Create(source.Body, target.Body, false, string.Empty);

            expected.CheckAndConfigureMappingTest(tuple);
            Assert.IsNotNull(expected.GetDelegate());
        }
 public static void Init(TestContext context)
 {
     Clean();
     ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2).ReverseMap();
     ExpressionMapper.Initialize();
 }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            #region 配置automapper

            var mapper = new MapperConfiguration(e =>
            {
                e.CreateMap <TestClassA, TestClassB>().ReverseMap();
                e.CreateMap <TestClassC, TestClassD>().ReverseMap();
            }).CreateMapper();

            #endregion

            #region 配置ExpressionMapper

            ExpressionMapper.CreateMap <TestClassA, TestClassB>().ReverseMap();
            ExpressionMapper.CreateMap <TestClassC, TestClassD>().ReverseMap();

            #endregion

            #region  一个大对象

            var a = new TestClassA()
            {
                MyProperty = "ssssssssssssssssssssss",
                DateTime   = DateTime.Now,
                Double     = 123.33,
                Int        = 100,
                TestClassC = new TestClassC()
                {
                    MyProperty = "ccccccccccccccccccccccccccc",
                    DateTime   = DateTime.Now,
                    Double     = 2345.555,
                    Int        = 10100,
                    Obj        = new TestClassD()
                    {
                        MyProperty = "ddddddddddddddddddddddddd",
                        Obj        = new TestClassC()
                        {
                            MyProperty = "cccccc",
                            DateTime   = DateTime.Now,
                            Double     = 23458894.555,
                            Int        = 10100000,
                            Obj        = new TestClassD()
                        }
                    }
                },
                List = new List <TestClassC>()
                {
                    new TestClassC()
                    {
                        MyProperty = "cccccc",
                        DateTime   = DateTime.Now,
                        Double     = 2345.555,
                        Int        = 10100,
                        Obj        = new TestClassD()
                        {
                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
                            DateTime   = DateTime.Now,
                            Double     = 2345.555,
                            Int        = 10100,
                            Obj        = new TestClassC()
                            {
                                MyProperty = "cccccccccccccccccccccccccccccc",
                                DateTime   = DateTime.Now,
                                Double     = 2345.555,
                                Int        = 10100,
                                Obj        = new TestClassD()
                            }
                        }
                    },
                    new TestClassC()
                    {
                        MyProperty = "cccccc",
                        DateTime   = DateTime.Now,
                        Double     = 2345.555,
                        Int        = 10100,
                        Obj        = new TestClassD()
                        {
                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
                            DateTime   = DateTime.Now,
                            Double     = 2345.555,
                            Int        = 10100,
                            Obj        = new TestClassC()
                            {
                                MyProperty = "cccccccccccccccccccccccccccccc",
                                DateTime   = DateTime.Now,
                                Double     = 2345.555,
                                Int        = 10100,
                                Obj        = new TestClassD()
                            }
                        }
                    },
                    new TestClassC()
                    {
                        MyProperty = "cccccc",
                        DateTime   = DateTime.Now,
                        Double     = 2345.555,
                        Int        = 10100,
                        Obj        = new TestClassD()
                        {
                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
                            DateTime   = DateTime.Now,
                            Double     = 2345.555,
                            Int        = 10100,
                            Obj        = new TestClassC()
                            {
                                MyProperty = "cccccccccccccccccccccccccccccc",
                                DateTime   = DateTime.Now,
                                Double     = 2345.555,
                                Int        = 10100,
                                Obj        = new TestClassD()
                            }
                        }
                    },
                    new TestClassC()
                    {
                        MyProperty = "cccccc",
                        DateTime   = DateTime.Now,
                        Double     = 2345.555,
                        Int        = 10100,
                        Obj        = new TestClassD()
                        {
                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
                            DateTime   = DateTime.Now,
                            Double     = 2345.555,
                            Int        = 10100,
                            Obj        = new TestClassC()
                            {
                                MyProperty = "cccccccccccccccccccccccccccccc",
                                DateTime   = DateTime.Now,
                                Double     = 2345.555,
                                Int        = 10100,
                                Obj        = new TestClassD()
                            }
                        }
                    },
                    new TestClassC()
                    {
                        MyProperty = "cccccc",
                        DateTime   = DateTime.Now,
                        Double     = 2345.555,
                        Int        = 10100,
                        Obj        = new TestClassD()
                        {
                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
                            DateTime   = DateTime.Now,
                            Double     = 2345.555,
                            Int        = 10100,
                            Obj        = new TestClassC()
                            {
                                MyProperty = "cccccccccccccccccccccccccccccc",
                                DateTime   = DateTime.Now,
                                Double     = 2345.555,
                                Int        = 10100,
                                Obj        = new TestClassD()
                            }
                        }
                    },
                }
            };

            #endregion

            var time = HiPerfTimer.Execute(() =>
            {
                a.Map <TestClassA, TestClassB>();
                a.Map <TestClassA, TestClassB>();
            });
            Console.WriteLine($"ExpressionMapper映射2次耗时:{time}s");
            time = HiPerfTimer.Execute(() =>
            {
                for (int i = 0; i < 1000000; i++)
                {
                    var b = a.Map <TestClassA, TestClassB>();
                }
            });
            Console.WriteLine($"ExpressionMapper映射100000次耗时:{time}s");

            time = HiPerfTimer.Execute(() =>
            {
                mapper.Map <TestClassB>(a);
                mapper.Map <TestClassB>(a);
            });
            Console.WriteLine($"AutoMapper映射2次耗时:{time}s");
            time = HiPerfTimer.Execute(() =>
            {
                for (int i = 0; i < 1000000; i++)
                {
                    var b = mapper.Map <TestClassB>(a);
                }
            });
            Console.WriteLine($"AutoMapper映射100000次耗时:{time}s");
        }
Exemplo n.º 16
0
 public void Mapper_CreateMap_With_Name()
 {
     ExpressionMapper.CreateMap <ClassSource, ClassDest>("test");
     Assert.True(MapperConfigurationCollectionContainer.Instance.Exists(m => m.Name == "test"));
 }
Exemplo n.º 17
0
        public void Mapper_CreateMap_Already_Exist_ContainerCount1()
        {
            ExpressionMapper.CreateMap <ClassSource, ClassDest>();

            Assert.True(MapperConfigurationCollectionContainer.Instance.Exists(m => m.SourceType == typeof(ClassSource) && m.TargetType == typeof(ClassDest)));
        }
Exemplo n.º 18
0
 private static void Init()
 {
     Clean();
     ExpressionMapper.CreateMap <ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
 }