Reputation: 1
I have been been Windsor Castle IoC container for the last 3 years; I am trying to put together a proof of concept using Autofac and is having trouble finding equivalent Windsor's Castle's features in Autofac.
• In Windsor Castle's IWinsorContainer API, there is a Release method that allow immediate release of a component instance. Is there an equivalent method in Autofac container?
• Windsor Castle has a concept of “pooled” lifestyle, please see the link below. Is there an equivalent in Autofac? http://www.castleproject.org/container/documentation/trunk/manual/coretypedocs/Generated_PooledAttribute.html
• Windsor Castle’s DynamicProxy provide a way to do method interception. I found the link below and followed Nicholas’s instruction but I am not able to find the IInterceptor interface in AutofacContrib.DynamicProxy2 or the source code that he mentioned. http://nblumhardt.com/archives/aop-with-autofac-and-dynamicproxy2/
I would appreciate any information you could provide!
Upvotes: 0
Views: 1398
Reputation: 23924
There is no explicit Release in Autofac. Component lifetimes in Autofac are managed by lifetime scopes. The closest you could probably get to explicit release of an object is to make use of the IDisposable support inherent in lifetime scopes.
Say you have a class that implements IDisposable and you register it:
var builder = new ContainerBuilder();
builder.RegisterType<SomeComponent>().As<IMyDependency>().InstancePerLifetimeScope();
var container = builder.Build();
If you create a lifetime scope from which to resolve the component, then when the lifetime scope is disposed, so is your component:
using(var lifetime = container.BeginLifetimeScope())
{
var myThing = lifetime.Resolve<IMyDependency>();
// myThing.Dispose() gets called at the end of the using
}
Alternatively, you may look into owned instances, where you control when IDisposable.Dispose() gets called.
There is no equivalent of the Pooled lifetime in Autofac. You have your choice of...
There's a wiki article about the lifetime scopes available here.
You could probably do some custom coding to get pooling to work involving creation of a custom IComponentLifetime implementation and an extension to IRegistrationBuilder so you could do something like...
builder.RegisterType<Foo>().As<IFoo>().PooledLifetime(32);
...but I don't think it'll be a one-or-two-line thing. If you do end up creating it, consider contributing back to AutofacContrib.
The IInterceptor interface is in Castle.Core.dll - Castle.DynamicProxy.IInterceptor - not in AutofacContrib.DynamicProxy2.
Upvotes: 4