2010-03-12 3 views
0

개인 생성자를 사용하여 Castle을 사용하여 서비스를 해결할 수 있기를 원합니다.모든 서비스에 대해 개인 생성자를 지원하도록 Castle Windsor를 확장하는 가장 간단한 방법은 무엇입니까

이 그에 대한 가상의 "사용 사례"입니다 :

public class SingletonClass : ISingletonClass 
{ 

    private SingletonClass() {...} // Class users cannot create an instance 

    public ISingletonClass Instance 
    { 
     get 
     { 
     // The intention here is to get an instance of this service 
     // it has previously been configured as singleton in the container 
     return Container.Resolve<ISingletonClass>(); 
     } 
    } 
} 

우리는 모든 서비스에 대해 그 개인 생성자를 사용할 수있는 옵션이 제공 할뿐만 아니라 몇 가지 특정 경우에. 따라서 우리는이를위한 간단하고 일반적인 솔루션을 찾고 있습니다.

맞춤 구성 요소 활성화 도구를 사용하여 솔루션을 찾았지 만 기본 구성 요소 활성화 도구의 중요하지 않은 부분을 재정의해야한다는 것을 알게되었습니다. CreateInstance 메서드.

기본 구성 요소의 활성이 코드입니다 :

protected virtual object CreateInstance(CreationContext context, object[] arguments, Type[] signature) 
    { 
     object instance = null; 
     Exception exception; 
     Type implementation = base.Model.Implementation; 
     bool flag = base.Kernel.ProxyFactory.ShouldCreateProxy(base.Model); 
     bool flag2 = true; 
     if (!(flag || !base.Model.Implementation.IsAbstract)) 
     { 
      throw new ComponentRegistrationException(string.Format("Type {0} is abstract.{2} As such, it is not possible to instansiate it as implementation of {1} service", base.Model.Implementation.FullName, base.Model.Service.FullName, Environment.NewLine)); 
     } 
     if (flag) 
     { 
      flag2 = base.Kernel.ProxyFactory.RequiresTargetInstance(base.Kernel, base.Model); 
     } 
     if (flag2) 
     { 
      try 
      { 
       if (this.useFastCreateInstance) 
       { 
        instance = FastCreateInstance(implementation, arguments, signature); 
       } 
       else 
       { 
        instance = ActivatorCreateInstance(implementation, arguments); 
       } 
      } 
      catch (Exception exception1) 
      { 
       exception = exception1; 
       throw new ComponentActivatorException("ComponentActivator: could not instantiate " + base.Model.Implementation.FullName, exception); 
      } 
     } 
     if (flag) 
     { 
      try 
      { 
       instance = base.Kernel.ProxyFactory.Create(base.Kernel, instance, base.Model, context, arguments); 
      } 
      catch (Exception exception2) 
      { 
       exception = exception2; 
       throw new ComponentActivatorException("ComponentActivator: could not proxy " + base.Model.Implementation.FullName, exception); 
      } 
     } 
     return instance; 
    } 

우리가 단지 "ActivatorCreateInstance"또는 "FastCreateInstance"를 오버라이드 (override) 할 수 있다면 훨씬 쉬울 것하지만이 닫혀 있습니다.

잘못된 경로에 있습니까?
일반적인 방법으로이 작업을 수행하는 훨씬 간단한 방법이 있습니까?

당신에게 대단히 감사는 오픈 소스 프로젝트이기 때문에, 당신은 당신이 필요로 변경하고 활성화 등록, 복사하여 자신의 구성 요소 활성제를 작성하고 DefaultComponentActivator 코드를 붙여 넣기 할 수

+2

처음부터 잘못하려고합니다. 그냥 하지마. –

+0

Krzysztof에 동의합니다. 이것은 좋지 않습니다. 근본적인 문제를 설명해 주시면 더 나은 솔루션을 제공 할 수있게 될 것입니다 ... –

+0

BTW에서는 반사판을 사용하여 소스 코드를 볼 필요가 없습니다. 오픈 소스 프로젝트입니다. http://github.com/castleproject /Castle.InversionOfControl/blob/master/src/Castle.Windsor/ComponentActivator/DefaultComponentActivator.cs#L91 –

답변

0


필. 캐슬 스택의 다른 여러 구성 요소들과 함께 몇 번했습니다. 완료되면 패치를 보내십시오.

관련 문제