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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:16:47+00:00 2026-06-11T05:16:47+00:00

I have an architecture in which all the default dependencies are automatically injected through

  • 0

I have an architecture in which all the default dependencies are automatically injected through the Initialize and OnActionExecuting. This all works like a charm because, in runtime (not tests), the default controller activator will call these methods passing in the correct concrete objects. Fine.

My problem begins when I have a custom dependency not injectable in the Initialize and OnActionExecuting methods.

Exemple:

public class MyController
{
    private IEmailSender emailSender = null;
}

Well.. in runtime emailSender will be null unless I set it to something else. In this case I would end up with something not so great, like this:

public class MyController
{
    private IEmailSender emailSender = null;
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
         if(this.emailSender == null)
              this.emailSender = new SomeConcreteEmailSender();
    }
}

Ugly. I don’t want to do this “if null, instantiate” thing.

A more sofisticated way to inject IEmailSender would be creating my own ControllerActivator that would only be called in runtime (not tests) and would inject IEmailSender automatically without the need to do this “if”. But I don’t want to create a custom ControllerActivator for each controller that needs injection.

That being said, what is considered standard when it comes to custom dependency injection for ASP.NET controllers?

  • 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-06-11T05:16:48+00:00Added an answer on June 11, 2026 at 5:16 am

    That being said, what is considered standard when it comes to custom
    dependency injection for ASP.NET controllers?

    Constructor injection:

    public class MyController: Controller
    {
        private readonly IEmailSender _emailSender;
    
        public MyController(IEmailSender emailSender)
        {
            _emailSender = emailSender;
        }
    
        ... actions using the _emailSender here ...
    }
    

    And here’s what’s considered as a poor-man’s-dependency-injection (please do not use) for people that do not wish to use an existing dependency injection framework (because for example they work in a company where some technically illiterate morons dictate what frameworks should be used and what not) or are too lazy to write a custom dependency resolver:

    public class MyController: Controller
    {
        private readonly IEmailSender _emailSender;
    
        public MyController(): this(new SomeConcreteEmailSender())
        {
        }
    
        public MyController(IEmailSender emailSender)
        {
            _emailSender = emailSender;
        }
    
        ... actions using the _emailSender here ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple architecture at the moment which looks a little something like
Please have a look: So basically I have a tree like architecture which is
I am developing an application in C# WPF which will have Client-Server architecture (Client
I have an architecture scenario and I would like to discuss to get your
I have an architecture similar to this: <div id=container> <div>stuff here</div> <div>stuff here</div> <div>stuff
All the examples I've seen regarding a RESTful architecture have dealt with a single
I have an assignment for my architecture class which to implement a 31 backgammon
I have a table in a DB (Postgres based), which acts like a superclass
I have following architecture in my application. Client (GWT) <--calls--> Servlet <--calls--> Service <--calls-->
I have an MVC architecture, but when I try to make another TabPanel insite

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.