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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:02:02+00:00 2026-05-23T09:02:02+00:00

Question: Is there a way to use Ninject to inject dependencies into my MVC

  • 0

Question: Is there a way to use Ninject to inject dependencies into my MVC Models?

First of all, I am new to MVC and DI (and stackoverflow, btw), so I wanted to make sure I’m going down the proper path with this problem. I’ve been Googling the answer for days, but I can’t find a clean explanation… I’m using C#, .NET4, MVC3, Ninject, and VS2010.

Here’s the scenario: I have a new user form where a user can set their login, roles, and set other profile data. When this gets submitted, I need to save the login info to the MembershipProvider, the roles to the RoleProvider, etc… Its gets a little hairy because I am using dual providers (Active Directory / local database) as per my project requirements, so there is a bit of logic in determining which provider to save to, if the user already exists, if they are allowed to self-identify any roles, and so on.

I feel like this logic should go in a .Save() method on my model. However, I then need to inject instances of my providers into my model, and Ninject doesn’t seem to inject models (at least not using the default setup that the NuGet package comes with). Hence my initial question.

I can think of some alternatives…

  1. Maybe the model is a bad place for this logic to begin with and I should abstract the logic into a service? But making a service just for this one save function seems like overkill to me.
  2. I could just eat the bloat in my controller…but I am told that “fat” controllers are a bad practice.
  3. I suppose I could instead inject my providers into my controller, and then pass those dependencies to my model, but that seems a bit wonky.

So… Is there a way to Ninject the model or do I need to re-factor because I am a newbie at this?

  • 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-23T09:02:02+00:00Added an answer on May 23, 2026 at 9:02 am

    I would go with option one. It’s not overkill – adding a class and an interface is just a few lines of code and it keeps your implementation neat, and maybe later you will want to add more methods… update a user?

    Add an interface and class to handle this which your controller uses – ninject will fetch this instance for you. I have written IMembershipAndRoleService as an example.

    public interface IMembershipAndRoleService
    {
        void ProcessNewUser(User newUser);
    }
    
    public class YourController : Controller
    {
        private readonly IMembershipAndRoleService _membershipAndRoleService;
    
        public YourController(IMembershipAndRoleService membershipAndRoleService)
        {
            _membershipAndRoleService = membershipAndRoleService;
        }
    
        [HttpPost]
        public ActionResult NewUserFormAction(NewUserForm newUserForm)
        {
            //process posted form, possibly map / convert it to User then call to save
            _membershipAndRoleService.ProcessNewUser(user);
            //redirect or return view
        }
    }
    

    Lastly make the class which implements IMembershipAndRoleService, this class will use your Membership and Role providers OR other interfaces which do, keeping your logic in this class simplified.

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

Sidebar

Related Questions

I have a very theoretical question: Is there a way to ban the use
Quick question, I was wondering if there is a way to use Solr's API
QUESTION: Re use of .NET Backgroundworker, is there not a way to let exceptions
The real question: Is there a way to clear certain attributes for all components
Well fast question is there any way to use a css class in a
The question: Is there a way I can use a ClassLoader to test both
Short Question: Is there a way to tell CoreData to use COLLATE while creating
Question: Is there a way to use existing objective-c methods to do a full
A similar question was posted as question:is-there-a-way-to-use-multiple-wsit-client-xml.. . I have two different WS-Security enabled
Question: Is there a way to load a lein plugin, say codex or lein-beanstalk,

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.