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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:05:10+00:00 2026-05-20T02:05:10+00:00

Recently I moved to MVC 3 and Ninject 2. In most of the code,

  • 0

Recently I moved to MVC 3 and Ninject 2. In most of the code, I use constructor injection, but there are some places, where I had to use Inject attribute. Ninject 2 registers its own IDepencyResolver interface. I don’t like DependencyResolver class being part of System.Web.Mvc namespace, because its function is not really strictly related to MVC, but now, when it is there, I can do

public SomeClass 
{
    public IUserService UserService { get; set; }

    public SomeClass()
    {
        UserService = DependencyResolver.Current.GetService<IUserService>();

instead of

public SomeClass 
{
    [Inject]
    public IUserService UserService { get; set; }

so I don’t have to reference Ninject namespace in my classes. Should DependencyResolver be used like that?

  • 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-20T02:05:11+00:00Added an answer on May 20, 2026 at 2:05 am

    I use property injection only for dependencies that are not required for the proper working of the class but could add some functionality if the user sets them. Example of such functionality is logging. So you could have a property which represents a logger where the user can supply his own implementation and if he doesn’t the class continues to work normally but it simply doesn’t log.

    For everything else I use constructor injection. This way you indicate to the consumer that this class has a required dependency on some other service.

    So to answer your question about the property injection I would simply have:

    public SomeClass 
    {
        public IUserService UserService { get; set; }
    
        public void SomeMethodWhichDoesntEnforceUserService()
        {
            if (UserService != null)
            {
                // Provide some additional functionality
            }
        }
    }
    

    and if your class cannot function properly without the user service:

    public SomeClass 
    {
        private readonly IUserService _userService;
        public SomeClass(IUserService userService)
        {
            _userService = userService;
        }
    
        public void SomeMethodWhichRequiresTheService()
        {
            _userService.DoSomething();
        }
    }
    

    So in both cases no reference to any DI specifics. That’s what Inversion of Control is all about.

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

Sidebar

Related Questions

We recently moved from SVN to Git, but there's a single legacy branch that
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.
I recently moved from mysql_ functions to PDO, and there is something i can't
I recently moved a friends blog onto his new web hosts but unfortunately the
I recently moved to a project where I noticed there have a specific requirement
I had recently moved a DataList control in to a UserControl and referenced it
I've recently moved host's, whomever owned our ip previously obviously ran some kind of
I recently moved my redmine installation to a new server and for some reason
I recently moved a project on github and I still use this library in
I recently started learning about ASP.Net MVC and its various features MVC_3_MUSIC_STORE + CODE

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.