Category Archives: TinyMapper

Object mapping with TinyMapper

I’ve written a few posts on AutoMapper in the past.

AutoMapper and the subject of this post, TinyMapper, are object mappers which basically means translating/mapping one object to another object. That is to say you have (for example) a DTO (data transfer object) used to transfer data via REST or SOAP services. This object is in a format best suited to the transfer process, or is simply a format that doesn’t match with how your application domain models might use the data. This is not the only scenario for using object mapping, our objects might match, but types differ in some subtle (or not so subtle) ways or we want to simple using an object mapper to clone/copy objects that are of the same type.

Let’s look at a simple example

Let’s take the simplest of examples whereby we have two objects which look almost exactly alike, differing only in one of the types used

public class PersonDto
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string DateOfBirth { get; set; }
}

public class Person
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public DateTime DateOfBirth { get; set; }
}

As you can see, the property names are the same, but the DateOfBirth string needs to map to a System.DateTime.

Let’s use TinyMapper to map our objects

AutoMapper is an excellent tool for object mapping, but sometimes it might feel like using a “sledgehammer to crack a nut”. If you are not using it’s full set of capabilities or more importantly find that it’s a little slow for your needs, TinyMapper is an option.

TinyMapper purports to be quite a bit faster than AutoMapper, but obviously at the expense of all the AutoMapper capabilities. If you don’t need everything AutoMapper offers then TinyMapper makes a lot of sense.

Add the nuget package TinyMapper to your project and here’s some code

var dto = new PersonDto
{
   FirstName = "Scooby",
   LastName = "Doo",
   DateOfBirth = "13 September 1969"
};

TinyMapper.Bind<Person, PersonDto>();

var person = TinyMapper.Map<Person>(dto);

Note: Scooby do was no born on 13th September 1969, this was when the show debuted on CBS.

The line TinyMapper.Bind<Person, PersonDto>(); sets up TinyMapper so that it knows it’s meant to map these two types together when required to using the Map method. The first generic argument is the source and the second the target type. However it really just means which two types do you want to bind together, so the order seems unimportant.

The final line then tells TinyMapper to Map the PersonDto to a new instance of a Person.

As you might expect, because the property names are the same, this is all we need for the mapping to succeed, the DateOfBirth type difference doesn’t matter as these are convertible from one type to the other. The names, however do matter.

What if my property names don’t match

If your property names differ, then we simply need to tell TinyMapper what should map to what. Let’s change our Dto DateOfBirth property to DOB. If you run the previous code you’ll get (as you probably expect) a null or default value for missing mappings, so in this case the Person DateOfBirth property will equal DateTime.MinValue.

If we change our Bind method to the following

TinyMapper.Bind<PersonDto, Person>(cfg =>
{
   cfg.Bind(o => o.DOB, o => o.DateOfBirth);
});

All other properties remain mapped, based upon the property names, but now DOB is mapped to DateOfBirth.

Ignoring properties

Sometimes we want to not map a property, i.e. ignoring it. This can be handled via the configuration of the Bind method like this

TinyMapper.Bind<PersonDto, Person>(cfg =>
{
   cfg.Ignore(o => o.LastName);
}

Summary

TinyMapper is very simple to use and might has minimal configuration options, but it’s also very fast, so depending upon your needs, TinyMapper might suit better as the object mapper
for your project.