public void Example() { // Create the object mapper var mapper = new ResourceMapper <SimpleDictionaryContext>(); mapper.RegisterOneWayMapping <SourceEntity, DestEntity>(mapping => { mapping.SetChildContext((from, to, context) => context.Set("ParentVariable", from.Id)); }); mapper.RegisterOneWayMapping <ChildEntity, ChildEntity>(mapping => { mapping.Set(to => to.ParentId, (from, to, context) => context.Get <int>("ParentVariable")); }); mapper.InitializeMap(); // Create source object var sourceObj = new SourceEntity { Id = 10, Child = new ChildEntity { Variable = 103 } }; var destObj = new DestEntity(); var mapContext = new SimpleDictionaryContext(); // Perform map mapper.Map(sourceObj, destObj, mapContext); Assert.Throws <KeyNotFoundException>(() => mapContext.Get <int>("ParentVariable")); Assert.AreEqual(sourceObj.Child.Variable, destObj.Child.Variable); Assert.AreEqual(sourceObj.Id, destObj.Child.ParentId); }
public void Example() { // Create our source object var sourceObj = new SourceEntity { Number = 10, NumberToString = -1000 }; var destObj = new DestEntity(); // Create the object mapper var mapper = new ResourceMapper <object>(); mapper.LoadStandardConverters(); // Load standard converters from System.Convert (e.g., int to string) mapper.RegisterOneWayMapping <SourceEntity, DestEntity>(mapping => { mapping.Set(to => to.DifferentlyNamedNumber, from => from.Number); // Directly assigns the source value to the destination value mapping.Set(to => to.DifferentlyNamedNumberToString, from => from.NumberToString); // There are many other variations of Set on the IMappingCollection<T> interface. Check these out on the API // Unspecified properties will be automapped after this point if not explicitly ignored using mapping.Ignore }); mapper.InitializeMap(); // Perform map destObj = mapper.Map(sourceObj, destObj, null); Assert.AreEqual(sourceObj.Id, destObj.Id); Assert.AreEqual(sourceObj.Number, destObj.DifferentlyNamedNumber); Assert.AreEqual(sourceObj.NumberToString.ToString(), destObj.DifferentlyNamedNumberToString); }
public void ComplexMapping_ExercisesAllSetters() { _mapper.LoadStandardConverters(); var domainClassObj = new DomainClassComplex(); _mapper.RegisterOneWayMapping <ResourceClassSimple, DomainClassComplex>(mapping => { mapping.Set(to => to.IntConversionProperty, from => ((InheritsFromResourceClassSimple)from).StringProperty); mapping.Set(to => to.StringConversionProperty, (from, to, context) => ((InheritsFromResourceClassSimple)from).IntProperty); mapping.Set(to => to.RecursiveExampleProperty, () => domainClassObj, false); mapping.Set(to => to.ExamplePropertyList, from => new [] { from }); mapping.Set(to => to.ExamplePropertyArray, from => new [] { from }); mapping.Set(to => to.ExampleProperty, from => new InheritsFromDomainClassSimple(), false); mapping.Set(to => ((InheritsFromDomainClassSimple)to.ExampleProperty).StringProperty, from => ((InheritsFromResourceClassSimple)from).StringProperty); }); _mapper.RegisterOneWayMapping <ResourceClassSimple, DomainClassSimple>(mapping => mapping.Ignore(to => to.RandomProperty)); _mapper.InitializeMap(); var sourceObj = _builder.Build <InheritsFromResourceClassSimple>(); var resultObj = _mapper.Map <ResourceClassSimple, DomainClassComplex>(sourceObj, null); Assert.AreEqual(sourceObj.StringProperty, resultObj.IntConversionProperty); Assert.AreEqual(sourceObj.IntProperty, resultObj.StringConversionProperty); Assert.AreSame(domainClassObj, resultObj.RecursiveExampleProperty); Assert.AreEqual(1, resultObj.ExamplePropertyList.Count); Assert.AreEqual(sourceObj.ExampleProperty, resultObj.ExamplePropertyList[0].ExampleProperty); Assert.AreEqual(1, resultObj.ExamplePropertyArray.Length); Assert.AreEqual(sourceObj.ExampleProperty, resultObj.ExamplePropertyArray[0].ExampleProperty); Assert.IsInstanceOf <InheritsFromDomainClassSimple>(resultObj.ExampleProperty); Assert.AreEqual(sourceObj.StringProperty, ((InheritsFromDomainClassSimple)resultObj.ExampleProperty).StringProperty); }
public void CreateAccessorChain_MultipleElements_WithConstruction_DeeperClass() { var resourceMapper = new ResourceMapper <object>(); resourceMapper.InitializeMap(); var chain = MapperUtils.CreateConstructingAccessorChain <object>(MemberExpressions.GetExpressionChain <DeeperClass>(c => c.DeepClass.Child.String), resourceMapper); Assert.IsNotNull(chain); var destination = new DeeperClass(); const string child = "teststring"; chain(destination, child, null); Assert.AreSame(child, destination.DeepClass.Child.String); }
public void Example() { // Create our source object var sourceObj = new SourceEntity { Number = 10, NumberToString = -1000 }; var destObj = new DestEntity(); // Create the object mapper var mapper = new ResourceMapper <object>(); mapper.LoadStandardConverters(); // Load standard converters from System.Convert (e.g., int to string) mapper.RegisterTwoWayMapping <SourceEntity, DestEntity>( sourceToDest => { sourceToDest.Set(to => to.DifferentlyNamedNumber, from => from.Number); sourceToDest.Set(to => to.DifferentlyNamedNumberToString, from => from.NumberToString); // Unspecified properties will be automapped after this point if not explicitly ignored using mapping.Ignore }, destToSource => { destToSource.Set(to => to.Number, from => from.DifferentlyNamedNumber); destToSource.Set(to => to.NumberToString, from => from.DifferentlyNamedNumberToString); // Unspecified properties will be automapped after this point if not explicitly ignored using mapping.Ignore }); mapper.InitializeMap(); // Perform map source => dest mapper.Map(sourceObj, destObj, null); Assert.AreEqual(sourceObj.Id, destObj.Id); Assert.AreEqual(sourceObj.Number, destObj.DifferentlyNamedNumber); Assert.AreEqual(sourceObj.NumberToString.ToString(), destObj.DifferentlyNamedNumberToString); // Perform map dest => source var newSourceObj = mapper.Map(destObj, new SourceEntity(), null); Assert.AreEqual(destObj.Id, newSourceObj.Id); Assert.AreEqual(destObj.DifferentlyNamedNumber, newSourceObj.Number); Assert.AreEqual(destObj.DifferentlyNamedNumberToString, newSourceObj.NumberToString.ToString()); }
public double BenchmarkTransmute(MapBuilder builderType) { // Resource mapper var mapper = new ResourceMapper <object>(builderType); mapper.LoadStandardConverters(); mapper.RegisterOneWayMapping <ResourceClassComplex, DomainClassComplex>(); mapper.RegisterOneWayMapping <ResourceClassSimple, DomainClassSimple>(mapping => mapping.Ignore(to => to.RandomProperty)); mapper.InitializeMap(); var start = DateTime.Now; for (int i = 0; i < Total; i++) { var domainObj = new DomainClassComplex(); mapper.Map(_resourceObj, domainObj, null); } var end = DateTime.Now; return((end - start).TotalMilliseconds); }
public void Example() { // Create our source object var sourceObj = new SourceEntity { Number = 10 }; sourceObj.NumberToString = -1000; var destObj = new DestEntity(); // Create the object mapper var mapper = new ResourceMapper <object>(); mapper.LoadStandardConverters(); // Load standard converters from System.Convert (e.g., int to string) mapper.RegisterOneWayMapping <SourceEntity, DestEntity>(); mapper.InitializeMap(); // Perform map mapper.Map(sourceObj, destObj, null); Assert.AreEqual(sourceObj.Number, destObj.Number); Assert.AreEqual(sourceObj.NumberToString.ToString(), destObj.NumberToString); }
public void RegisterOneWayMapping_SimpleObjects() { _mapper.InitializeMap(); var resourceObj = _builder.Build <ResourceClassSimple>(); var domainObj = new DomainClassSimple(); _mapper.Map(resourceObj, domainObj, null); Assert.AreEqual(resourceObj.ExampleProperty, domainObj.ExampleProperty); Assert.IsNull(domainObj.RandomProperty); }