Skip to content

Mapping.MapFieldAttribute

Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Mapping

Note that the mapper tries to convert the source values to their destination types.

ObjectToObject.cs

using System;

using NUnit.Framework;

using BLToolkit.Mapping;

namespace HowTo.Mapping
{
    [TestFixture]
    public class ObjectToObject
    {
        public class SourceObject
        {
            public bool   Value1   = true;
            public string Value2   = "10";
            public string StrValue = "test";
        }

        public class DestObject
        {
            [MapField("Value1")] public bool   BoolValue;
            [MapField("Value2")] public int    IntValue;

            // If the source and destination field/property names are equal,
            // there is no need for using the MapField attribute.
            //
                                 public string StrValue; 
        }

        [Test]
        public void Test1()
        {
            SourceObject source = new SourceObject();
            DestObject   dest   = Map.ObjectToObject<DestObject>(source);

            Assert.AreEqual(true,   dest.BoolValue);
            Assert.AreEqual(10,     dest.IntValue);
            Assert.AreEqual("test", dest.StrValue);
        }
    }
}

To map data to inner objects the MapField attribute can be used in the following way:

MapFieldAttribute.cs

using System;

using NUnit.Framework;

using BLToolkit.Mapping;

namespace HowTo.Mapping
{
    [TestFixture]
    public class MapField
    {
        public class SourceObject1
        {
            public string Street = "1 Main";
            public string City   = "Bigtown";
            public string State  = "XX";
            public string Zip    = "00000";
        }

        public class Address
        {
            public string Street;
            public string City;
            public string State;
            public string Zip;
        }

        [MapField("Street", "Address.Street")]
        [MapField("City",   "Address.City")]
        [MapField("State",  "Address.State")]
        [MapField("Zip",    "Address.Zip")]
        public class Order1
        {
            public Address Address = new Address();
        }

        [Test]
        public void MapFieldTest1()
        {
            SourceObject1 source = new SourceObject1();
            Order1        order  = Map.ObjectToObject<Order1>(source);

            Assert.AreEqual("1 Main",  order.Address.Street);
            Assert.AreEqual("Bigtown", order.Address.City);
            Assert.AreEqual("XX",      order.Address.State);
            Assert.AreEqual("00000",   order.Address.Zip);
        }

        public class SourceObject2
        {
            public string BillingStreet = "1 Main";
            public string BillingCity   = "Bigtown";
            public string BillingState  = "XX";
            public string BillingZip    = "00000";

            public string ShippingStreet = "2 Main";
            public string ShippingCity   = "Bigtown";
            public string ShippingState  = "XX";
            public string ShippingZip    = "00000";
        }

        public class Order2
        {
            [MapField(Format="Billing{0}")]
            public Address BillingAddress = new Address();

            [MapField(Format="Shipping{0}")]
            public Address ShippingAddress = new Address();
        }

        [Test]
        public void MapFieldTest2()
        {
            SourceObject2 source = new SourceObject2();
            Order2        order  = Map.ObjectToObject<Order2>(source);

            Assert.AreEqual("1 Main",  order.BillingAddress.Street);
            Assert.AreEqual("Bigtown", order.BillingAddress.City);
            Assert.AreEqual("XX",      order.BillingAddress.State);
            Assert.AreEqual("00000",   order.BillingAddress.Zip);

            Assert.AreEqual("2 Main",  order.ShippingAddress.Street);
            Assert.AreEqual("Bigtown", order.ShippingAddress.City);
            Assert.AreEqual("XX",      order.ShippingAddress.State);
            Assert.AreEqual("00000",   order.ShippingAddress.Zip);
        }
    }
}
Clone this wiki locally