Neil
Neil

Reputation: 2688

Castle.Windsor - understanding transient lifestyle

New to Castle.Windsor and just wanted to check something.

I've been reading about transient lifestyles and "releasing what you resolve" - I've created a Resource provider for my ASP.NET MVC3 application and am resolving it within my new resource provider factory (which is the "hub" for resource access in ASP.NET):

IContainerAccessor accessor = HttpContext.Current.ApplicationInstance as IContainerAccessor;            
IResourceProvider provider = accessor.Container.Resolve<IResourceProvider>(new Arguments(new { resourceName = resourceName }));

LoggerService.Information(String.Format("Tracking? {0}", accessor.Container.Kernel.ReleasePolicy.HasTrack(provider)));

The IResourceProvider is transient so I can pass a different resourceName to the constructor (so different labels can be returned from different resource files).

I was concerned I'd need to explicitly call Release on these IResourceProvider objects but my HasTrack check always returns false - so I assume this is a good thing?

The container isn't tracking this object so the GC will (eventually clean this object up).

When then would the container track this object - if it had creation commission concerns?

Upvotes: 2

Views: 4269

Answers (1)

Marwijn
Marwijn

Reputation: 1681

You only release components that you explicity ask for. There are 2 ways of explicitly obtaining a component:

a) Resolve, which you used above. In general it is a good practise to only have a single resolve call in your application. So you probably want to look into the TypedFactoryFacility.

b) Use of a factory. In this case you would use the TypeFactoryFacility. If you obtain a component from a factory it and it's dependencies will be released when:

  • You release it explicitly by calling a "destroy" method on your factory.
  • Or the factory it self is released.

If you didn't create a component explicitly you should never have to release it.

Upvotes: 3

Related Questions