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 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());
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        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);
        }