In the answer "nekno" (answered September 30 at 22:19) there are two alternatives to ViewModel that either return "IEnumerable <SelectListItem>", or 'IEnumerable <int? > '. Both of these alternatives use the repository, but without creating it, so I would like to expand the sample code a bit and choose the second option, that is, the class with the property "IEnumerable <int?>",
using Microsoft.Practices.ServiceLocation; // ServiceLocator , http://commonservicelocator.codeplex.com/ using MyOwnRepositoryNameSpace; // IRepository public class EditViewModel { public int? FooType { get; set; } public IEnumerable<int?> FooTypes { get { return Repository.GetFooTypes(); } } private IRepository Repository { get { return ServiceLocator.Current.GetInstance<IRepository>(); } } }
The above kind of code with "Dependecy Lookup" now uses a dependency on the third part library, in this case, the Common Service locator library.
My question is how to replace the above code with "Injection of Dependency"? The ViewModel model itself would indeed be very trivial to implement, like this:
using MyOwnRepositoryNameSpace; // IRepository public class EditViewModel { private readonly IRepository _repository; public EditViewModel(IRepository repository) { _repository = repository; } public int? FooType { get; set; } public IEnumerable<int?> FooTypes { get { return _repository.GetFooTypes(); } } }
The problem is how to force the ViewModel to be injected into the implementation when the ASP.NET MVC structure creates an instance of “EditViewModel” and sends it as an argument to the action method, such as the signature of the tihs method:
public ActionResult Edit(int id, EditViewModel model) {
The official MVC guide does not seem to give anything good as far as I can see. In the section “Processing Changes” (methods “Public Action ActionResult Edit (...)”) on the following pages, they duplicate the creation of parameters in the same way as in the poster of this stack question that you are currently reading.
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5
http://mvcmusicstore.codeplex.com/SourceControl/changeset/view/d9f25c5263ed#MvcMusicStore%2fControllers%2fStoreManagerController.cs
If there is a decision on how to make a model for introducing a presentation model with your data retrievers (for example, a repository), I believe that some implementation of "IModelBinderProvider" or "IModelBinder" can be used, but I experimented with them without real success ...
So, can anyone provide a link to a complete working example with ASP.NET MVC 3 code that allows you to embed a data retriever in the view model constructor, which creates an instance of the frame and will send as an argument to the action method?
Update 2012-01-01 : For those who are interested in resolving this specific issue of introducing a ViewModel instance constructor, when the environment instantiates an object and sends it as an argument to the MVC Action Method parameter, I created a new question with a more specific subject and, therefore, I hope it is more likely that someone with the solution will find it and post a good answer: Embedding the constructor of the View Model instance used as the parameter of the action method