2013-02-21 1 views
0

Castle Windsor와 DynamicProxy를 사용하여 영속성을 지연없이 구현할 수 있습니다. NHibernate가 옵션 일 수 있다는 것을 알고 있습니다. 저는 항상 비즈니스 클래스를 프록시로 인스턴스화하기 위해 사용자 정의 구성 요소 활성기를 구현했습니다. 클래스 메소드가 클래스 자체에서 호출 될 때 인터셉터를 사용할 때 자동으로 생성되는 기본 mixin 프록시가 사용되지 않는 것을 발견했습니다. 이는 문제였습니다. 그래서 DefaultComponentActivator를 상속하고 CreateInstance()를 재정 의하여 CreateClassProxy()를 호출하여 비즈니스 클래스에서 상속받은 프록시를 얻습니다. 비즈니스 클래스에서 상속 받으면 괜찮습니다.캐슬 윈저 (Castle Windsor) 부품 활성제의 예상 라이프 스타일은 얼마입니까?

이제이 'ProxyComponentActivator'Activator가 Castle에 의해 인스턴스화되지만 각 클래스 유형에 대해 새 인스턴스가 생성 될 것으로 예상했습니다. 그 맞습니까?

현재 등록은 다음과 같이이다 :

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. 
    ); 
); 

활성제의 구현은 다음과 같다 :

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; 
    } 
} 

나는이 같은 활성을 등록하는 것을 시도했다, 아무 소용이 ...

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

사전에 도움을 주셔서 감사합니다. Luis

답변

1

윈저의 모든 구성 요소는 자체 활성 자 인스턴스를 갖습니다.

+0

그래, 잘못하지는 않습니다. 싱글 톤이 될 수없는 이유는 무엇입니까? 재사용 할 수있을 것 같습니다. 감사. –

관련 문제