Saturday, January 9, 2016

New Project: MiniMapper

When I write APIs, I usually create data models with properties that follow the database fields pretty closely, and data contracts that are more human readable. As such, the data models talk to the database and the data contracts interface with the client. I prefer a chatty API over a chunky API, so my data models and data contracts tend to be pretty flat.
Because of this, mapping between data model and data contract (and vice versa) is pretty straightforward. The time spent mapping the objects is really just a matter of “how fast can you type”. Things like AutoMapper are overkill. Don’t get me wrong. AutoMapper definitely has its place and it is a fantastic tool, but for what I’m doing now in my career, it’s just too much.
And I really don’t like writing code to map one object to another.
So I took some of the ideas floating around in my code and created MiniMapper. MiniMapper is lightweight, a total of 13kb for the two assemblies. And it doesn’t do a lot, but it does do what I need it to do, and I hope that it will be able to do the same for some others.

So what is different about MiniMapper compared to some other libraries?

Well, first, MiniMapper maps property to property. That’s it. There is no way to add logic when mapping the fields. I may add some type conversion later, but that’s it.
Second, MiniMapper relies on attributes on the source object to map the

Getting MiniMapper

MiniMapper is available on NuGet as two packages:

  • MiniMapper.Attributes - Install this package in your project with your data models or data contracts
  • MiniMapper.Core - Install this package in your project that will map one object to another

Using MiniMapper

The readme page on GitHub has much of this same code, but I’ll reproduce it here so you have an idea of how MiniMapper works.

Add attributes to the properties of your objects

You can map the properties of one object to the properties of another object just by adding an attribute to the property:

public class SourceClass
{
	[MapsTo("DestinationProperty")]
	public string SourceProperty { get; set; }
}

public class DestinationClass
{
	public string DestinationProperty {get; set; }
}

You can also specify that a property maps to another property depending on the destination object type:

public class SourceClass
{
	[MapsTo(“DestinationProperty1”, DestinationType = typeof(DestinationClass1))]
	[MapsTo(“DestinationProperty2”, DestinationType = typeof(DestinationClass2))]
	public string SourceProperty { get; set; }
}

public class DestinationClass1
{
	public string DestinationProperty1 { get; set; }
}

public class DestinationClass2
{
	public string DestinationProperty2 { get; set; }
}

Creating the maps

After you have added the attributes, you need to create the maps. This is done during initialization of your application. If you have a web application hosted in OWIN, for instance, you would do this in the Startup class.
For this example, we’ll use a console application:

using MiniMapper.Core;

namespace MiniMapper.Example
{
    class program
    {
        static void Main(string[] args)
        {
            Mapper.CreateMap();
        }
    }
}

Mapping from one object to another

When you need to map one object to another, you just do this:

var source = new SourceClass { SourceProperty = "Value" };
var destination = new DestinationClass();

destination = Mapper.Map(source, destination);

You can use this simplified syntax if you do not need to initialize the destination object:

var source = new SourceClass { SourceProperty = "Value" };

var destination = Mapper.Map(source);

That’s really all there is to it! I hope you enjoy the project, and please feel free to let me know what you think or to provide suggestions.

No comments :

Post a Comment