2014-12-22 3 views
2

나 장식의 체인 StructureMap 3 종속성 그래프를 작성하는 것을 시도하고있다 :Structuremap 3 복수 장식

각 인스턴스 여러 arugments와 생성자를 갖지만, 내부 IGeocoder의 한 인자, 예를 들어

For<OviGeoCoder>().Use<OviGeoCoder>(); 
For<SqlCachingGeocoder>().Use<SqlCachingGeocoder>().Ctor<IGeoCoder>().Is<OviGeoCoder>(); 
For<RedisCachingGeocoder>().Use<RedisCachingGeocoder>().Ctor<IGeoCoder>().Is<SqlCachingGeocoder>(); 
For<IGeoCoder>().Use<RedisCachingGeocoder>(); 

을하지만

양방향 종속 관계 감지

를 얻을 :

public SomeCachingGeocoder(IGeoCoder inner, IFoo somethingElse) 

나는이처럼 그들을 접선입니다!
1) SOAM.Services.IGeoCoder의 인스턴스 (SOAM.Services.Geocoding.RedisCachingGeocoder)
2) 새로운 RedisCachingGeocoder (IDatabase의 기본, IGeoCoder기본값)
3 : 아래의 StructureMap의 스택 트레이스를 확인 .) SOAM.Services.Geocoding.RedisCachingGeocoder
4) SOAM.Services.IGeoCoder (SOAM.Services.Geocoding.RedisCachingGeocoder의 인스턴스)
5) 새로운 HomeController (IGeoCoder
기본, IAlertService기본)
제 516,) SOAM.Web.Controllers.HomeController
7) SOAM.Web.Controllers.HomeController
8) Container.GetInstance 인스턴스 (SOAM.Web.Controllers.HomeController)

어떤 아이디어 어떻게 해결할 수 있을까요?

+0

[여기] (https://stackoverflow.com/a/25136317/264697)와 같이 'DecorateAllWith' 메소드를 사용하지 않는 이유는 무엇입니까? – Steven

+0

특정 데코레이터로 모든 인스턴스를 장식하고 싶지 않습니다. A와 B, B와 C, C와 D 등을 장식하고 싶습니다. DecorateWith도 (볼 수있는 한) 사출을 허용합니다. 인스턴스를 직접 만들어야합니다. –

답변

3

DecorateAllWith은 기본적으로 자동 배선을 허용하고 아주 쉬운 방법으로 장식을 적재 할 수 있습니다 :

For<IGeoCoder>().Use<OviGeoCoder>(); 
For(typeof(IGeoCoder)).DecorateAllWith(typeof(SqlCachingGeocoder)); 
For(typeof(IGeoCoder)).DecorateAllWith(typeof(RedisCachingGeocoder)); 
2

당신이 DecorateAllWith()를 사용할 수없는 이유 다음이 작업을해야하는 경우 :

 var container = new Container(
      c => 
       { 
        c.For<IFoo>().Use<Foo>(); 
        c.For<IGeoCoder>().Add<OviGeoCoder>().Named("default"); 
        c.For<IGeoCoder>() 
         .Add<SqlCachingGeocoder>() 
         .Ctor<IGeoCoder>() 
         .Is(ctx => ctx.GetInstance<IGeoCoder>("default")) 
         .Named("SqlCaching"); 
        c.For<IGeoCoder>() 
         .Use<RedisCachingGeocoder>() 
         .Ctor<IGeoCoder>() 
         .Is(ctx => ctx.GetInstance<IGeoCoder>("SqlCaching")); 
       }); 

차이점을 찾으려면 을 사용하십시오.을 추가 하시겠습니까? Take a look here