2013-05-11 4 views
0

안녕하세요, Ninject IoC 컨테이너를 사용하고 있습니다. 구조체 코드를 ninject로 변환 할 수 없습니다.구조적 맵과 같은 문맥 적 바인딩

이것은 내가 Ninject에 코드를 사용하고

For<IProductCatalogService>().Use<ProductCatalogService>().Named("realProductCatalogService"); 
For<IProductCatalogService>().Use<CachedProductCatalogService>() 
        .Ctor<IProductCatalogService>().Is(p => p.TheInstanceNamed("realProductCatalogService")); 

그리고 바인딩 Structuremap 코드는이

Kernel.Bind<IProductCatalogService>().To<ProductCatalogService>().Named("realProductCatalogService"); 
Kernel.Bind<IProductCatalogService>().To<CachedProductCatalogService>().Named("cachedProductCatalogService"); 

비슷하지만이 작동하지 않습니다.

+0

참조 http://stackoverflow.com/questions/8447037/how-the-binding-are-done-with-decorators-using-ninject –

답변

1

IProductCatalogService의 구현을 CachedProductCatalogService에 삽입하고 IProductCatalogService을 구현 한 다음이 응용 프로그램의 나머지 부분에서이 캐시 된 구현을 기본 구성 요소로 사용하는 것이 좋습니다. 당신이 사용하여 구성 할 수 있습니다 Ninject에 함께

.WhenParentNamed 조건과 같은 바인딩 :

Kernel.Bind<IProductCatalogService>() 
     .To<ProductCatalogService>() 
     .WhenParentNamed("cached"); 

Kernel.Bind<IProductCatalogService>() 
     .To<CachedProductCatalogService>() 
     .Named("cached"); 

을 조건을 해결하기 위해 노력할 것입니다 Ninject에 IProductCatalogService에 대한 요청이있는 경우. 부모 구성 요소 (의 주입을 요청한 경우)가 "cached" (사용자의 경우 CachedProductCatalogService) 인 경우 ninject는 ProductCatalogService을 반환하고 그렇지 않으면 CachedProductCatalogService을 기본값으로 반환합니다.