2017-03-18 14 views
1

내가 같은 인터페이스를 가지고 상상하는 아래 ICacheManager <의 모든 상속>다른 유형의 구성으로 ICacheManager <>를 동시에 사용할 수 있습니까?

public interface ICacheManagerRuntime<T> : ICacheManager<T> 
public interface ICacheManagerRedis<T> : ICacheManager<T> 
public interface ICacheManagerRedisWithRuntime<T> : ICacheManager<T> 

내가 좋아하는 캐시 클래스의 implemantation에 ICacheManager {CacheType} 인터페이스를 주입 할 : 통일로

CacheRuntime, CacheRedis, CacheRedisWithRuntime 

내가 원하는 다음과 같이 주사하십시오.

container.RegisterType<ICacheManagerRuntime<object>>(
       new ContainerControlledLifetimeManager(), 
       new InjectionFactory((c, t, n) => 
       { 
        return CacheFactory.Build... // Return CacheManager just with RuntimeCacheHandle 
       }))); 


container.RegisterType<ICacheManagerRedis<object>>(
       new ContainerControlledLifetimeManager(), 
       new InjectionFactory((c, t, n) => 
       { 
        return CacheFactory.Build... // Return CacheManager just with RedisCacheHandle 
       }))); 


container.RegisterType<ICacheManagerRedisWithRuntime<object>>(
       new ContainerControlledLifetimeManager(), 
       { 
        return CacheFactory.Build... // Return CacheManager just with RuntimeCacheHandleWithRedisBackPlane 
       }))); 

나는이 모든 예외를 겪고 있습니다.

An unhandled exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll 

Additional information: Resolution of the dependency failed, type = "Solid.Play.Business.Interfaces.IProductService", name = "(none)". 

Exception occurred while: Resolving parameter "cache" of constructor Solid.Play.Cache.Caches.CacheRuntime(Solid.Play.Cache.Interfaces.ICacheManagerRuntime`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] cache). 

Exception is: InvalidCastException - Unable to cast object of type 'CacheManager.Core.BaseCacheManager`1[System.Object]' to type 'Solid.Play.Cache.Interfaces.ICacheManagerRuntime`1[System.Object]'. 

----------------------------------------------- 

At the time of the exception, the container was: 



    Resolving Solid.Play.Business.Services.ProductService,(none) (mapped from Solid.Play.Business.Interfaces.IProductService, (none)) 

    Resolving Solid.Play.Cache.Interception.CachingInterceptorBehavior,(none) 

    Resolving parameter "cache" of constructor Solid.Play.Cache.Interception.CachingInterceptorBehavior(Solid.Play.Cache.Interfaces.ICacheSolid cache) 

     Resolving Solid.Play.Cache.Caches.CacheSolid,(none) (mapped from Solid.Play.Cache.Interfaces.ICacheSolid, (none)) 

     Resolving parameter "cacheRuntime" of constructor Solid.Play.Cache.Caches.CacheSolid(Solid.Play.Cache.Interfaces.ICacheRuntime cacheRuntime, Solid.Play.Cache.Interfaces.ICacheRedis cacheRedis, Solid.Play.Cache.Interfaces.ICacheRedisWithRuntime cacheRedisWithRuntime) 

     Resolving Solid.Play.Cache.Caches.CacheRuntime,(none) (mapped from Solid.Play.Cache.Interfaces.ICacheRuntime, (none)) 

     Resolving parameter "cache" of constructor Solid.Play.Cache.Caches.CacheRuntime(Solid.Play.Cache.Interfaces.ICacheManagerRuntime`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] cache) 

답변

1

인스턴스가 구현하지 않은 인터페이스로 캐스팅하려고하기 때문에 캐스트가 작동하지 않습니다. 는이 같은 모습을 시도하고있는 무슨, 간체 :

public interface IBase 
{ 
} 

public interface ISub : IBase { } 

public class BaseClass : IBase 
{ 
} 

var sub = (ISub)new BaseClass(); 

같은 인터페이스의 인스턴스의 다른 종류를 주입 할 경우, 유니티 DI 프레임 워크라는 주사를 통해 것을 할 수있는 방법을 제공합니다.

예 :

 container.RegisterType<ICacheManager<object>>("runtimeCache", 
     new ContainerControlledLifetimeManager(), 
     new InjectionFactory((c, t, n) => 
     { 
      return CacheFactory.Build<object>(s => 
      { 
       s.WithSystemRuntimeCacheHandle("cache.runtime"); 
      }); 
     })); 

     container.RegisterType<ICacheManager<object>>("redisCache", 
     new ContainerControlledLifetimeManager(), 
     new InjectionFactory((c, t, n) => 
     { 
      return CacheFactory.Build<object>(s => 
      { 
       s.WithRedisConfiguration("cache.redis", config => 
       { 
        config 
        .WithAllowAdmin() 
        .WithDatabase(0) 
        .WithEndpoint("localhost", 6379); 
       }) 
       .WithRedisCacheHandle("cache.redis"); 
      }); 
     })); 

은 첫 번째를 해결하려면, 당신은

var runtimeCache = container.Resolve<ICacheManager<object>>("runtimeCache"); 

당신은 예를 들어 속성을 생성자에 ICacheManager 인터페이스를 삽입 할 수 사용하십시오.

public YourClass([Dependency("runtimeCache")] ICacheManager<object> cache) 
{ 

} 
+0

오, 이것은 매우 시작에서 트릭있다 : [종속성 ("runtimeCache")] 이 기능을 모르는의 (이전 이름이 주사를 사용하지 않은) 때문에, 난을 찾기 위해 노력했다 해결해라. 다시 한 번 감사드립니다. –