コード例 #1
0
        public void FailedTestDefaultMerger()
        {
            StringBuilder buffer = new StringBuilder();

            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>
                (
                    r => { throw new Exception("error"); },
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);
        }
コード例 #2
0
        public void TestInterfaceToClass1()
        {
            var mapper = FactoryMapper.DynamicResolutionMapper<IPersonHeader, PersonDetails>();
            Assert.IsNotNull(mapper);

            IPersonHeader ps = new Person{ Name = "name", Surname = "surname", AnnoNascita = 1980, Parent = null };
            PersonDetails res = mapper.Map(ps);
            Assert.IsNotNull(res);
        }
コード例 #3
0
        public void TestInterfaceToClass2()
        {
            var mapper = FactoryMapper.DynamicResolutionMapper(typeof(IPersonHeader), typeof(PersonDetails));
            Assert.IsNotNull(mapper);

            IPersonHeader ps = new Person { Name = "name", Surname = "surname", AnnoNascita = 1980, Parent = null };
            object res = mapper.Map(ps);
            Assert.IsNotNull(res);
        }
コード例 #4
0
        public void TestDefaultMerger2()
        {
            StringBuilder buffer = new StringBuilder();

            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>
                (
                    n => buffer.AppendLine(string.Format("ToString before mapping: {0}", n.ToString())),
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
コード例 #5
0
        public void TestDefaultMergerIgnoringException()
        {
            StringBuilder buffer = new StringBuilder();

            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name, "Name", "Name")
                    ,new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                    ,new PropertyMapper<Student, Person>( (student, person) => person.Parent = ((Student)null).Father )
                };

            var merger = new SourceMerger<Student, Person>(propMappers,
                    null,
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.IgnoreExceptionOnMapping = true;
            merger.Merge(st, pr);

            merger.IgnoreExceptionOnMapping = false;
            merger.Merge(st, pr);
        }
コード例 #6
0
        public void TestDefaultMerger()
        {
            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>();

            Assert.IsTrue(merger.PropertyMappers.Count() == 3);

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
コード例 #7
0
        public void MakeTransformerBuilderTest4()
        {
            TransformerObserver observer = new TransformerObserver();
            var mapper = observer.MakeTransformerBuilder<Person, Student>(BuilderType.DynamicResolver);
            mapper.Include(new PropertyMapper<Person, Student>((person, student) => student.Father = person.Parent, "Father", "Parent"));
            mapper.BuildMapper();

            Assert.IsNotNull(mapper);

            Person ps = new Person
            {
                Name = "name",
                Surname = "surname",
                AnnoNascita = 1980,
                Parent = new Person
                {
                    Name = "parent_name",
                    Surname = "parent_surname"
                }
            };

            var resDto = observer.TryToMap<Person, Student>(ps);
            Assert.IsNotNull(resDto);
            Assert.IsNotNull(resDto.Father);

            var resDto1 = observer.TryToMap(ps, typeof(Student));
            Assert.IsNotNull(resDto1);
            Assert.IsNotNull(resDto1.GetType().GetProperty("Father").GetValue(resDto1, null));
            Assert.IsTrue(resDto1 is Student);

            var resDto2 = observer.TryToMap<Person, Student>(ps, "mykey");
            Assert.IsNull(resDto2);

            var resDto3 = observer.TryToMap(ps, typeof(Student), "mykey");
            var resDto4 = observer.TryToMap(ps, null, "mykey");

            Assert.IsNull(resDto3);
            Assert.IsNull(resDto4);
        }
コード例 #8
0
        public void MakeTransformerBuilderTest3()
        {
            TransformerObserver observer = new TransformerObserver();
            var mapper = observer.MakeTransformerBuilder<Person, Student>(BuilderType.DynamicResolver);
            /*
             * here It's possible to add / delete property mappers.
            */
            // before using the mapper object, It's needed to build it in order to associate into TransformerObserver
            mapper.BuildMapper();

            Assert.IsNotNull(mapper);

            Person ps = new Person
                {
                    Name = "name",
                    Surname = "surname",
                    AnnoNascita = 1980,
                    Parent = new Person
                        {
                            Name = "parent_name",
                            Surname = "parent_surname"
                        }
                };

            var resDto = observer.TryToMap<Person, Student>(ps);
            Assert.IsNotNull(resDto);
            Assert.IsNull(resDto.Father);

            var resDto1 = observer.TryToMap(ps, typeof(Student));
            Assert.IsNotNull(resDto1);
            Assert.IsNull(resDto1.GetType().GetProperty("Father").GetValue(resDto1, null));
            Assert.IsTrue(resDto1 is Student);

            var resDto2 = observer.TryToMap<Person, Student>(ps, "mykey");
            Assert.IsNull(resDto2);

            var resDto3 = observer.TryToMap(ps, typeof(Student), "mykey");
            Assert.IsNull(resDto3);
        }
コード例 #9
0
        public void MakeTransformerBuilderTest2()
        {
            TransformerObserver observer = new TransformerObserver();
            var mapper = observer.MakeTransformerBuilder<IPersonHeader, PersonDetails>(BuilderType.DefaultMappers);
            mapper.BuildMapper();

            Assert.IsNotNull(mapper);

            IPersonHeader ps = new Person { Name = "name", Surname = "surname", AnnoNascita = 1980, Parent = null };

            var resDto = observer.TryToMap<IPersonHeader, PersonDetails>(ps);
            Assert.IsNotNull(resDto);

            var resDto1 = observer.TryToMap(ps, typeof(PersonDetails));
            Assert.IsNotNull(resDto1);
            Assert.IsTrue(resDto1 is PersonDetails);

            var resDto2 = observer.TryToMap<IPersonHeader, PersonDetails>(ps, "mykey");
            Assert.IsNull(resDto2);

            var resDto3 = observer.TryToMap(ps, typeof(PersonDetails), "mykey");
            Assert.IsNull(resDto3);
        }
コード例 #10
0
        public void Test3()
        {
            Type t1 = typeof(ISimpleMapper<Student, Person>);
            Type t2 = typeof(ISourceMapper<Student, Person>);

            Assert.IsTrue(t1.IsAssignableFrom(t2));
            Assert.IsFalse(t2.IsSubclassOf(t1));

            Person p1 = new Person();
            PersonaGiuridica p2 = new PersonaGiuridica();

            Type t0 = p1.GetType();
            Assert.IsTrue(t0.IsInstanceOfType(p1));
            Assert.IsTrue(t0.IsInstanceOfType(p2));
        }