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 4576148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:09:52+00:00 2026-05-21T20:09:52+00:00

Does anyone know if it’s possible to use Ninject to resolve any unresolved abstract

  • 0

Does anyone know if it’s possible to use Ninject to resolve any unresolved abstract dependencies outside of the instantiation process? I’ve just been looking into constructor injection vs property/method/field injection, but it looks to me as though Ninject is still expecting to be the creator of the type using the IKernel.Get<>() method.

Basically, we’re using MVC3 to build our product, and we’ve come up against a situation where we want the default ModelBinder to map form values to an instance of the object, and then be able to call a method on the submitted ViewModel that is dependent on an abstract interface e.g.

public class InviteFriend {
    [Required]
    public string EmailAddress { get; set; }

    public void Execute() {
        var user = IUserRepository.GetUser(this.EmailAddress);

        if (user == null) {
               IUserRepository.SaveInvite(this.EmailAddress);
        }

        MailMessage toSend = new MailMessage(); // Obviously some logic to prepare the body, subject and other mail properties
        SmtpClient.Send(toSend);
    }
}

where the controller action would receive InviteFriend as the method argument. We want Ninject to be able to resolve that IUserRepository dependency, but I can’t quite work out how to since the object itself is instantiated by the MVC ModelBinder rather than Ninject IKernel.Get<>().

Maybe the solution is a Ninject-based ModelBinder, or does that seem a really bad idea?

EDIT TO ADD: After the comments below, I realise that my hastily mocked-up code sample doesn’t really reflect what we’re facing. I’ve updated the code sample to reflect that the logic for InviteFriend.Execute() is more complex than just calling a method on one repository. Potentially, this is logic representing a discrete task that could co-ordinate interactions between multiple different domain objects and multiple repositories. The repositories are defined abstractly, and ideally would be resolved by Ninject.

  • 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-21T20:09:53+00:00Added an answer on May 21, 2026 at 8:09 pm

    Thank you for all your comments, but I’ve subsequently found the information I was looking for.

    The answer is that it is possible to inject dependencies post-instantiation with Ninject. The solution is as follows:

    public class InviteFriend {
        [Inject]
        public IUserRepository UserRepo { get; set; }
    
        [Required]
        public string EmailAddress { get; set; }
    
        public void Execute() {
            var user = UserRepo.GetUser(this.EmailAddress);
    
            if (user == null) {
                   UserRepo.SaveInvite(this.EmailAddress);
            }
    
            MailMessage toSend = new MailMessage(); // Obviously some logic to prepare the body, subject and other mail properties
            SmtpClient.Send(toSend);
        }
    }
    

    With client code then using the Ninject kernel as follows:

    IKernel container = new StandardKernel(new ModuleWithMyBindings());
    container.Inject(instanceOfInviteFriend);
    

    The code itself is a bit more sophisticated than that i.e. I’m not instantiating a new IKernel each time I need it.

    I realise that this is architecturally less pure than some of the suggestions put forward in comments, but in the spirit of YAGNI, this is good enough for now and we can always refactor later on with some of the good suggestions in Daniel’s answer. However, this was a question about the capabilities of Ninject rather than an architectural review question, and this is what I consider the answer to my own question 🙂

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

Sidebar

Related Questions

Does anyone know of any existing packages or libraries that can be used to
Does anyone know of any good tutorials on ADO.NET Entity Framework? There are a
Does anyone know any good chart api for a web application in java? This
Does anyone know if there's any benefit in 'minifying' locally-stored javascript code to squeeze
Does anyone know whether it's possible to stop Visual Studio 2010 from rearranging its
Does anyone know how facebook connect buttons generate the pop-up window that you use
Does anyone know where I can find a tutorial on how to use GStreamer
Does anyone know REST service for weather casting which will be available also outside
Does anyone know, with good experience of mono development, what the equiv, if any,
Does anyone know any sane reason for such bundling decision? Google engineers act wisely

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.