I’m using ASP.NET MVC 3, with Raven DB as a backing data store. I have a set of models, which I’m interested in transforming into ViewModels. To accomplish this, I’m leveraging AutoMapper to take care of the dirty work of mapping each property to the corresponding one in the ViewModel. Let’s say that I’ve got a model like so:
public class FooModel
{
public int Id { get; set; }
public string Name { get; set; }
public int AlphaId { get; set; }
public int BetaId { get; set; }
}
And then let’s say I want to transform it into a ViewModel like so:
public class FooViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int AlphaId { get; set; }
public Alpha Alpha { get; set; }
public int BetaId { get; set; }
public Beta Beta { get; set; }
}
My map is then set up like this at app startup:
Mapper.CreateMap<Foo, FooViewModel>();
Then, in the controller I’m performing the map as so:
public ActionResult FooDetails(string id)
{
using(var session = this.documentStore.OpenSession())
{
var fooInstance = session.Load<Foo>(id);
var fooViewModel = Mapper.Map<FooViewModel>(fooInstance);
return this.View(fooViewModel);
}
}
The problem is that as you can see above, the entity coming out of the repository has 2 properties which are the keys of other objects, of types Alpha and Beta. I’m interested in hydrating Alpha and Beta based upon the AlphaId and BetaId keys.
At first I thought I’d leverage AutoMapper’s custom conversion capabilities, but I don’t think that will work given that we would need a data session to be injected into the mapping (to call the data store to retrieve the Alpha or Beta object).
The other option is to just do all the work in the controller action, but that quickly gets unwieldy (not in the particular example given, but thats just an example to illustrate the point).
Where should the hydration of Alpha and Beta take place, and what’s a good pattern here?
It perhaps isn’t ideal, but could you not just create maps for the two references and map them after the fact. You still have to call them seperately, but you’re letting AutoMapper do the heavy mapping, rather than filling your controller with your own maps.
I haven’t used AutoMapper all that much, but perhaps you can pass in child objects through the Map function (to feed the ForMember chains on CreateMap)? Perhaps Alexandre’s AfterMap suggestion might work, but I don’t see how you’d hydrate the ‘referenced’ children without creating the map inside the controller action (but then you may as well just use ForMember and the Raven session directly and have only one Mapper.Map).
Addition – The AutoMapper documentation suggests a similar method for nested/child maps – https://github.com/AutoMapper/AutoMapper/wiki/Nested-mappings. Although it’s more akin to an object reference, rather than a object id reference situation.
I can’t comment on other answers yet, but with regard to Pavel’s answer – you don’t really use Raven in that way, there is no o/r mapping (other than the internal JSON -> object) or data access layer, you just use the Raven session directly (perhaps through a service, but there’s certainly no need for a repository or similar). With regards to referencing, byte has it right and as he commented on one of the other answers, the best approach is to store an id reference and use a Raven include – it’s the same result and still uses only one request.