//[Test] public void TestReflection() { TransformerObserver a = new TransformerObserver(); ITransformerResolver aa = a; ISourceMapper <Person, PersonaGiuridica> mapper0 = FactoryMapper.DynamicResolutionMapper <Person, PersonaGiuridica>(); SourceMapper <Person, PersonaGiuridica> mapper = new SourceMapper <Person, PersonaGiuridica>(new List <IPropertyMapper <Person, PersonaGiuridica> >(), null, null); //aa.Register<ISourceMapper<Person, PersonaGiuridica>>(mapper); //aa.Register(mapper0); object obj1 = new Person(); object obj2 = new Person(); Type t1 = typeof(IPersonHeader); Type t2 = typeof(Person); try { object instance = 5.5; //int i = (int) instance; byte bb = Convert.ToByte(instance); byte b = (byte)instance; double d = (double)instance; } catch (Exception) { Assert.IsFalse(true, "Cast invalid"); } Compare <IPersonHeader>(obj1, obj2); //Compare<long>(1, 10); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyMapper{TSource, TDestination}"/> class. /// </summary> /// <param name="propertySrc">The property source.</param> /// <param name="propertyDest">The property dest.</param> /// <exception cref="MapperParameterException">Unknown</exception> /// <exception cref="System.MissingMemberException"> /// </exception> /// <exception cref="LambdaSetterException">The setter action for property mapper cannot be null.</exception> public PropertyMapper(string propertySrc, string propertyDest) : base(propertySrc, propertyDest) { PropertyInfo srcProperty; PropertyInfo destProperty; try { srcProperty = typeof(TSource).GetProperty(propertySrc); destProperty = typeof(TSource).GetProperty(propertyDest); } catch (Exception ex) { throw new MapperParameterException("Unknown", string.Format("An error occurs when property source (name: {0}) or property destination (name: {1}) was used", propertySrc, propertyDest), ex); } if (srcProperty == null) { throw new MissingMemberException(typeof(TSource).Name, propertySrc); } if (destProperty == null) { throw new MissingMemberException(typeof(TSource).Name, propertyDest); } Action <TSource, TDestination> action = FactoryMapper.DynamicPropertyMap <TSource, TDestination>(srcProperty, destProperty); if (action == null) { throw new LambdaSetterException("The setter action for property mapper cannot be null."); } this.setter = action; }
public void TestDefaultContainer5() { string naming = "test naming"; var mapper = FactoryMapper.MakeDefaultBuilder <PersonaGiuridica, PersonDetails>() .Exclude("Code") .Exclude("Name") .Include( new PropertyMapper <PersonaGiuridica, PersonDetails>( (giuridica, details) => details.UpdateNome(naming))) .BuildMapper(); PersonaGiuridica person = new PersonaGiuridica { Code = "150", Name = "Sergio", Surname = "Hill", AnnoNascita = 1980, Parent = new Person { Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950 } }; var res = mapper.Map(person); Assert.IsNotNull(mapper); Assert.IsNull(res.Code); Assert.AreEqual(res.Name, naming); }
public void Test() { ISourceMapper <Student, Person> mapper1 = FactoryMapper.DynamicResolutionMapper <Student, Person>(); ISourceMapper mapper2 = FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(PersonDetails)); ISourceMapper mapper3 = FactoryMapper.DynamicResolutionMapper(typeof(IPersonHeader), typeof(PersonDetails)); 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> mapper4 = new SourceMapper <Student, Person>(propMappers, null, null); StudentDetails example = new StudentDetails(); ServiceTransformer <ISourceMapper <Student, Person> > srv1 = new ServiceTransformer <ISourceMapper <Student, Person> >("default", mapper4); var res1 = srv1.Match <ISourceMapper <Student, Person> >("default"); var res2 = srv1.Match <SourceMapper <Student, Person> >("default"); var res3 = srv1.Match <ISourceMapper <Student, Person> >("ss"); var res4 = srv1.Match <SourceMapper <Student, Person> >("ss"); var res5 = srv1.Match("default", example.GetType(), typeof(Person)); Assert.IsTrue(res1); Assert.IsFalse(res2); Assert.IsFalse(res3); Assert.IsFalse(res4); Assert.IsTrue(res5); }
public void TestOnAllProperties() { Type c = typeof(IClassC); var res = FactoryMapper.GetPropertiesOf(c); Assert.AreEqual(4, res.Length); }
/// <summary> /// Builds the automatic resolver merger for compatibles properties using the given key service for dedicated context. /// </summary> /// <param name="keyService">The key service.</param> /// <param name="sourceType">Type of the source.</param> /// <param name="destinationType">Type of the destination.</param> /// <returns></returns> public bool BuildAutoResolverMerger(object keyService, Type sourceType, Type destinationType) { ISourceMerger merger = new SourceMerger(sourceType, destinationType, FactoryMapper.GetDefaultPropertyMappers(sourceType, destinationType, this)); return(this.RegisterMerger(merger, keyService)); }
public void TestDefaultContainer3() { var container = FactoryMapper.MakeDefaultBuilder <PersonaGiuridica, PersonDetails>() .Exclude("") .Exclude((string)null); Assert.IsNotNull(container); }
/// <summary> /// Builds the automatic resolver merger for compatibles properties using the given key service for dedicated context and actions. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <typeparam name="TDestination">The type of the destination.</typeparam> /// <param name="keyService">The key service.</param> /// <param name="beforeMapping">The before mapping.</param> /// <param name="afterMapping">The after mapping.</param> /// <returns></returns> public bool BuildAutoResolverMerger <TSource, TDestination>(object keyService, Action <TDestination> beforeMapping, Action <TDestination> afterMapping) where TSource : class where TDestination : class { ISourceMerger merger = new SourceMerger <TSource, TDestination>( FactoryMapper.GetDefaultPropertyMappers <TSource, TDestination>(this), beforeMapping, afterMapping); return(this.RegisterMerger(merger, keyService)); }
public void AllPropertiesFromTree() { Type t0 = typeof(MyDerivedClass); const BindingFlags flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance; var prop = this.GetPropertiesFromDeclaredType(FactoryMapper.GetPropertiesOf(t0, flags), flags).ToArray(); Assert.NotNull(prop); Assert.AreEqual(4, prop.Length); Assert.AreEqual(4, prop.Count(n => (n.GetSetMethod() ?? n.GetSetMethod(true)) != null)); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyMapper{TSource, TDestination}"/> class. /// </summary> /// <param name="srcProperty">The source property.</param> /// <param name="destProperty">The dest property.</param> /// <param name="resolver">The resolver.</param> /// <exception cref="LambdaSetterException">The setter action for property mapper cannot be null.</exception> public PropertyMapper(PropertyInfo srcProperty, PropertyInfo destProperty, ITransformerResolver resolver) : base("[resolver]", destProperty.Name) { Action <TSource, TDestination> action = FactoryMapper.DynamicPropertyMap <TSource, TDestination>(srcProperty, destProperty, resolver); if (action == null) { throw new LambdaSetterException("The setter action for property mapper cannot be null."); } this.setter = action; }
public void TestDefaultMapper() { var mapper = FactoryMapper.DynamicResolutionMapper <Student, Person>(); Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 }; Person pr = mapper.Map(st); Assert.AreEqual(st.Name, pr.Name); Assert.AreEqual(st.Surname, pr.Surname); Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita); }
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); }
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); }
public TimeSpan GetElapsedFromMapResolver(List <Person> lista) { var mapper = FactoryMapper.DynamicResolutionMapper <Person, PersonaGiuridica>(); Stopwatch meter = new Stopwatch(); meter.Start(); foreach (var person in lista) { mapper.Map(person); } meter.Stop(); return(meter.Elapsed); }
public void ServiceTransformerMembersTest1() { object defaultKey = KeyService.Type1; ISourceMerger mapper = new SourceMerger <Student, Person>( FactoryMapper.GetDefaultPropertyMappers <Student, Person>(), null, null); var service = new ServiceTransformer <ISourceMerger>(defaultKey, mapper); Assert.IsNotNull(service); Assert.IsTrue(service.ServiceKey is Enum); Assert.AreEqual(service.ServiceKey, KeyService.Type1); }
public void ServiceTransformerMembersTest() { object defaultKey = " ciao "; ISourceMerger mapper = new SourceMerger <Student, Person>( FactoryMapper.GetDefaultPropertyMappers <Student, Person>(), null, null); var service = new ServiceTransformer <ISourceMerger>(defaultKey, mapper); Assert.IsNotNull(service); Assert.IsTrue(service.ServiceKey is string); Assert.AreNotEqual(service.ServiceKey, " ciao"); Assert.AreNotEqual(service.ServiceKey, "ciao "); Assert.AreEqual(service.ServiceKey, "ciao"); }
public void FailedTestDefaultMapper() { StringBuilder buffer = new StringBuilder(); var mapper = FactoryMapper.DynamicResolutionMapper <Student, Person> ( n => buffer.AppendLine(string.Format("ToString before mapping: {0}", n.ToString())), r => { throw new Exception("error"); } ); Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 }; mapper.Map(st); }
public void TestOnComplexDerivedClasses() { var mapper = FactoryMapper.DynamicResolutionMapper <MyDerivedClass, DerivedClassLast>(); Assert.NotNull(mapper); Assert.AreEqual(4, mapper.PropertyMappers.Count()); MyDerivedClass source = new MyDerivedClass(1, "mycomment", "mycomment2", 12.5); var res = mapper.Map(source); Assert.NotNull(res); Assert.AreEqual(source.Avarage, res.Avarage); Assert.AreEqual(source.Comment, res.Comment); Assert.AreEqual(source.CommentTwo, res.CommentTwo); Assert.AreEqual(source.Counter, res.Counter); }
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); }
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); }
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 TestNonGenericMapper1() { ISourceMapper mapper = FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(PersonDetails)); PersonaGiuridica person = new PersonaGiuridica { Code = "150", Name = "Sergio", Surname = "Hill", AnnoNascita = 1980, Parent = new Person { Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950 } }; var result = mapper.Map(person); Assert.IsNotNull(result); }
public void TestMapperNonPublicMembers() { ISourceMapper <PersonaGiuridica, PersonDetails> mapper = FactoryMapper.DynamicResolutionMapper <PersonaGiuridica, PersonDetails>(); PersonaGiuridica person = new PersonaGiuridica { Code = "150", Name = "Sergio", Surname = "Hill", AnnoNascita = 1980, Parent = new Person { Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950 } }; var result = mapper.Map(person); Assert.IsNotNull(result); }
/// <summary> /// Makes the transformer builder following the specified builder type. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <typeparam name="TDestination">The type of the destination.</typeparam> /// <param name="type">The builder type to use.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException">Builder type not implemented by now.</exception> public ITransformerBuilder <TSource, TDestination> MakeTransformerBuilder <TSource, TDestination>(BuilderType type) where TSource : class where TDestination : class { switch (type) { case BuilderType.DefaultMappers: return(new TransformerBuilder <TSource, TDestination>(this, FactoryMapper.GetDefaultPropertyMappers <TSource, TDestination>())); case BuilderType.Empty: return(new TransformerBuilder <TSource, TDestination>(this)); case BuilderType.DynamicResolver: return(new TransformerBuilder <TSource, TDestination>(this, FactoryMapper.GetDefaultPropertyMappers <TSource, TDestination>(this))); default: throw new NotImplementedException("Builder type not implemented by now."); } }
public void TestDefaultMapper2() { StringBuilder buffer = new StringBuilder(); var mapper = FactoryMapper.DynamicResolutionMapper <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 = 19 }; Person pr = mapper.Map(st); Assert.AreEqual(st.Name, pr.Name); Assert.AreEqual(st.Surname, pr.Surname); Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita); }
public void TestDefaultContainer6() { var mapper = FactoryMapper.MakeDefaultBuilder <PersonaGiuridica, PersonDetails>() .Exclude(typeof(PersonDetails).GetProperty("Code")) .BuildMapper(); PersonaGiuridica person = new PersonaGiuridica { Code = "150", Name = "Sergio", Surname = "Hill", AnnoNascita = 1980, Parent = new Person { Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950 } }; var res = mapper.Map(person); Assert.IsNotNull(mapper); Assert.IsNull(res.Code); }
public void TestDefaultContainer1() { var container = FactoryMapper.MakeDefaultBuilder <PersonaGiuridica, PersonDetails>(); Assert.IsNotNull(container); }
public async Task <IEnumerable <RoleDto> > GetAllRoles() { var roleList = await _roleDataService.GetAllRoles(); return(FactoryMapper.MapToDto(roleList.ToList())); }
public void FailedTestDestinationInterfaceType2() { FactoryMapper.DynamicResolutionMapper <PersonaGiuridica, IPersonHeader>(); }
public void FailedTestDestinationInterfaceType1() { FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(IPersonHeader)); }