Example #1
0
        public void RegisterMapperTest3()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father)
            };

            ISourceMapper mapper1 = new SourceMapper <Student, Person>(propMappers, null, null);
            ISourceMapper <Student, Person> mapper2 = new SourceMapper <Student, Person>(propMappers, null, null);

            Assert.IsTrue(observer.BuildAutoResolverMapper <User, UserDto>(null, null));               //register a mapper which transfomr User into UserDto (1)
            Assert.IsTrue(observer.BuildAutoResolverMapper <Student, Person>(null, null));             //register a mapper which transfomr Student into Person (1)

            Assert.IsFalse(observer.RegisterMapper(mapper1));                                          // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) )
            Assert.IsFalse(observer.RegisterMapper(mapper2));                                          // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) )

            Assert.IsFalse(observer.BuildAutoResolverMapper <User, UserDto>(null, null));              //It's equals to mapper (1), so It cannot be registered.
            Assert.IsFalse(observer.BuildAutoResolverMapper <User, UserDto>(null, Console.WriteLine)); //It's similar to mapper (2), so It cannot be registered.

            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type1));                         // this mapper can be registered 'cause It was registered with another KeyService nevertheless using a similar mapper ( as (1) )
            Assert.IsFalse(observer.RegisterMapper(mapper2, KeyService.Type1));                        // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) ), using the same serviceKey like a previous registration.
        }
Example #2
0
        public void RegisterMapperTest2()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father)
            };
            SourceMapper <Student, Person> mapper1 = new SourceMapper <Student, Person>(propMappers, null, null);

            Assert.IsTrue(observer.RegisterMapper(mapper1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type2));
            Assert.IsFalse(observer.RegisterMapper(mapper1));
            Assert.IsFalse(observer.RegisterMapper(mapper1, "default"));

            Student st = new Student {
                Name = "mario", Surname = "monti", AnnoNascita = 19
            };

            var res1 = observer.TryToMap <Student, Person>(st);

            Assert.IsNotNull(res1);

            var res11 = observer.TryToMap(st, typeof(Person));

            Assert.IsTrue(res11 is Person);
            Assert.IsNotNull(res11);


            var res2 = observer.TryToMap(st, typeof(Person), KeyService.Type1);

            Assert.IsNotNull(res2);

            var res22 = observer.TryToMap(st, typeof(Person), KeyService.Type1);

            Assert.IsNotNull(res22);


            var res0 = observer.TryToMap(st, typeof(Person), KeyService.Type3);

            Assert.IsNull(res0);

            var res00 = observer.TryToMap <Student, Person>(st, KeyService.Type3);

            Assert.IsNull(res00);
        }
Example #3
0
        public void BuildAutoResolverMapperTest5()
        {
            TransformerObserver observer = new TransformerObserver();

            Assert.IsTrue(observer.BuildAutoResolverMapper(typeof(CustomSimpleTypeDto), typeof(CustomSimpleType)));
            Assert.IsTrue(observer.RegisterMapper(new SimpleMapper <int?, int>(i => i.GetValueOrDefault())));

            CustomSimpleTypeDto instance = new CustomSimpleTypeDto
            {
                Naming = "naming",
                Code   = 10
            };

            var res = observer.TryToMap <CustomSimpleTypeDto, CustomSimpleType>(instance);

            Assert.IsNotNull(res);
            Assert.AreEqual(res.Naming, instance.Naming);
            Assert.AreEqual(res.Code, instance.Code);

            var res1 = observer.TryToMap(instance, typeof(CustomSimpleType)) as CustomSimpleType;

            Assert.IsNotNull(res1);
            Assert.AreEqual(res1.Naming, instance.Naming);
            Assert.AreEqual(res1.Code, instance.Code);
        }
Example #4
0
        public void RegisterMapperTest1()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father)
            };
            SourceMapper <Student, Person> mapper1 = new SourceMapper <Student, Person>(propMappers, null, null);

            observer.RegisterMapper(mapper1);
            Student st = new Student {
                Name = "mario", Surname = "monti", AnnoNascita = 19
            };
            StudentDetails st2 = new StudentDetails {
                Name = "mario", Surname = "monti", AnnoNascita = 19, CF = "CF"
            };

            var res  = observer.TryToMap <Student, Person>(st);
            var res2 = observer.TryToMap <Student, Person>(st2);

            Assert.IsNotNull(res);
            Assert.IsNotNull(res2);
        }
Example #5
0
        public void RegisterSimpleMapperTest1()
        {
            TransformerObserver observer = new TransformerObserver();
            var mapper = new SimpleMapper <int?, int>(i => i.HasValue ? i.Value : 0);

            Assert.IsTrue(observer.RegisterMapper(mapper));
            var res = observer.TryToMap <int?, int>(5);

            Assert.AreEqual(res, 5);

            var mapper1 =
                new SimpleMapper <KeyService, KeyServiceOther>(
                    service => (KeyServiceOther)Enum.ToObject(typeof(KeyServiceOther), service));

            Assert.IsTrue(observer.RegisterMapper(mapper1));
            var res1 = observer.TryToMap <KeyService, KeyServiceOther>(KeyService.Type2);

            Assert.AreEqual(res1, KeyServiceOther.Type2);

            var res2 = observer.TryToMap <KeyService, KeyServiceOther>(KeyService.Type3);

            Assert.AreEqual(res2, KeyServiceOther.Type3);
        }
Example #6
0
        public void BuildAutoResolverMapperTest3()
        {
            TransformerObserver observer = new TransformerObserver();

            Assert.IsTrue(observer.BuildAutoResolverMapper(typeof(CustomComplexType), typeof(CustomComplexTypeDto)));
            Assert.IsTrue(observer.BuildAutoResolverMapper <User, UserDto>(null, null));
            Assert.IsTrue(observer.RegisterMapper(new SimpleMapper <KeyService, KeyServiceOther>(service => (KeyServiceOther)Enum.ToObject(typeof(KeyServiceOther), service))));

            CustomComplexType instance = new CustomComplexType
            {
                MyKeyService  = KeyService.Type2,
                ComplexNaming = "complex_naming",
                Owner         = new User
                {
                    Name    = "name1",
                    Surname = "surname1",
                    Parent  = new User
                    {
                        Name    = "parteName1",
                        Surname = "parentSurname1",
                        Parent  = new User
                        {
                            Name    = "parentParentName1",
                            Surname = "parentParentSurname1",
                            Parent  = new User()
                        }
                    }
                }
            };

            var res = observer.TryToMap <CustomComplexType, CustomComplexTypeDto>(instance);

            Assert.IsNotNull(res);
            Assert.AreEqual(res.MyKeyService, KeyServiceOther.Type2);
            Assert.AreEqual(res.ComplexNaming, instance.ComplexNaming);
            Assert.IsNotNull(res.Owner);

            var res1 = observer.TryToMap(instance, typeof(CustomComplexTypeDto)) as CustomComplexTypeDto;

            Assert.IsNotNull(res1);
            Assert.AreEqual(res1.MyKeyService, KeyServiceOther.Type2);
            Assert.AreEqual(res1.ComplexNaming, instance.ComplexNaming);
            Assert.IsNotNull(res1.Owner);

            var res2 = observer.TryToMap(KeyService.Type1, null);

            Assert.IsNull(res2);
        }
Example #7
0
        public void TestMapper()
        {
            TransformerObserver             observer = new TransformerObserver();
            ISourceMapper <Student, Person> mapper1  = FactoryMapper.DynamicResolutionMapper <Student, Person>();

            observer.RegisterMapper(mapper1);
            Student st = new Student {
                Name = "mario", Surname = "monti", AnnoNascita = 19
            };

            var res = observer.TryToMap <Student, Person>(st);

            Assert.IsNotNull(res);
            Assert.AreEqual(st.Name, res.Name);
            Assert.AreEqual(st.Surname, res.Surname);
            Assert.IsNull(st.Father);
            Assert.IsNull(res.Parent);
        }
        public void BuildAutoResolverMapperTest3()
        {
            TransformerObserver observer = new TransformerObserver();
            Assert.IsTrue(observer.BuildAutoResolverMapper(typeof(CustomComplexType), typeof(CustomComplexTypeDto)));
            Assert.IsTrue(observer.BuildAutoResolverMapper<User, UserDto>(null, null));
            Assert.IsTrue(observer.RegisterMapper(new SimpleMapper<KeyService, KeyServiceOther>(service => (KeyServiceOther)Enum.ToObject(typeof(KeyServiceOther), service))));

            CustomComplexType instance = new CustomComplexType
                {
                    MyKeyService = KeyService.Type2,
                    ComplexNaming = "complex_naming",
                    Owner = new User
                    {
                        Name = "name1",
                        Surname = "surname1",
                        Parent = new User
                        {
                            Name = "parteName1",
                            Surname = "parentSurname1",
                            Parent = new User
                            {
                                Name = "parentParentName1",
                                Surname = "parentParentSurname1",
                                Parent = new User()
                            }
                        }
                    }
                };

            var res = observer.TryToMap<CustomComplexType, CustomComplexTypeDto>(instance);
            Assert.IsNotNull(res);
            Assert.AreEqual(res.MyKeyService, KeyServiceOther.Type2);
            Assert.AreEqual(res.ComplexNaming, instance.ComplexNaming);
            Assert.IsNotNull(res.Owner);

            var res1 = observer.TryToMap(instance, typeof(CustomComplexTypeDto)) as CustomComplexTypeDto;
            Assert.IsNotNull(res1);
            Assert.AreEqual(res1.MyKeyService, KeyServiceOther.Type2);
            Assert.AreEqual(res1.ComplexNaming, instance.ComplexNaming);
            Assert.IsNotNull(res1.Owner);

            var res2 = observer.TryToMap(KeyService.Type1, null);
            Assert.IsNull(res2);
        }
        public void TestMapper()
        {
            TransformerObserver observer = new TransformerObserver();
            ISourceMapper<Student, Person> mapper1 = FactoryMapper.DynamicResolutionMapper<Student, Person>();

            observer.RegisterMapper(mapper1);
            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };

            var res = observer.TryToMap<Student, Person>(st);
            Assert.IsNotNull(res);
            Assert.AreEqual(st.Name, res.Name);
            Assert.AreEqual(st.Surname, res.Surname);
            Assert.IsNull(st.Father);
            Assert.IsNull(res.Parent);
        }
        public void RegisterSimpleMapperTest1()
        {
            TransformerObserver observer = new TransformerObserver();
            var mapper = new SimpleMapper<int?, int>(i => i.HasValue ? i.Value : 0);

            Assert.IsTrue(observer.RegisterMapper(mapper));
            var res = observer.TryToMap<int?, int>(5);

            Assert.AreEqual(res, 5);

            var mapper1 =
                new SimpleMapper<KeyService, KeyServiceOther>(
                    service => (KeyServiceOther)Enum.ToObject(typeof(KeyServiceOther), service));

            Assert.IsTrue(observer.RegisterMapper(mapper1));
            var res1 = observer.TryToMap<KeyService, KeyServiceOther>(KeyService.Type2);
            Assert.AreEqual(res1, KeyServiceOther.Type2);

            var res2 = observer.TryToMap<KeyService, KeyServiceOther>(KeyService.Type3);
            Assert.AreEqual(res2, KeyServiceOther.Type3);
        }
        public void RegisterMapperTest3()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father )
                };

            ISourceMapper mapper1 = new SourceMapper<Student, Person>(propMappers, null, null);
            ISourceMapper<Student, Person> mapper2 = new SourceMapper<Student, Person>(propMappers, null, null);

            Assert.IsTrue(observer.BuildAutoResolverMapper<User, UserDto>(null, null));     //register a mapper which transfomr User into UserDto (1)
            Assert.IsTrue(observer.BuildAutoResolverMapper<Student, Person>(null, null));   //register a mapper which transfomr Student into Person (1)

            Assert.IsFalse(observer.RegisterMapper(mapper1));           // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) )
            Assert.IsFalse(observer.RegisterMapper(mapper2));           // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) )

            Assert.IsFalse(observer.BuildAutoResolverMapper<User, UserDto>(null, null));    //It's equals to mapper (1), so It cannot be registered.
            Assert.IsFalse(observer.BuildAutoResolverMapper<User, UserDto>(null, Console.WriteLine));   //It's similar to mapper (2), so It cannot be registered.

            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type1));           // this mapper can be registered 'cause It was registered with another KeyService nevertheless using a similar mapper ( as (1) )
            Assert.IsFalse(observer.RegisterMapper(mapper2, KeyService.Type1));           // this mapper cannot be registered 'cause It was registered another similar mapper ( as (1) ), using the same serviceKey like a previous registration.
        }
        public void RegisterMapperTest2()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father )
                };
            SourceMapper<Student, Person> mapper1 = new SourceMapper<Student, Person>(propMappers, null, null);

            Assert.IsTrue(observer.RegisterMapper(mapper1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type2));
            Assert.IsFalse(observer.RegisterMapper(mapper1));
            Assert.IsFalse(observer.RegisterMapper(mapper1, "default"));

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };

            var res1 = observer.TryToMap<Student, Person>(st);
            Assert.IsNotNull(res1);

            var res11 = observer.TryToMap(st, typeof(Person));
            Assert.IsTrue(res11 is Person);
            Assert.IsNotNull(res11);

            var res2 = observer.TryToMap(st, typeof(Person), KeyService.Type1);
            Assert.IsNotNull(res2);

            var res22 = observer.TryToMap(st, typeof (Person), KeyService.Type1);
            Assert.IsNotNull(res22);

            var res0 = observer.TryToMap(st, typeof (Person), KeyService.Type3);
            Assert.IsNull(res0);

            var res00 = observer.TryToMap<Student, Person>(st, KeyService.Type3);
            Assert.IsNull(res00);
        }
        public void RegisterMapperTest1()
        {
            TransformerObserver observer = new TransformerObserver();
            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.Father )
                };
            SourceMapper<Student, Person> mapper1 = new SourceMapper<Student, Person>(propMappers, null, null);

            observer.RegisterMapper(mapper1);
            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };
            StudentDetails st2 = new StudentDetails { Name = "mario", Surname = "monti", AnnoNascita = 19, CF = "CF"};

            var res = observer.TryToMap<Student, Person>(st);
            var res2 = observer.TryToMap<Student, Person>(st2);
            Assert.IsNotNull(res);
            Assert.IsNotNull(res2);
        }
        public void BuildAutoResolverMapperTest5()
        {
            TransformerObserver observer = new TransformerObserver();
            Assert.IsTrue(observer.BuildAutoResolverMapper(typeof(CustomSimpleTypeDto), typeof(CustomSimpleType)));
            Assert.IsTrue(observer.RegisterMapper(new SimpleMapper<int?, int>(i => i.GetValueOrDefault() )));

            CustomSimpleTypeDto instance = new CustomSimpleTypeDto
            {
                Naming = "naming",
                Code = 10
            };

            var res = observer.TryToMap<CustomSimpleTypeDto, CustomSimpleType>(instance);
            Assert.IsNotNull(res);
            Assert.AreEqual(res.Naming, instance.Naming);
            Assert.AreEqual(res.Code, instance.Code);

            var res1 = observer.TryToMap(instance, typeof(CustomSimpleType)) as CustomSimpleType;
            Assert.IsNotNull(res1);
            Assert.AreEqual(res1.Naming, instance.Naming);
            Assert.AreEqual(res1.Code, instance.Code);
        }