Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:32:19+00:00 2026-05-26T07:32:19+00:00

I have an ASP.NET MVC 3 application and am using Ninject for injecting dependencies

  • 0

I have an ASP.NET MVC 3 application and am using Ninject for injecting dependencies into my classes.

Actions on the Controllers pass ViewModels (that do not contain logic) to the view layer.

When a HTTP form is POSTed MVC 3 creates a ViewModel instance and binds the incoming POST data to the ViewModel’s properties. MVC 3 uses a class called DefaultModelBinder to create the instance and perform the binding.

Most of my ViewModels share a dependency which I do not really want to set from each individual Controller method (DRY principle).

Therefore, I have created a customized 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 have set it to replace the DefaultModelBinder as follows (in Global.asax.cs):

protected void Application_Start() {

    // ...

    ModelBinders.Binders.DefaultBinder = new CustomModelBinder( DependencyResolver.Current );

    // ...

}

By doing this when a Controller method receives a ViewModel, it will be one that was built by Ninject using the binding I sepecified in my NinjectModule. The ViewModel now receives it’s dependencies injected into it’s constructor.

Is this an appropriate use of the Service Locator Pattern?

CLARIFICATION/UPDATES:

  • The application uses Ninject and is integrated into the MVC system as documented on the Ninject Wiki (https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:32:20+00:00Added an answer on May 26, 2026 at 7:32 am

    From what I understand so far is that you have a ViewModel which inherits from ViewModelBase to expose AccountInformation on all Views where a user is logged in.

    Unless I completely misunderstood, this is my point of view:
    The AccountInformation should only be used for displaying purposes. So it should not be a problem when the default ModelBinder does not instantiate it when a post to your action occurs. I encourage you to fetch the AccountInformation again using the information you have eg. Controller.User and your database. A registration/profile page is the only place where you’d want this information to come from POST variables. You could cache this information per user if necessary.

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

    All the logic you’d want to put in a view, goes into the controller.

    So to conclude; There shouldn’t be the need to use a Service Locator in your ModelBinder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I am using asp.net MVC to develop an application that will have ajax interactions.
I'm using Ninject 2 with an ASP.NET MVC web app. All the dependencies are
I have a ASP.NET MVC application using NHibernate and the application runs fine when
This is nuts. I have an ASP.NET MVC application using Windows authentication that, amongst
I have an ASP.Net MVC web application using forms authentication. I am using OpenID
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am
I have an ASP.Net MVC application and I am using StructureMap within MVC to
Background: I have an ASP.NET MVC application that wraps controller actions in a TransactionScope
I have a strange issue with an Asp.NET MVC application. Using Asp.NET MVC 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.