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

  • SEARCH
  • Home
  • 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 7440637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:52:56+00:00 2026-05-29T10:52:56+00:00

I’m using NInject with NInject.Web.Mvc. To start with, I’ve created a simple test project

  • 0

I’m using NInject with NInject.Web.Mvc.

To start with, I’ve created a simple test project in which I want an instance of IPostRepository to be shared between a controller and a custom model binder during the same web request. In my real project, I need this because I’m getting IEntityChangeTracker problems where I effectively have two repositories accessing the same object graph. So to keep my test project simple, I’m just trying to share a dummy repository.

The problem I’m having is that it works on the first request and that’s it. The relevant code is below.

NInjectModule:

public class PostRepositoryModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<IPostRepository>().To<PostRepository>().InRequestScope();
    }
}

CustomModelBinder:

public class CustomModelBinder : DefaultModelBinder
{
    [Inject]
    public IPostRepository repository { get; set; }

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        repository.Add("Model binder...");

        return base.BindModel(controllerContext, bindingContext);
    }
}

public class HomeController : Controller
{
    private IPostRepository repository;

    public HomeController(IPostRepository repository)
    {
        this.repository = repository;
    }

    public ActionResult Index(string whatever)
    {
        repository.Add("Action...");

        return View(repository.GetList());
    }
}

Global.asax:

protected override void OnApplicationStarted()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    ModelBinders.Binders.Add(typeof(string), kernel.Get<CustomModelBinder>());
}

Doing it this way is actually creating 2 separate instances of IPostRepository rather than the shared instance. There’s something here that I’m missing with regards to injecting a dependency into my model binder. My code above is based on the first setup method described in the NInject.Web.Mvc wiki but I have tried both.

When I did use the second method, IPostRepository would be shared only for the very first web request, after which it would default to not sharing the instance. However, when I did get that working, I was using the default DependencyResolver as I couldn’t for the life of me figure out how to do the same with NInject (being as the kernel is tucked away in the NInjectMVC3 class). I did that like so:

ModelBinders.Binders.Add(typeof(string),
    DependencyResolver.Current.GetService<CustomModelBinder>());

I suspect the reason this worked the first time only is because this isn’t resolving it via NInject, so the lifecycle is really being handled by MVC directly (although that means I have no idea how it’s resolving the dependency).

So how do I go about properly registering my model binder and getting NInject to inject the dependency?

  • 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-29T10:52:57+00:00Added an answer on May 29, 2026 at 10:52 am

    I eventually managed to solve it with a factory as suggested. However, I just could not figure out how to accomplish this with Ninject.Extensions.Factory which is what I would’ve preferred. Here is what I ended up with:

    The factory interface:

    public interface IPostRepositoryFactory
    {
        IPostRepository CreatePostRepository();
    }
    

    The factory implementation:

    public class PostRepositoryFactory : IPostRepositoryFactory
    {
        private readonly string key = "PostRepository";
    
        public IPostRepository CreatePostRepository()
        {
            IPostRepository repository;
    
            if (HttpContext.Current.Items[key] == null)
            {
                repository = new PostRepository();
                HttpContext.Current.Items.Add(key, repository);
            }
            else
            {
                repository = HttpContext.Current.Items[key] as PostRepository;
            }
    
            return repository;
        }
    }
    

    The Ninject module for the factory:

    public class PostRepositoryFactoryModule : NinjectModule
    {
        public override void Load()
        {
            this.Bind<IPostRepositoryFactory>().To<PostRepositoryFactory>();
        }
    }
    

    The custom model binder:

    public class CustomModelBinder : DefaultModelBinder
    {
        private IPostRepositoryFactory factory;
    
        public CustomModelBinder(IPostRepositoryFactory factory)
        {
            this.factory = factory;
        }
    
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            IPostRepository repository = factory.CreatePostRepository();
    
            repository.Add("Model binder");
    
            return base.BindModel(controllerContext, bindingContext);
        }
    }
    

    The controller:

    public class HomeController : Controller
    {
        private IPostRepository repository;
    
        public HomeController(IPostRepositoryFactory factory)
        {
            this.repository = factory.CreatePostRepository();
        }
    
        public ActionResult Index(string whatever)
        {
            repository.Add("Action method");
    
            return View(repository.GetList());
        }
    }
    

    Global.asax to wire up the custom model binder:

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        ModelBinders.Binders.Add(typeof(string), kernel.Get<CustomModelBinder>());
    }
    

    Which in my view, gave me the desired output of:

    Model binder
    Action method

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Specifically, suppose I start with the string string =hello \'i am \' me And

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.