typed factory facility not releasing transient factories?

180 views
Skip to first unread message

bling

unread,
Nov 6, 2012, 5:11:13 PM11/6/12
to castle-pro...@googlegroups.com
public interface IFactory : IDisposable
{
}

[Test]
public void DisposedFactory_Should_Not_Be_Tracked()
{
var c = new WindsorContainer();
c.AddFacility<TypedFactoryFacility>()
.Register(Component.For<IFactory>().AsFactory().LifestyleTransient());

var factory = c.Resolve<IFactory>();
Assert.IsTrue(c.Kernel.ReleasePolicy.HasTrack(factory));
factory.Dispose();
Assert.IsFalse(c.Kernel.ReleasePolicy.HasTrack(factory));
}

this behavior is surprising to me....  short of writing an interceptor, is there another way i can release the factory without referencing the container?  thanks.

Krzysztof Kozmic

unread,
Nov 6, 2012, 5:17:40 PM11/6/12
to castle-pro...@googlegroups.com
disposing of the factory releases the objects the factory was tracking.
The factory itself is released as any other object.

Why is that surprising?

What is your actual scenario?

-- 
Krzysztof Kozmic

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/dw_fOgOqghIJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

bling

unread,
Nov 6, 2012, 5:23:36 PM11/6/12
to castle-pro...@googlegroups.com
yes, if the factory resolves Foo, then after calling factory.Dispose(), Foo will get disposed.  my problem is that the factory itself doesn't get disposed, despite calling Dispose.

my scenario is "give me a transient factory which can create a certain set of components".  so i'd have something like:

   public interface IFactory<T> : IDisposable { T[] Get(); }

and then i would inject into something else like this:

   public Foo(Func<IFactory<Bar>> barFactory) { }

and when i want to use the code, it'd be something like this:

  using (var factory = this.barFactory()) {
    var bars = factory.Get();
    // do stuff
  }

despite wrapping the factory is a using block, Windsor is still tracking it.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.

bling

unread,
Nov 7, 2012, 12:30:32 PM11/7/12
to castle-pro...@googlegroups.com
any additional thoughts on this?  should i file a bug?  thanks.


On Tuesday, November 6, 2012 5:17:48 PM UTC-5, Krzysztof Koźmic wrote:
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.

Krzysztof Kozmic

unread,
Nov 7, 2012, 3:46:29 PM11/7/12
to castle-pro...@googlegroups.com
yes, two thoughts.

1. The pattern you're using looks very odd. Why would you be creating the factory in a using block?
That seems to me like over complication.
2. That's how it was built to work, so I wouldn't call it a bug. You can add another interceptor to the factory that will release it on dispose.
However My recommendation would be to review your design first.

HTH
-- 
Krzysztof Kozmic


To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.

bling

unread,
Nov 7, 2012, 6:21:03 PM11/7/12
to castle-pro...@googlegroups.com
1) how else would you release the factory?  should i inject a singleton ITypedFactoryFactory?
2) for the time being i've refactored my design to inject a singleton typed factory, since that was less code then writing an interceptor.

cheers,
bling

bling

unread,
Nov 8, 2012, 3:42:50 PM11/8/12
to castle-pro...@googlegroups.com
i've been thinking about it a while and i'm still a bit confused why you feel that transient factories are a bad idea.  can you elaborate a bit on that?  if disposing the factory also automatically got released by the container, then you'd be able to create scoped lifestyles within the code very easily without referencing the container.

thanks.
Reply all
Reply to author
Forward
0 new messages