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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:50:24+00:00 2026-05-28T07:50:24+00:00

I’ve been reading Mark Seemann’s book Dependency Injection in .NET and his blog post

  • 0

I’ve been reading Mark Seemann’s book Dependency Injection in .NET and his blog post regarding DI Composition Roots.

And I’m in the process of refactoring my ASP.MVC 3 application Container calls out of my controllers and into a compositional root.

In the past I’d resolve types inside my controller like this.

[HttpGet]
public ActionResult Create()
{
    // Don't do this...
    var color = MvcApplication.Container.Resolve<IColor>();                           
    return View(color);
}

After reading Mark’s book I realize this is less then ideal so I’ve setup my MVC application to inject a repository into the controller on app start using a class derived from the IControllerFactory as dicussed in Mark’s book in section 7.2.1.

If I’m not supposed to call an IoC container directly from my ASP.MVC 3 Controller using the Service Locator pattern how and where do I instantiate new types? Each controller is different so I want a generic way of resolving new types.

I could instantiate types directly like this but that would kind of defeat the purpose.

[HttpGet]
public ActionResult Create()
{
   // Don't do this...
   var color = new Color();
   return View(color);
}

Should I be injecting a Generic Factory into my Controller along side the Generic Repository and use it to new create types? Is there a better way?

EDIT

I realize this is similar to another SO question but my entities have dependencies that need to be resolved before passing them on to a view.

  • 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-28T07:50:24+00:00Added an answer on May 28, 2026 at 7:50 am

    You want to create a container and register services in global.asax (including your controllers) when you application starts up. You also want to provide your own implementation for IControllerFactory that uses the container I just described. Inform the application to use your new controller factory by setting the proprty ControllerBuilder.Current.SetControllerFactory.

    More details here: http://bradwilson.typepad.com/blog/2010/07/service-location-pt2-controllers.html

    You don’t want to new up objects in your controller actions, rather, your controllers should take dependencies on services that can provide the objects it needs.

    One approach would be the following

    public interface IColorFactory
    {
      IColor GetColor();
    }
    
    // this implementation is registered with the container
    public class DefaultColorFactory : IColorFactory
    {
      // this implementation is the only one that knows the details of the IColor implementation
      public IColor GetColor() {  return new Color(); } 
    }
    
    public class MyController : ControllerBase
    {
      private readonly IColorFactory _colorFactory;
      public MyController(IColorFactory cf)
      {
        _colorFactory = cf;
      }
    
        [HttpGet]
        public ActionResult Create()
        {
          return View(_colorFactory.GetColor());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been reading up on various injection-type attacks, and it seems like the best way
I been reading pro asp.net mvc 2.0 framework and I am a bit confused
I was reading over my text book Data Structures and Algorithms: By Mark Allen
I been reading a bit about SQL injection and I want to be sure
I've been reading Joe Duffy's book on Concurrent programming. I have kind of an
Ive been reading about trapping .Net errors at page and application level and cant
I've been reading this post Creating a login form in CodeIgniter based on Ion
Been reading up on MitB attacks and some things worry me about this. From
I have been reading through the C++ FAQ and was curious about the friend
I've been reading a little about temporary tables in MySQL but I'm an admitted

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.