2011-10-23 1 views
0

Castle.Windsor를 IOC 컨테이너로 사용하려면 기존 웹 응용 프로그램을 수정해야합니다. 그것은 원래 StructureMap으로 개발되었습니다.윈저 성에서 어떻게 이것을 할 수 있습니까? (Structure Map에서 마이그레이션)

다음과 같은 문제가 있습니다.

IFoo -> Foo 
IBar -> Bar 

이 잘 container.Resolve<IFoo>() 또는 container.Resolve<IBar>() 작품을 호출 :

내가 인터페이스와 해당 구현의 몇 가지를 등록 말할 수 있습니다. 이는 서비스가 올바르게 등록되었음을 의미합니다.

var test = ObjectFactory.GetInstance<BadRequestErrorHandler>(); 

이이 IFoo 의존성을 해결할 수 :

나는 그런 내가 호출 할 수 있습니다 StructureMap에서 IFoo

public class BadRequestErrorHandler : HttpErrorHandler 
{ 
    // services 
    public BadRequestErrorHandler(IFoo foo) {...} // has dependency on IFoo 
} 

같은 다른 서비스에 종속성이있는 웹 API 클래스가 있습니다.

이제 윈저에서는 작동하지 않습니다.

어떻게 윈저로이 작업을 수행 할 수 있습니까?

감사합니다.

* 나는 그것이 명시 적으로 BadRequestErrorHandler를 등록하여 작동하도록 단지 수 있었다 * 편집 할 수 있습니다. 난 그냥 바라고

container.Register(Component.For<BadRequestErrorHandler>()); 

는 종속성이 등록하는 클래스를 포함하지 않는이를 달성하기 위해 더 나은 방법이있다. 나는 그들의 무리 ...

* 편집 고통을 완화하기 위해 2 **

을 가지고, 나는이 구체적인 유형을 처리하는 특별한 방법을 추가했습니다.

public T GetInstanceWithAutoRegister<T>() 
{ 
    if (container.Kernel.GetHandler(typeof(T)) == null) 
    { 
     container.Register(Component.For<T>()); 
    } 
    return container.Resolve<T>(); 
} 

public object GetInstanceWithAutoRegister(Type pluginType) 
{ 
    if (container.Kernel.GetHandler(pluginType) == null) 
    { 
     container.Register(Component.For(pluginType)); 
    } 
    return container.Resolve(pluginType); 
} 

이상적은 아니지만 explicetly 각 유형을 등록하는 것보다 좋습니다. 누군가가 더 나은 해결책을 가지고 있기를 바랍니다

+0

StructureMap과 비교하여 구체적인 인스턴스를 명시 적으로 등록하지 않고 Castle Windsor에서 확인할 수 없습니다. 컨테이너에 전화해야합니다.등록 '을 선택하십시오 (유형별 또는 일괄 등록 사용). – Steven

답변

1

ILazyComponentLoader을 등록하면 구성 요소를 확인할 수 없을 때 Windsor가 "최후의 수단"이라고 부르는 고리가되어 원하는 것을 얻을 수 있습니다.

public class JustLetWindsorResolveAllConcreteTypes : ILazyComponentLoader 
{ 
    public IRegistration Load(string key, Type service) 
    { 
     return Component.For(service); 
    } 
} 

를 - 그리고 그 다음이 같은 등록해야합니다 :

귀하의 경우, 구현은 아마 다음과 같이 다소 보일 것이다

container.Register(Component.For<ILazyComponentLoader>() 
     .ImplementedBy<JustLetWindsorResolveAllConcreteTypes>()); 

당신은 그것을 in the docs에 대한 자세한 내용을보실 수 있습니다.

+0

@ mookid8000이 작성한 것 외에도 http://docs.castleproject.org/Windsor.FAQ.ashx#Why_cant_Windsor_resolve_concrete_types_without_registering_them_first_11을 살펴보면 처음부터 이렇게하는 것이 좋지 않은 이유를 설명합니다. –

관련 문제