Map Objects to one Object

In my previous post I wrote about how we could use Dynamic Compilation for mapping object to object in a configuration file, mosly for fun to simply try something different. I spend a few minutes to see if I could do something more with it. It ended up that I can use it to several things, like creating Factories, or send in different kind of objects and map several into one. Here is how I for example can send in more domain object to map them into a PresentationCustomer:

var presentationCustomer = map.Map<PresentationCustomer>(
                                    "CustomerToPresentationCustomer", 
                                    customer, 
                                    address);

My configuration file look like this:

public PresentationCustomer CustomerToPresentationCustomer(Customer customer, Address address)
{
       return new PresentationCustomer()
                  {
                       ID = customer.ID,
                       CompanyName = customer.CompanyName,
                       FirstName = customer.FirstName,
                       LastName = customer.LastName,
                       Street = address.Street
                  };
}

Some comments I got was also to automatically map properties from object with the same name, so I added an extension method called MapTo, to make that possible, it will also open up support for mapping specific properties manually:

public PresentationCustomer CustomerToPresentationCustomer(Customer customer, string lastName)
{
      var presentationCustomer = new PresentationCustomer();

      customer.MapTo(presentationCustomer);

      presentationCustomer.FullName = customer.FirstName + " " + customer.LastName;

      return presentationCustomer;
}


I did also add support so other developers can simply add their own code which could be used by the configuration file. It's done by adding the namespace and an assembly of the code to the mapper.

DynamicMapper map = new DynamicMapper();

map.Assemblies.Add("MyHelper.dll");
map.Namespaces.Add("MyHelper");

var presentationCustomer = map.Map<PresentationCustomer>(
                                               "CustomerToPresentationCustomer", 
                                                customer, 
                                                "Normén");


Here is the MyHelper:


namespace MyHelper
{
    public static class MyExtension
    {
        public static string GetName(this string value)
        {
            return "Fredrik";
        }
    }
}


I can now use this extension method in my configuration file:


public PresentationCustomer CustomerToPresentationCustomer(Customer customer, string Name)
{
       return new PresentationCustomer()
                  {
                       ID = customer.ID,
                       CompanyName = customer.CompanyName,
                       FirstName = name.GetName(),
                       LastName = customer.LastName,
                       Street = address.Street
                  };
}


Because I can pass in several objects and map them to one single object, I can also pass in XmlDocument, DataSet, DataTable and SqlDataReaders etc and map them to an object:


var presentationCustomer = map.Map<PresentationCustomer>(
                                    "CustomerToPresentationCustomer", 
                                    customer, 
                                    sqlReader);
public PresentationCustomer CustomerToPresentationCustomer(Customer customer, SqlDataReader reader)
{
       return new PresentationCustomer()
                  {
                       ID = customer.ID,
                       CompanyName = customer.CompanyName,
                       FirstName = customer.FirstName,
                       LastName = customer.LastName,
                       Street = reader["Street"]
                  };
}


I can think of using this solution if I don't use an ORM but still want to bind data from a SqlDataReader to an object, or combine several object and use formatting and specifying default values which could be changed over time, and when it does I don't need to recompile my code, only change the configuration file.

No Comments