AgileMapper: a zero-configuration, highly configurable, transparent, id-aware object mapper

Regular readers of my blog (maybe even both of you) will have seen sporadic references to 'my pet mapper project', going back to my first use of NDepend in December 2013. I've actually thrown it away and restarted a few times since January 2013, but it's now – finally! – in a beta stage I'm happy to talk about.

Given AutoMapper and ValueInjecter, why write a mapper? Well…

  • I spent the second half of 2012 working on an insurance application which required a lot of mapping between objects with 30 – 50 fields each. We did it all manually because we couldn't find a way in any of the available mappers to see what would and wouldn't be mapped
  • I thought it would be useful to have a mapper which can map anything to anything without configuration – like ValueInjecter – but with a friendly, fluent API for configuration when necessary – like AutoMapper
  • I thought it would be useful to have a mapper which – also without configuration – can pair up and update source and target objects using their identifiers
  • It's a difficult project (seriously – try it), so working on it and solving the problems therein would make me a better programmer

So I've written a mapper which solves the above problems.

Highlights

Create new objects from existing objects or perform deep clones

var customer = new Customer { Name = "Barney" };
var customerDto = mapper.Map(customer).ToANew<CustomerDto>();

var clonedCustomerDto = mapper.Clone(customerDto);

Update existing objects in an id-aware manner

var dtos = new[]
{
    new CustomerDto { Id = 1234, Name = "Maggie" },
    new CustomerDto { Id = 5678, Name = "Lisa" }
};
var customers = new[]
{
    new Customer { CustomerId = 5678 }, // Name updated to 'Lisa'
    new Customer { CustomerId = 1234 }  // Name updated to 'Maggie'
};
mapper.Map(dtos).Over(customers);

View (and cache) a mapping execution plan for mapping two types

var plan = mapper.GetPlanFor<PersonViewModel>().ToANew<Person>();
// 'plan' looks like this:

Other Stuff

Check it out!

AgileMapper is free, MIT licensed, available at version 0.6 on NuGet, and can be installed via the package manager console with:

PM> Install-Package AgileObjects.AgileMapper

It's hosted on GitHub with a pretty decent wiki, and the public API is all documented. I'll be reporting on and fine-tuning performance over the coming weeks, as well as adding new features like better support for Dictionaries. If you give it a try and find a bug please feel free to add it on GitHub. Enjoy!

Print | posted @ Monday, October 3, 2016 3:59 PM

Comments on this entry:

Gravatar # re: AgileMapper: a zero-configuration, highly configurable, transparent, id-aware object mapper
by Nicholas Petersen at 10/4/2016 6:19 PM

Definitely worth checking out sir, but it would really help to have a pure code comparison of let's say 10 examples where agilemapper does something (on the left) alongside on the right how to do that in automapper, if even available. It's not exactly clear to me the scope of what agilemapper does that automapper doesn't.

> "because we couldn't find a way in any of the available mappers to see what would and wouldn't be mapped" -- please give a concrete example of what you mean by this! How while coding could you possibly see "what would be mapped", without just looking at the results in a dubugger for instance?

By the way, it would help the clarity of your examples to not use var when the type returned is ambiguous and it's particular the types themselves were interested in.

Cheers
Gravatar # re: AgileMapper: a zero-configuration, highly configurable, transparent, id-aware object mapper
by Steve Wilkes at 10/4/2016 6:36 PM

Thanks for the feedback, Nicholas - I'll definitely be writing up examples and comparisons :)

I see where you're coming from with var in the examples, but aren't the types made obvious enough by the variable names and type arguments?

Gravatar # re: AgileMapper: a zero-configuration, highly configurable, transparent, id-aware object mapper
by Robert Santana at 10/5/2016 6:41 PM

Congratulations for this great idea and implementation!

I just have a suggestion:
Why, instead of developing another new (an awesome) mapper like this one, not to help with your brilliant ideas the development and improvement of other awesome ones, like my current favorite: https://github.com/fluentsprings/ExpressMapper?

I dream with the day when there will be one to rule them all!
Gravatar # re: AgileMapper: a zero-configuration, highly configurable, transparent, id-aware object mapper
by Steve Wilkes at 10/5/2016 7:28 PM

Hi Robert!

Thanks! I have to admit, ExpressMapper is a new one on me - I'll add it to future posts comparing features and performance.

I had specific ideas for things I wanted my mapper to do which didn't sit easily with existing mappers. AutoMapper (and ExpressMapper by the look of it) require up-front registration of the types you want to map - I didn't want to demand that. I wanted to surface the execution plans - I didn't know if that would fit into any of the existing options.

Still - thanks for the feedback! :)
Post A Comment
Title:
Name:
Email:
Comment:
Verification: