Sunday, January 24, 2016

MiniMapper updated to version 1.1

I pushed an update to MiniMapper this weekend. The most important change is an improvement to exception handling when creating the maps and when mapping the objects. I also added type conversion when mapping properties. Turned out to be a more of a challenge than I expected, but I ended up learning something out of it.

And MiniMapper is a more stable project, so there’s that.

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.

Friday, July 3, 2015

Introducing Furball

I've been doing a lot with OWIN lately, thanks in part to the excellent IdentityServer3 project from Dominick Baier and Brock Allen.  They've really created a great product and forced me to learn more about OWIN than I expected to.  So once I started learning more about OWIN I decided that I wanted to write something that would take advantage of some of the knowledge that I gained.

With that in mind, I decided to create my first repository at Github and make my first open source project.  Meet Furball.  The name comes from the fact that there is almost always a cat sitting on my lap when I program at home, so it seemed appropriate for now.

So what exactly is Furball?  It's a lightweight framework for creating RESTful APIs.  Similar to Microsoft's Web Api, but lighter weight, and hopefully with a little more flexibility.  I want to separate the code for the API from the actual framework as much as possible to make the API code easier to unit test.

This is still an early alpha project, as I am trying to prove out the concept.  Some of the code is ugly too.  But it's a start.