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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:56:38+00:00 2026-05-15T13:56:38+00:00

I’m writing an IoC container for my own learning/growth. Normally I would write something

  • 0

I’m writing an IoC container for my own learning/growth. Normally I would write something like the following:

using(DisposableObject dispObj = new DisposableObject())
{
    UserRepository users = new UserRepository(dispObj);

    // Do stuff with user.
}

Would turn to:

using(IDisposableObject dispObj = Container.Resolve<IDisposableObject>())
{
    IUserRepository users = Container.Resolve<IUserRepository>();

    // Do stuff with user.
}

How can I abstract the DisposableObject so that it is the only instanced used in the using scope when using an IoC? I’m trying to figure out how Autofac does it but I’m not totally sure.

EDIT: When an object is instantiated with the using scope, all calls to resolve that type (in this case IDisposableObject) should return the scoped variable, and not a new instance. It’s also important that after the using statement, and another Resolve<IDisposableObject> is called, it returns a new instance.

  • 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-15T13:56:39+00:00Added an answer on May 15, 2026 at 1:56 pm

    The thing to remember when using a disposable object in this fashion, is that the reference in the container would also be disposed, so ideally when you return an instance through Resolve<> it needs to return a new instance each time.

    What you would need to do, is when registering types with your container, allow it to specify a behaviour, either Shared (Singleton), or NonShared, e.g.:

    Container.RegisterType<IDisposableObject>(CreationPolicy.NonShared);
    
    using (var disposable = Container.Resolve<IDisposableObject>()) {
    
    }
    

    The above would work for NonShared instances, as a new instance is created each time, so we can safely dispose of it. If you tried the above with a CreationPolicy = Shared, the Singleton would be disposed of, so future access will likely result in an ObjectDisposedException.

    By building this behaviour in, you can create Singleton instances by passing a CreationPolicy = Shared, e.g.:

    Container.RegisterType<IUserRepository>(CreationPolicy.Shared);
    
    using (var disposable = Container.Resolve<IDisposableObject>()) {
        var userRepository = Container.Resolve<IUserRepository>();
        // only one instance of user repository is created and persisted by the container.
    }
    

    Hope that helps?

    This terminology might be familiar if you have previously used MEF.

    EDIT: So, my understanding is that you want to do something like:

    using (var repository = Container.Resolve<IUserRepository>())
    {
      var other = Container.Resolve<IUserRepository>();
      // should resolve to the same instance.
    }
    

    You need to find some way of monitoring a disposable object in the container. Perhaps introduce an additional creation policy, SharedScope, e.g:

    Container.Register<IUserRepository, UserRepository>(CreationPolicy.SharedScope);
    

    Now, when you resolve the type using the container, you’d need to figure out the CreationPolicy of the item. If the item is SharedScope, and it hasn’t been created, create an instance of it and return.

    If you resolve the instance and it has already been created, return the existing instance.

    When Dispose is called on that item, you need some way to callback to the container to remove the instance.

    EDIT TWO:

    Well, there isn’t an easy way of figuring that out. The only way I can think is that you introduce another interface:

    public interface IMonitoredDisposable : IDisposable
    {
      bool IsDisposed { get; set; }
    }
    

    When an object is disposed, make sure it sets the IsDisposed property. Could you then monitor that property from your container?

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.