Los Techies : Blogs about software and anything tech!

AutoMapper: the Object-Object Mapper


In Domain-Driven Design, creating a rich domain model in code is essential for capturing the richness and complexity of the real-world domain.  These domain models, designed as POCOs, are not very portable, nor should they be.  Domain models live inside the domain layer, not to be exposed to the outside world.  Very often, we don’t want to expose these models to other layers of our application.  To mitigate this issue, we often create various sorts of other models, like DTOs, ViewModels, messages, and so on.  Two things were in common with these objects:

  • Mapping code is tedious to write
  • Mapping code is tedious to test

In fact, some of the most boring code in the world to write is mapping between two objects, as it often looked something like this:

public class OrderToOrderViewModelMapper
{
    public OrderViewModel Map(Order order)
    {
        return new OrderViewModel
            {
                CustomerFullName = order.Customer.GetFullName(),
                Total = order.GetTotal(),
                LineItems = order.GetLineItems()
                    .Select(x => new OrderLineItemViewModel
                        {
                            Price = x.Price,
                            Quantity = x.Quantity,
                            Total = x.GetTotal(),
                            ProductName = x.Product.Name
                        })
                    .ToArray()
            };
    }
}

That’s quite a bit of code to write, test and maintain, especially when all I’m trying to do here is carve a slice out of my rich domain model for consumption for something else, whether it’s my MVC view, JSON, a reporting application, and so on.  I hated writing this code over and over again, so instead our team brainstormed and decided to create an auto-mapper.

Enter the AutoMapper

If you look closely at the mapping logic above, the design of my destination type looks awfully similar to our domain model, just flattened out.  Let’s do that mapping code with AutoMapper:

public static void Configure()
{
    Mapper.CreateMap<Order, OrderViewModel>();
    Mapper.CreateMap<OrderLineItem, OrderLineItemViewModel>();
}

Whew, that was tough!  I create two maps, for each source/destination pair (remember, I had to map the OrderLineItem to the child line items as well).  Why don’t we actually use the Mapper to execute a map:

var viewModel = Mapper.Map<Order, OrderViewModel>(order);

Sweet fancy Moses, I’m out of breath.  But I lost my tests around the mapping code.  How do I make sure my configuration is valid?  From a feature I lifted from StructureMap:

Mapper.AssertConfigurationIsValid();

This will throw an AutoMapperConfigurationException (with details on the offending source type, destination type, and offending property(s).  AutoMapper enforces that for each type map (source/destination pair), all of the properties on the destination type are matched up with something on the source type.  Since AutoMapper matches type members up by name, you’re really looking at misspelled/missing members.

You do lose a little refactoring friendliness, but you’ll get the one-line descriptive test to let you know where you went awry.

AutoMapper conventions

Since AutoMapper flattens, it will look for:

  • Matching property names
  • Nested property names (Product.Name maps to ProductName, by assuming a PascalCase naming convention)
  • Methods starting with the word “Get”, so GetTotal() maps to Total
  • Any existing type map already configured

Basically, if you removed all the “dots” and “Gets”, AutoMapper will match property names.  Right now, AutoMapper does not fail on mismatched types, but for some other reasons.

AutoMapper feature rundown

AutoMapper supports:

  • Mapping of simple types
  • Mapping to arrays from any IEnumerable source
  • Custom member resolution, for that 1% case you have to do some extra mapping work
  • Polymorphic array mapping
  • Custom formatting for mapping from any type to a string
  • Global formatters
  • Null substitutes for formatting (i.e. a missing Product formats to “n/a”)
  • Profiles, for separating different sets of configurations (JSON vs. ViewModel vs. EditModel etc)

Other features include:

  • Convention of no null destination objects, ever.
  • No null destination array properties, ever.
  • A fluent configuration, with both a method chaining syntax (through Mapper) and object scoping (through a Profile)

What’s missing:

  • Auto-registration (mainly because you then can’t test it easily)
  • Support for conversion operators, as these are compile-time, not run-time in C#.  I can do it, but only by reflection and looking for crazy method names
  • Documentation

There are specs, mostly organized by feature area in the source code.  AutoMapper was built completely with TDD, so there aren’t any features that don’t have a test (and aren’t used in a production system).  You can find the latest release at the CodePlex site:

http://www.codeplex.com/AutoMapper

The source is hosted on my favorite free SVN host, Google Code:

http://code.google.com/p/automapperhome/

Enjoy!

Kick It on DotNetKicks.com
Posted Jan 22 2009, 09:33 PM by bogardj
Filed under:

Comments

DotNetShoutout wrote AutoMapper: the Object-Object Mapper
on 01-22-2009 11:05 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Liam McLennan wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 1:36 AM

Jimmy, I love this tool. Is there a way to use automapper to update an entity with a view model, rather than creating a new entity?

Tobin Harris wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 2:06 AM

Thanks Jimmy, looks brilliant. I think I'll be test driving this at the same time as Fluent NHibernate for a "walk in the park" db->model->viewmodel mapping experience :)  

Reflective Perspective - Chris Alcock » The Morning Brew #271 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #271
on 01-23-2009 2:42 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #271

Rasmus Kromann-Larsen wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 3:40 AM

Damn, I've been wanting to do this kind of project for so long. Oh well - at least now I can be lazy instead.

Gonna have a look at it :-)

Jeffrey Palermo (.com) wrote Jimmy Bogard Spawns His AutoMapper OOM (object-object mapper)
on 01-23-2009 7:15 AM

Although all we have to go on is his twitter announcement , Jimmy Bogard has put the project is up on CodePlex . It is version 0.1, so prepare to bleed if you want to use it (bleeding edge), but this library tries to fill the gap in the midst of object

Benny Thomas wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 7:17 AM

Looking good, gonna have a look at it!

Steve Strong wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 7:19 AM

Also wrote something similar a while back - details here:  blogs.imeta.co.uk/.../581.aspx

I like what you've done with the Method to Property mapping, and the automatic handling of nested types; might pick up those ideas in mine at some point.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 7:29 AM

@Liam

That's something we've looked at for a while (two-way binding), but in the end, we found that there was just too much business validation in an Update.

Instead, we formed other patterns around updating a model from a message/form.  See Code Camp Server source for more details.

Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew
on 01-23-2009 9:37 AM

Pingback from  Dew Drop - January 23, 2009 | Alvin Ashcraft's Morning Dew

Jeff Gonzalez wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 10:14 AM

Man - love it!

This is yucky, but there seems to be no support for this:

           Mapper.CreateMap<List<Dog>, List<Cat>>();

           var results = Mapper.Map<List<Dog>, List<Cat>>(dogs);

Probably a good thing - it'll force me to not do silly things like that, but would be nice on occasion.

Thanks!

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 10:56 AM

@Jeff

No, but you can do this:

Mapper.CreateMap<Dog, Cat>();

Mapper.Map(someDogArrayOrListOrAnythingIEnumerable, typeof(Dog), typeof(Cat)) => returns a Cat[] (never null)

I just don't have a way to do it with generics....but it's probably a great idea, supporting a generic way of making an array.

You could try Mapper.Map<Dog[], Cat[]>(dogArrayOrWhatever), that might work.

Josh Schwartzberg wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 11:59 AM

Is there a way to ignore/exclude properties from the mapping?  Would also be cool if we generated a proxy class so we could lazy load the properties.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 12:56 PM

@Josh

On the first point, yes, there's an "Ignore()" option for a member mapping.

CreateMap<Foo, Bar>().ForMember(x => x.Blarg, opt => opt.Ignore());

On the second, do you have a scenario in mind where lazy loading/deferred execution is needed, and you want to come out of a Map operation with not all the data there?

Usually, I design each DTO specifically for each need, not much reuse.

huey wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 4:01 PM

I had always wondered if I was missing the point of DDD when doing object-object mapping.  Glad to see it is common :)

Erik wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 6:04 PM

I'm still missing the point - I don't understand why reusing the domain model to populate the presentation layer is a bad thing. I just posted on StackOverflow about this -- stackoverflow.com/.../mvc-data-models-and-view-models -- can someone please help me to fill in the blanks?

Liam McLennan wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 6:51 PM

@bogardj

It would be nice to have the option of naive two-way binding. It could always be overriden for special scenarios. In keep my validaiton in my domain model so my mappings tend to be equally simple in both directions.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-23-2009 7:16 PM

@Liam

To be honest, we just haven't needed it yet.  We're moving towards a paradigm of entities updating themselves from messages.  I do see the need, maybe an email with conventions/scenarios you're looking for?  Even a failing unit test would be flippin' awesome :)

Our entities were just too complex to be bound from messages, with complex collection operations, value objects, local entities, etc.  There really wasn't a "convention" around being able to populate our model, nor did we want one, the names and operations needed to come from the ubiquitous language.

Steve wrote re: AutoMapper: the Object-Object Mapper
on 01-28-2009 8:38 PM

Can you map from a Request.Form collection to an object with the mapper ?

Nicholas Becker wrote re: AutoMapper: the Object-Object Mapper
on 01-28-2009 9:37 PM

@Erik

I don't personally have a huge problem with using the model directly to populate view pieces, but from my experience the pain isn't generally with meshing a conceptual difference in view/model data or representations, but rather from dealing with issues like null checks, and the data type discrepancies (i.e. using a bool vs. outputting "Yes" or "No").  Having a view object that just has strings as fields makes the view so much cleaner to look at, and simple to work with too.  Dealing with the same translation crap over and over again is just a PITA to deal with all the time, and this is a solution to that problem.

I can honestly say, having worked on a project that used the Automapper, that it's a pain to think of starting a project that didn't have it.  

Marcel Popescu wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 2:03 AM

You have an Initalize method - it should be Initialize.

I have a mapping that's missing a couple of fields - how do I tell it to add those? (That is, the source has fewer fields than the ViewModel - I know how to instantiate the missing fields, but how do I tell that to the mapper?)

Thanks for the project, it was annoying to do this by hand :)

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 6:21 AM

@Steve

I'd have to hear more about your situation, but you'd probably want to use a different mechanism to do so, such as MVC's ModelBinder.  Right now AutoMapper supports only no-arg methods and properties on source types.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 7:32 AM

@Marcel

To do that, use a custom value resolver.  Just create a class that implements IValueResolver, then tell AutoMapper to use it for that one field.

ForMember(m => m.Foo, opt => opt.ResolveUsing<CustomFooResolver>())

Hallgrim wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 8:57 AM

We needed mapping from List<T> to List<T>, so we checked out the sourcecode and made something that seams to work fine. It actually supports mapping from IEnumerable to List<T> if there exist a mapping for the source element type and <T>. Maybe you could use our results? Maybe you can do it more generic too, like support IList<T> and ICollection<T> or something?

We added a new test in the Map method:

private object Map(ResolutionContext context) {

...

               else if ((context.DestinationType.IsGenericType && (context.DestinationType.GetGenericTypeDefinition().Equals(typeof(List<>).GetGenericTypeDefinition()))) && (context.SourceValue is IEnumerable))

               {

                   valueToAssign = CreateListObject(context);

               }

...

}

Here's the code based on the CreateArrayObject method in the MappingEngine.cs class:

       private object CreateListObject(ResolutionContext context)

       {

           IEnumerable<object> enumerableValue = ((IEnumerable)context.SourceValue).Cast<object>();

           Type sourceElementType = GetElementType(context.SourceType);

           Type destElementType = context.DestinationType.GetGenericArguments()[0];

           var dstList = (IList)Activator.CreateInstance(context.DestinationType);

           int i = 0;

           foreach (object item in enumerableValue)

           {

               Type targetSourceType = sourceElementType;

               Type targetDestinationType = destElementType;

               if (item.GetType() != sourceElementType)

               {

                   TypeMap itemTypeMap = Configuration.FindTypeMapFor(sourceElementType, destElementType);

                   targetSourceType = item.GetType();

                   targetDestinationType = itemTypeMap.GetDerivedTypeFor(targetSourceType);

               }

               TypeMap derivedTypeMap = Configuration.FindTypeMapFor(targetSourceType, targetDestinationType);

               var newContext = context.CreateElementContext(derivedTypeMap, item, targetSourceType, targetDestinationType, i);

               object mappedValue = Map(newContext);

               dstList.Add(mappedValue);

               i++;

           }

           object valueToAssign = dstList;

           return valueToAssign;

       }

Hallgrim wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 9:22 AM

One more note.

With the new method we created you only have to configure mappings for the element types. Like this:

Mapper.CreateMap<Dog, Cat>();

You DON'T need to specify it like this:

Mapper.CreateMap<List<Dog>, List<Cat>>();

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-29-2009 2:18 PM

@Hallgrim

Awesome.  I'm setting up an AutoMapper-users group, but I'd love to get a patch on this one.  Can you contact me through the blog above? Thanks!

matt wrote re: AutoMapper: the Object-Object Mapper
on 01-30-2009 2:02 AM

For enum mapping support would I be looking at writing a custom resolver or is that supported as well? How do you typically handle those?

Hallgrim wrote re: AutoMapper: the Object-Object Mapper
on 01-30-2009 7:03 AM

Patch for List<T> support uploaded to the AutoMapper-users group

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-30-2009 7:27 AM

@matt

If you could, would you mind putting up some mapping that doesn't work up on the AutoMapper users mailing list?  Or better yet, a failing unit test.  It's easier if I have a scenario with code to look at, as we're doing quite a bit of Enum mapping in our codebase.

matt wrote re: AutoMapper: the Object-Object Mapper
on 01-30-2009 3:04 PM

@bogardj

Sure thing - I've got a test fixture prepared - I'll send it to the list. Thanks!

Clément Bouillier wrote re: AutoMapper: the Object-Object Mapper
on 02-03-2009 4:50 AM

Hi,

Initially, I've got the same needs as yours (mapping object to data contracts of WebServices is really boring and error prone). I was searching for an Object-to-Object Mapper, I do not find it in .NET community, so have a look at Java community and found "Dozer".

Then, I make some search and find some interesting posts giving some start points to implement it. I have started, wanted to publish it on CodePlex and finally do not have time. I think it would be interesting to share my work with you (it would be ridiculous to make two different tools I think).

That's something we talk about in a Alt.net Paris meeting. And I see that there are other needs for this type of tool (dynamic mapping).

I have started a post on my "starting" blog, but I would like to detail it a bit before publish. Keep an eye.

Clément

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 02-03-2009 7:26 AM

@Clement

Sounds good, I'll keep an eye on it!

Martin Nyborg wrote re: AutoMapper: the Object-Object Mapper
on 02-08-2009 4:12 PM

The mapper returns a new object and for 99% of the cases this is god.

like this:

Mapper.CreateMap<Model.Site.RamSite, RamSite>();

RamsiteDTO = Mapper.Map<Model.Site.RamSite, RamSite>(ramsite);

But the DTO object has a lot going on in the property setters like NotifyChange and validations rules and I need to create the object first to bypass this checks.

var RamsiteDTO = new RamSite();

using (RamsiteDTO.BypassPropertyChecks)

{

Mapper.CreateMap<Model.Site.RamSite, RamSite>();

Mapper.Map<Model.Site.RamSite, RamSite>(ramsite, RamsiteDTO);

}

Is this possible?

I have also posted this on Codeplex

Pat Gannon wrote re: AutoMapper: the Object-Object Mapper
on 02-13-2009 8:21 PM

I'm really surprised there doesn't seem to be any support at all for auto-mapping fields.  (Public fields on DTOs make for cleaner messages when using the DataContractSerializer.)

Kristian Erbou wrote re: AutoMapper: the Object-Object Mapper
on 02-14-2009 7:56 AM

Hi Jimmy

I've tried after a few Hello World-examples to implement mapping between a source DataRow object and a target Person-object - I guess it will be possible to do it with a class implementing IValueResolver but I haven't tried it out yet (is it possible?).

However - if you retrieve a DataTable with tablecolumns named i.e. ID, Name and Adress from your database it would be awesome to have the conventionbased rules engine to check for matching property names in the target object if the source is i.e. a DataRow or an equivilant data carrier. Will you consider the following scenario for a future release?

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 02-14-2009 4:38 PM

@Pat

No, not yet.  That would be easy to do, however.

Nathan wrote re: AutoMapper: the Object-Object Mapper
on 02-15-2009 12:16 PM

Jimmy, re: field mapping, Easy yes, trivial no. Tragically, PropertyInfo and FieldInfo, despite sharing large portion of their API, don't share a common root. That means that for AutoMapper to support field mapping I think it would require creating a common interface, like IPropertyInfo, a wrapper that implements that interface and that can contain an inner Member info and treat PropertyInfo and FieldInfo the same, and touching all the places in the AutoMapper code that use PropertyInfo to replace them with IPropertyInfo, and of course everywhere that talks directly to the .NET reflection system would need to be have a shim inserted as well. This could all be accomplished in a few hours, but if we were to make those changes, what are the chances they could be included in the project? If that is unlikely, what are the chances this functionality will naturally evolve out of AutoMapper?

Nathan wrote re: AutoMapper: the Object-Object Mapper
on 02-15-2009 4:59 PM

Never mind - I went ahead and made the changes and uploaded them as a patch (created using tortoise - never done that before, hope it is useful) to the Codeplex site. The patch includes two sets of tests, copied from existing tests but using fields instead of properties.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 02-15-2009 10:06 PM

@Nathan

I was in the process of adding it myself, but a patch works just as well!  Thanks!

Nathan wrote re: AutoMapper: the Object-Object Mapper
on 02-16-2009 7:33 PM

What a fast response! Awesome! Love the tool BTW. We are currently working on two ends of a messaging system, using a hand mapping process on the one end to go from entities in one source system to common DTO's, and evaluating AutoMapper on the other end, going from DTO's to a different breed of entities, and AutoMapper makes for a nice clean mapping layer when you compare the two side by side.

Billy McCafferty wrote A response to Validation in a DDD world
on 02-17-2009 2:17 PM

Jimmy Bogard presented a well written argument for always keeping validation out of entities in a domain

Community Blogs wrote A response to Validation in a DDD world
on 02-17-2009 2:25 PM

Jimmy Bogard presented a well written argument for always keeping validation out of entities in a domain

theusualsuspect.com » Simplifying View Model to Domain Model Mapping wrote theusualsuspect.com &raquo; Simplifying View Model to Domain Model Mapping
on 02-19-2009 2:30 AM

Pingback from  theusualsuspect.com » Simplifying View Model to Domain Model Mapping

Richard Dingwall » Domain entities vs presentation model objects, part 2: mapping wrote Richard Dingwall &raquo; Domain entities vs presentation model objects, part 2: mapping
on 03-02-2009 4:10 AM

Pingback from  Richard Dingwall  » Domain entities vs presentation model objects, part 2: mapping

Adventures in Software » Blog Archive » AutoMapper misses the point wrote Adventures in Software &raquo; Blog Archive &raquo; AutoMapper misses the point
on 04-19-2009 11:57 PM

Pingback from  Adventures in Software  » Blog Archive   » AutoMapper misses the point

Xsword wrote re: AutoMapper: the Object-Object Mapper
on 05-11-2009 8:31 PM

Would you like to see NBear.Mapping in www.nbear.org?

The project in CodePlex is nbear.codeplex.com.

Xsword wrote re: AutoMapper: the Object-Object Mapper
on 05-11-2009 8:32 PM

Would you like to see NBear.Mapping in www.nbear.org?

The project in CodePlex is nbear.codeplex.com.

Xsword wrote re: AutoMapper: the Object-Object Mapper
on 05-11-2009 8:36 PM

Would you like to visit the project NBear(www.nbear.org)?

The project is also in codeplex(nbear.codeplex.com).

The nbear.mapping component is design for DataSet <-> object and object <-> object.Thank you.

Craig Shoemaker wrote AutoMapper with Jimmy Bogard
on 05-20-2009 1:52 PM

Jimmy Bogard , lead developer for the AutoMapper project, joins us today to discuss the inner workings

ConfusedInAlberta wrote re: AutoMapper: the Object-Object Mapper
on 05-25-2009 6:20 PM

Can we get some examples of how ot use AutoMapper?

i.e.what are all the methods available and when to use them. Also how to meet this never null requirement?

mainly I'm getting a compiler error trying to use the AutoMapper to map from an IEnumerable to IEnumerable ( why does it have to be an Array?? I need to transform the results if necessary and do not want to be converting back and forth.

I have a simple IMapper that already does this here is the signature if you could help clarify which Automapper method could best accomplish this it would be appreciated:

public static IEnumerable<Out> MapAll<In, Out>(this IEnumerable<In> input, IMapper<In, Out> mapper)

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 05-25-2009 9:36 PM

@Confused

Can you do me a huge favor?  Would you mind reposting this on the AutoMapper mailing list?  That way, if anyone else is interested in these questions, they can get the answer too :)

groups.google.com/.../automapper-users

Utils anti-pattern & AutoMapper : David Laing’s blog wrote Utils anti-pattern &#038; AutoMapper : David Laing&#8217;s blog
on 06-02-2009 5:18 AM

Pingback from  Utils anti-pattern & AutoMapper : David Laing’s blog

… what AutoMapper can do for you « What I've Learned Today wrote &#8230; what AutoMapper can do for you &laquo; What I&#039;ve Learned Today
on 06-04-2009 10:20 PM

Pingback from  … what AutoMapper can do for you « What I've Learned Today

Richard Dingwall » AutoMapper: writing a custom IValueResolver for Gravatar wrote Richard Dingwall &raquo; AutoMapper: writing a custom IValueResolver for Gravatar
on 06-10-2009 6:26 AM

Pingback from  Richard Dingwall  » AutoMapper: writing a custom IValueResolver for Gravatar

espinete wrote re: AutoMapper: the Object-Object Mapper
on 09-09-2009 2:09 AM

www.codeplex.com/AutoMapper broken link !!! thanks

Dale Smith wrote re: AutoMapper: the Object-Object Mapper
on 09-12-2009 11:56 AM

Just so you know, this is one of the awesomest pieces of awesome we have put to use in my office in the past year.  Thanks for a great tool!

Giovanni Coppola wrote re: AutoMapper: the Object-Object Mapper
on 09-18-2009 2:18 AM

codeplex link is still down. has it been moved elsewhere?

kind regards,

links for 2009-10-13 « Murratore’s Weblog wrote links for 2009-10-13 &laquo; Murratore&#8217;s Weblog
on 10-13-2009 8:03 PM

Pingback from  links for 2009-10-13 « Murratore’s Weblog

Marvion wrote re: AutoMapper: the Object-Object Mapper
on 11-04-2009 9:05 PM

Hi, Very useful tool.

But i have a question.

does this run slower than manual entity mapping or not?

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 11-04-2009 11:08 PM

@Marvion

Yep, it runs slower.  You trade less code and a more powerful tool for the speed.  However, it's not much slower from the numbers I've run, and is very far from the bottleneck in our apps we use it on.

JOe wrote re: AutoMapper: the Object-Object Mapper
on 11-06-2009 9:35 PM

Hello,

Will automapper work with a type that does not have a parameterless constructor? If not,would it be possible to pass in a delegate to return a new instance of the type when you register it?

Thanks.

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 11-07-2009 8:49 AM

@Joe

Yes, there is a ConstructedBy method that accepts a Func<TSource, TDestination> for custom construction of the destination type.

Ken Baltrinic wrote re: AutoMapper: the Object-Object Mapper
on 12-23-2009 12:20 PM

Jimmy,  

I am trying to use automapper in IIS 7 with signed assemblies and it chokes with a "Request Failed" error.  Prior to finding your post here I posted a detailed explanation and request for help on stackoverflow.  (stackoverflow.com/.../problem-with-automapper-in-iis7-when-using-signed-assemblies).

Could you take a look and tell me if I am doing something wrong or if there is a compatibility problem?  Not being able to use AutoMapper in IIS7 is killing us.

Regards,

Ken

Sanjay wrote re: AutoMapper: the Object-Object Mapper
on 01-08-2010 7:19 AM

Hi, Here are my concerns

The Automapper only maps if the Source and the Destination properties which share the same name. for instance i will not be able to map CustId with CustomerId

It does even map nullable dataTypes properly - if the value

is provided as Null - It maps and puts the value as an empty string or zero in case of Nullable Int.

Works fine with complex datatypes- Not sure how will it behave if the class is nested.

Guess, the next version should use some kind of refelection methodologies to map accordingly

Sanjay wrote re: AutoMapper: the Object-Object Mapper
on 01-08-2010 7:25 AM

Applogies

Hi Juimmy, Here are my concerns

The Automapper only maps if the Source and the Destination properties share the same name. for instance i will not be able to map CustId with CustomerId

It does not map nullable dataTypes properly - if the value

is provided as Null - It maps and puts the value as an empty string or zero in case of Nullable Int.

Works fine with complex datatypes- Not sure how will it behave if the class is nested.

Guess, the next version should use some kind of refelection methodologies to map accordingly

Thanks & Regards

Sanjay JDM

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-08-2010 8:50 AM

@Sanjay

Could you post these issues on the google group?  Thanks!

Pedro Dias wrote re: AutoMapper: the Object-Object Mapper
on 01-27-2010 2:45 PM

Test is looking good:

-- code --

// Prepare

Mapper.CreateMap< Gallery, GalleryVm >( );

Mapper.CreateMap< Album, AlbumVm >( );

Mapper.CreateMap< Slide, SlideVm >( );

Slide slide = new Slide{Title = "I'm a slide" };

Album album = new Album{ Title = "I'm an Album", Slides = new List< Slide >{ slide } };

Gallery gallery = new Gallery{ Title = "I'm a Gallery", Albums = new List< Album >{ album } };

// Act

GalleryVm vm = Mapper.Map< Gallery, GalleryVm >( gallery );

// Assert

Mapper.AssertConfigurationIsValid( );

-- end code --

Questions:

Q: In a large solution (100+ projects), will the auto mapper be able to live? It seems like it's storing all the maps statically? (I haven't downloaded it's source yet)

Q: Is  this using reflection, or are the maps generated during compile time?

Looking awesome otherwise, simply awesome!

bogardj wrote re: AutoMapper: the Object-Object Mapper
on 01-27-2010 3:40 PM

@pedro

100+ projects???!?!  I think you'll have other issues before Automapper becomes your problem.

It actually uses reflection.emit to perform mapping, Configuration is done once per app domain.

Pedro Dias wrote re: AutoMapper: the Object-Object Mapper
on 01-28-2010 12:29 AM

I'm a consultant, the 100+ projects are my customers work. Unfortunately, I have to relate to them, and try to make them better. Automapper would go a long way in refactoring the app :)

Tom's ramblings wrote Build asp.net MVC applications FAST with MVCExtensions v0.4
on 02-05-2010 5:31 AM

Build asp.net MVC applications FAST with MVCExtensions v0.4

DotNetKicks.com wrote AutoMapper: the Object-Object Mapper
on 02-18-2010 3:17 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

lucisferre wrote Using an AutoMapFilter for Model -> ViewModel mapping
on 02-23-2010 10:28 PM

Using an AutoMapFilter for Model -> ViewModel mapping

Ramani Sandeep wrote re: AutoMapper: the Object-Object Mapper
on 02-27-2010 2:15 AM

Good one.. very useful..

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems