After reading ASP.NET MVC 2 in Action and watching Jimmy Bogard's presentation from MvcConf (both highly recommended!), I began to implement some of their ideas.
One of the cool things they do, is not only to use AutoMapper to map your entities to some viewmodel, but automate this with an AutoMapViewResult:
public class EventsController : BaseController
{
public ActionResult Show(Event id) // EntityModelBinder gets Event from repository
{
return AutoMapView<EventsShowModel>(id); // AutoMapView<T>(model) is a helper method on the BaseController, that calls AutoMapViewResult<T>(...)
}
}
// not exactly what you'll find in the book, but it also works :-)
public class AutoMapViewResult<TDestination> : ViewResult
{
public AutoMapViewResult(string viewName, string masterName, object model)
{
ViewName = viewName;
MasterName = masterName;
ViewData.Model = Mapper.Map(model, model.GetType(), typeof(TDestination));
}
}
This all works great, but now there's a Edit
action with its EventsEditModel
:
public class EventsEditModel
{
// ... some properties ...
public int LocationId { get; set; }
public IList<SelectListItem> Locations { get; set; }
}
public class EventsController : BaseController
{
public ActionResult Edit(Event id)
{
return AutoMapView<EventsEditModel>(id);
}
}
And now (finally) the question:
What do you think, is the best way to get the locations from some sort of data source such as a repository to the EventsEditModel
's Locations
property?
Keep in mind, that I want to use the AutoMapViewResult
and a lot of different entity-viewmodel combinations.
Update:
I went with Necros' idea and created a custom attribute. You can look at the code and download it on my blog ASP.NET MVC: Loading data for select lists into edit model using attributes.
Well, add a constructor with a parameter and a property into your controller and use DI (personally I like Ninject) to inject the correct repository implementation:
Wire (bind) the dependencies up in the global.asax.cs in Ninject application and site module (if you need expanded answer with that included, please let me know),
then in your Edit action use the repository to get the
Locations
. Suppose you have theLoadLocations()
method on your repository interface and concrete implementation of it in, for instance,SqlEventsRepository
(implementsIEventsRepository
), you do it simply by calling the method:I am making this up because you have not provided too much information. And I don't know Automapper specifics when you want to load some additional data from the datastore prior to mapping the Entity to the ViewModel.
Also you don't specify whether this Edit action is
GET
orPOST
, but I assume it isGET
. Assuming that it's reallyGET
, I don't know how can you load anything by providing the Entity to the action.Most commonly the
GET
methods use parameters of type string or int (most likely those are slugs or ids of somekind) andPOST
methos use parameters of type ViewModel (not Entity).So you POST method signature should be like this
I used to use Entities directly in my action signatures and was failing all the time, so I discourage it to others now.
HTH
I would recommend you to look at the
asp.net-mvc sample application
from here which does this much much simpler than automaperEspecially look at the
TinyController
My solution to this was to introduce the concept of Model enrichers, simple classes that "enrich" the model before it is passed to the View():
My AutoMapperViewResult
ExecuteResult
method then looks like:As I'm also using the FormActionResult from Jimmy's presentation I also use the enricher before returning the Failure result. This means that things like select lists are rebinded and keeps things super DRY.
[Update]
I posted an improved solution here that builds on the above.
I haven't gotten to the point (since I saw the talk) when I needed this, but I have a possible solution for this in mind. I think it would work to create an attribute, specifying that this property needs to be loaded. I would start with an abstract class:
Then create specific version for each type you want to load (Locations in your case)
In your
ExecuteResult
ofAutoMappViewResult
you would find all properties withLoadDataAttribute
, callLoadData()
, cast it to type specified in the attribute and assign it to the property.I case you just want to load select lists this way, you can just return
IList<SelectListItem>
instead ofobject
, and save yourself some trouble with casting.Your view model would the obviously use the attribute.