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

  • Home
  • SEARCH
  • 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 833867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:35:10+00:00 2026-05-15T04:35:10+00:00

we want to use Unity for IOC. All i’ve seen is the implementation that

  • 0

we want to use Unity for IOC.
All i’ve seen is the implementation that there is one global static service (let’s call it the the IOCService) which holds a reference to the Unity container, which registers all interface/class combinations and every class asks that object: give me an implementation for Ithis or IThat.

Frequently i see a response that this pattern is not good because it leads to a dependency from ALL classes to the IOCService (not to the Unity container because it is only known inside the IOCService).

But what i don’t see often, is: what is the alternative way?

Michel

EDIT: found out that the global static service is called the service locator, added that to the title.

  • 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-15T04:35:11+00:00Added an answer on May 15, 2026 at 4:35 am

    The alternative is to have a single instance of your container at the highest application level only, then use that container to resolve every object instance you need to create in that layer.

    For example, the main method of most executables just looks like this (minus exception handling):

    private static void main(string[] args) {
    
         Container container = new Container();
    
         // Configure the container - by hand or via file
    
         IProgramLogic logic = container.Resolve<IProgramLogic>();
    
         logic.Run();
    }
    

    Your program (represented here by the IProgramLogic instance) doesn’t have to know anything about your container, because container.Resolve will create all its dependencies – and its dependencies’ dependencies, on down to leaf classes with no dependencies of their own.


    ASP.NET is a harder case, because web forms doesn’t support constructor injection. I typically use Model-View-Presenter in my web forms applications, so my Page classes really only have one dependency each – on their presenter. I don’t unit test them (everything interesting and testable is in my presenters, which I do test), and I don’t ever substitute presenters. So I don’t fight the framework – I just expose a container property on my HttpApplication class (in global.asax.cs) and use it directly from my Page files:

    protected void Page_Load(object sender, EventArgs args) {
        ICustomerPresenter presenter = Global.Container.Resolve<ICustomerPresenter>();
        presenter.Load();
    }
    

    That’s service locator of course – though the Page classes are the only thing coupled to the locator: your presenter and all of its dependencies are still fully decoupled from your IoC container implementation.

    If you have a lot of dependencies in your Page files (that is, if you do not use Model-View-Presenter), or if it’s important to you to decouple your Page classes from your Global application class, you should try to find a framework that integrates into the web forms request pipeline and use property injection (as suggested by Nicholas in the comments below) – or write your own IHttpModule and perform the property injection yourself.

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

Sidebar

Related Questions

I want to use Unity interception for an interface without an implementation, so that
Is there a framework or a code tutorial that shows how one can use
I want use a single php file to handle all of my voting requests.
i want use some data from a website with web service. i have a
I want to use Unity resolve IService to two different implementations, to make use
I'm working on a demo that makes use of Spring.NET IoC capability in ASP.NET
I want to use unity for a polymorphic event aggregation/handling where handlers are registered
I am trying to make use of Unity Framework with Entity Framework. Let me
I am new to IoC and I am playing with Unity. Let' say you
For a unit test I want to use the Range attribute from NUnit to

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.