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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:59:05+00:00 2026-05-26T12:59:05+00:00

I know this has been discussed ad nauseum…but I have an issue with the

  • 0

I know this has been discussed ad nauseum…but I have an issue with the way Windsor is tracking Transient IDisposable objects.

I understand the benefits of letting Windsor manage my IDiposables…but I don’t like it.

What happens if I want to wrap my component in a using block? The coder would make the assumption the resource would get cleaned up at the end of the using block, right? Wrong – Dispose would be called, but Windsor would hold onto the instance until explicitly released. This is all well and fine for me, since I know what I’m doing…but what about another developer who’s coding a class and wants to use an IDisposable the way every other IDisposable is usually used – in a using block?

using(factory.CreateInstance()) 
{
   ....  
}

looks much clearer to me than:

MyDisposable instance;
try
{
    instance = factory.GetInstance();
}
finally 
{
    factory.Release(instance);
}

In order to truly dispose my instances and have them eligible for GC, I need to reference the WindsorContainer or use a typed factory that exposes a release method. That means the only acceptable way of using IDisposable components is to use a typed factory. This is not good, in my opinion…what if someone adds the IDisposable interface to an existing component? Every single place that expects the component to be injected will need to change. That’s really bad in my opinion. (Granted, in a non DI scenario, it would need to change to call Dispose also…but with Windsor every place will need to change to use a typed factory, which is a much larger change).

Ok, fair enough, I can use a custom ReleasePolicy right? How about this?

public class CustomComponentsReleasePolicy : AllComponentsReleasePolicy
{
    public override void Track(object instance, Burden burden)
    {
        if (burden.Model.LifestyleType == LifestyleType.Pooled) 
            base.Track(instance, burden);
    }
}

Ok, great, my IDisposable Transient components will be GC’d now.

What if I want to use a TypedFactory so my class can produce many instances of a type?

public interface IMyFactory 
{
    MyDisposable GetInstance();
    void Release(MyDisposable instance);
}

[Singleton]
public class TestClass
{
    public TestClass(IMyFactory factory) { }
}

Ok, well, for one, calling Release on factory will do nothing to call Dispose() on MyDisposable, since MyDisposable isn’t tracked….

How can I overcome these difficulties?

Thanks.

  • 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-26T12:59:05+00:00Added an answer on May 26, 2026 at 12:59 pm

    First of all, how do you know the decommision concerns associated with an object which you did not create? You do not have control over the creation of the object because you did not create it yourself (the factory did this for you). When you combine ioc resolving with consumer disposing (calling .Dispose instead of factory.Release) you are introducing the requirement that you object knows how it was created, yet it did not create itself. Consider the following example:

    “Component” is something you resolve through the container, but you want to dispose of yourself

    public class Component : IDisposable
    {
        private readonly IAmSomething _something;
    
        public Component(IAmSomething something)
        {
            _something = something;
        }
    
        public void Dispose()
        {
            // Problem 1: the component doesnt know that an implementation of IAmSomething might be disposable
            // Problem 2: the component did not create "something" so it does not have the right to dispose it
            // Problem 3: What if an implementation of "something" has a depenency on a disposable instance deep down in its object graph?
    
            // this is just bad..
            IDisposable disposable = _something as IDisposable;
    
            if (disposable != null)
                disposable.Dispose();
    
        }
    }
    
    public interface IAmSomething
    {
    
    }
    
    public class SomethingA : IAmSomething
    {
    
    }
    
    public class SomethingB : IAmSomething, IDisposable 
    {
        public void Dispose()
        {
        }
    }
    

    As shown above decommission can be complex and I simple don’t see how I can handle this gracefully myself, especially when Windsor does this for me. If your codebase is littered with the service-locator anti-pattern (http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/) I can see how this becomes an issue (I am not saying that you code is), but then you really how lot bigger problems.

    using(factory.CreateInstance()) 
    {
       ....  
    }
    

    looks much clearer to me than: …

    Well the using statement is a convention, there no compile time error if you omit it, so from my point of view the try/finally with release is just another convention, although a bit more verbose. You could for example shorten the try/finally by creating a helper, such as:

    [TestFixture]
    public class SomeCastleTests
    {
        [Test]
        public void Example()
        {
            var container = new WindsorContainer();
    
            // you would of course use a typed factory instead in real word
    
            using (var usage = new ComponentUsage<IAmSomething>(container.Resolve<IAmSomething>, container.Release))
            {
                // use..
                usage.Component
            }
        }
    }
    
    public class ComponentUsage<T> : IDisposable where T : class
    {
        private Func<T> _create;
        private Action<T> _release;
    
        private T _instance;
    
        public ComponentUsage(Func<T> create, Action<T> release)
        {
            _create = create;
            _release = release;
        }
    
        public T Component
        {
            get
            {
                if (_instance == null)
                    _instance = _create();
    
                return _instance;
            }
        }
    
        public void Dispose()
        {
            if (_instance != null)
            {
                _release(_instance);
                _instance = null;
            }
        }
    }
    

    This is not good, in my opinion…what if someone adds the IDisposable
    interface to an existing component? Every single place that expects
    the component to be injected will need to change.

    I don’t understand this statement. How a component is released and when you need it are to different concerns, if a component is provided as a constructor dependency, adding IDisposable doesn’t change anything. The class which gets the dependency through the constructor did not create it and is therefore not responsible for releasing it.

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

Sidebar

Related Questions

I know this has been discussed ad-nauseum but I just don't get some of
I know this issue has been discussed numerous times, but I think this is
I know this has been discussed a number of times but I still have
I know this topic has been discussed a lot , but I have a
I know this issue has been discussed already ( here for example), but It's
I know this subject has been discussed before but still have problem with refresh
I know this has been discussed a lot of times but is there any
I know this topic has been discussed but I think it has some differences.
I know this is a question that has been discussed over and over, but
I know this topic has been discussed and killed over and over again, but

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.