Is this a suitable use of the Locator service pattern in my MVC 3 ModelBinder?

I have an ASP.NET MVC 3 application and I use Ninject to inject dependencies into my classes.

Actions on controllers pass ViewModels (which do not contain logic) to the presentation layer.

When the HTTP POSTED MVC 3 form creates an instance of the ViewModel and associates the incoming POST data with the ViewModel properties. MVC 3 uses the DefaultModelBinder class to instantiate and bind.

Most of my ViewModels models use a dependency that I really don't want to install from each individual controller method (DRY principle).

Therefore, I created an individual subclass of DefaultModelBinder as follows:

using System; using System.Web.Mvc; namespace Application.Classes { public sealed class CustomModelBinder : DefaultModelBinder { private readonly IDependencyResolver DependencyResolver; public CustomModelBinder( IDependencyResolver dependencyResolver ) { DependencyResolver = dependencyResolver; } protected override object CreateModel( ControllerContext controllerContext , ModelBindingContext modelBindingContext , Type modelType ) { return DependencyResolver.GetService( modelType ); } } } 

And I installed it to replace DefaultModelBinder as follows (in Global.asax.cs):

 protected void Application_Start() { // ... ModelBinders.Binders.DefaultBinder = new CustomModelBinder( DependencyResolver.Current ); // ... } 

By doing this, when the Controller method gets a ViewModel, it will be the one that Ninject created using the I binding specified in my NinjectModule. ViewModel now gets the dependencies nested in the constructor.

Is this a suitable use of a locator service template?

clarifications / UPDATE:

+4
source share
3 answers

From what I understand so far, is that you have a ViewModel that inherits from ViewModelBase to give account information about all the views where the user is logged in.

If I do not completely understand, this is my point of view:
Credentials should be used for display purposes only. Thus, this should not be a problem if, by default, ModelBinder does not instantiate when a message appears about your action. I urge you to get your credentials again using the information you have. Controller.User and your database. The registration / profile page is the only place you want this information to come from POST variables. You can cache this information for each user, if necessary.

As I said in the comments, ViewModels should be as stupid as possible. They should contain only properties with their types and metadata regarding validation, etc.

All the logic that you want to put in the view goes into the controller.

So, to conclude; Your ModelBinder should not need to use a Locator.

+5
source

Models should not have dependencies. These are just dumb data containers without functionality. Instead, use filters for cross cutting tasks.

+2
source

Do you consider using Ninject for MVC3? You can download it from here.

Then in your global.asax it inherits from NinjectHttpApplication:

 public class MvcApplication : NinjectHttpApplication { 

Create a kernel override method:

 protected override IKernel CreateKernel() { return new StandardKernel(new NinjectRepositoryModule(), new NinjectAggregateServiceModule()); } 

NijectRepositoryModule is where I bind my interfaces to specific implementations:

 public class NinjectRepositoryModule: NinjectModule { public override void Load() { Bind<IContactsRepository>().To<EFContactRepository>(); } } 
0
source

All Articles