What is the expected LifeStyle of a Castle Windsor

2019-08-31 09:37发布

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条回答
Bombasti
2楼-- · 2019-08-31 09:37

Every component in Windsor will get its own activator instance

查看更多
登录 后发表回答