What is the expected LifeStyle of a Castle Windsor component activator?

好久不见. 提交于 2019-12-25 06:33:08

问题


I'm using Castle Windsor and DynamicProxy to implement persistence Lazy Loading from scratch (I know NHibernate could be an option etc.) I have implemented a custom component activator to always instantiate my business classes as proxies. I found that the default mixin proxies automatically created when using interceptors were not being used when class methods are called from inside the class itself, which was a problem. So I inherited DefaultComponentActivator and overriding CreateInstance() I'm calling CreateClassProxy() to get a proxy that inherits from the business class, which in that respect works fine.

Now I was expecting this 'ProxyComponentActivator' activator of mine to be instantiated by Castle only once, but a new instance is being created for each class type. Is that correct?

Current registration is like this:

public void Install(
   IWindsorContainer container,
   Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) {
    container.Register( 
        Classes
            .FromAssemblyContaining(typeof(OneOfMyBusinessClasses))
            .InNamespace(typeof(OneOfMyBusinessClasses).Namespace)
            .WithService.DefaultInterfaces()
            .Configure(reg => reg.Activator<ProxyComponentActivator>())
            .LifestyleTransient(),
        etc.
    );
);

The activator implementation is the following:

public class ProxyComponentActivator : DefaultComponentActivator
{
    protected Castle.DynamicProxy.ProxyGenerator ProxyGenerator { get; set; }
    protected PersistenceInterceptor PersistenceInterceptor { get; set; }


    public ProxyComponentActivator(ComponentModel model, Castle.MicroKernel.IKernelInternal kernel, ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
        : base(model, kernel, onCreation, onDestruction)
    {
        this.ProxyGenerator = kernel.Resolve<Castle.DynamicProxy.ProxyGenerator>();
        this.PersistenceInterceptor = kernel.Resolve<PersistenceInterceptor>();
    }


    protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments) //, Type[] signature)
    {
        object instance;

        Type implType = this.Model.Implementation;

        ProxyGenerationOptions p = new ProxyGenerationOptions();

        IPersistent ip = new Persistent();
        p.AddMixinInstance(ip);

        try
        {          
            instance = this.ProxyGenerator.CreateClassProxy(implType, null, p, arguments, this.PersistenceInterceptor);                
        }
        catch
        {
            throw new ComponentActivatorException("ComponentActivator: could not proxy " + implType.FullName, Model);
        }

        return instance;
    }
}

I have also tried to register the activator like this, to no avail...

Component.For<ProxyComponentActivator>()
.ImplementedBy<ProxyComponentActivator>()
.LifestyleSingleton()

Thanks in advance for any help, Luis


回答1:


Every component in Windsor will get its own activator instance



来源:https://stackoverflow.com/questions/15006128/what-is-the-expected-lifestyle-of-a-castle-windsor-component-activator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!