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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:14:04+00:00 2026-06-06T17:14:04+00:00

I thought of applying Dependency Injection and use Unity in the Project I am

  • 0

I thought of applying Dependency Injection and use Unity in the Project I am working on now. Registered the types in Application_Start event of Global.asax file and kept the Unity Conatiner in Application object global variable. However before resolving the presenter instance I need to pass the current webform’s instance as a constructor parameter to the presenter. I am doing this in OnInit event of the page.

protected override void OnInit(EventArgs e)
{
    IUnityContainer container = (IUnityContainer)
        HttpContext.Current.Application["container"];

    container.RegisterInstance<IAddRoleView>(this, 
        new ExternallyControlledLifetimeManager());

    _presenter = container.Resolve<AddRolePresenter>();
    base.OnInit(e);
}

My questions here are:

  1. What will happen to the AddRoleView instance once the Request is
    served?
  2. Will it be Garbage Collected or will the Unity container always
    keep a reference until application shutdown, as the Unity Conatiner
    instance has been kept in an application object?
  • 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-06T17:14:05+00:00Added an answer on June 6, 2026 at 5:14 pm

    What you are doing will not work. You are registering the page instance as singleton in the container. This will not work. Depending on how Unity is built, it will either throw an exception the second time you call RegisterInstance<IAddRoleView>, cache all registered instances forever, or replace the previous implementation. But even if it replaces the previous registration, you have a concurrency bug in your code, since it is possible for a different page to be injected in the AddRolePreventer that is resolved for this Page instance.

    I understand what you are trying to achieve. Yyou want to resolve the AddRolePresenter, and want to inject the page into the presenter’s constructor. You made both types dependent on each other through their constructor, which causes a cyclic dependency. You need to break this dependency cycle by injecting the page into a property on the presenter. But don’t let Unity inject the page into the constructor; do this manually:

    _presenter = container.Resolve<AddRolePresenter>();
    _presenter.View = this;
    

    Some tips:

    • Prevent doing any registrations after the initialization phase (after your Application_Start ran). Adding registrations after the initialization phase is typically considered bad practice, since it makes the configuration brittle, and hard to maintain.
    • Move this page initialization where you resolve the presenter to the constructor of the page. This allows you to more easily verify whether page’s can be created and do this during starting up your application (or even inside a unit test). Take a look for instance here to see an example of how to do this (it’s documentation of another DI framework, but it will work with Unity as well).
    • Prevent storing the container inside the HttpContext.Application dictionary. This makes it too easy to use the container at wrong places, and couples your application too much with Unity. This prevents you from doing refactorings or change Unity for another framework (what you most definitely be doing in the future). Again take a look at this example. It also shows a better way of integrating your container with Web Forms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought it was a good idea to add some testing to my project,
I thought I was pretty well acquainted with Subversion, but now something I thought
I made a control that inherits directly from ErrorProvider. I thought that applying it
I'm trying to learn TDD by applying it to a simple project of mine.
How do I use the partial dependency caching in Kentico CMS? Here's a very
Thought my range of search options would easily find this. I wish to combine
I thought I had resolved this but I obviously haven't and was hoping someone
I thought the following script will create div element but I got nothing output
I thought I would be smart and create member functions that accepted output iterators.
I thought this would be fairly simple but it turns out not to work

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.