2017-03-18 94 views
1

想象一下,我有一個接口的下方,從的ICacheManager <所有繼承>是否可以同時使用具有不同配置類型的ICacheManager <>?

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

我要注入的ICacheManager {CacheType}接口,諸如高速緩存類implemantation:

CacheRuntime, CacheRedis, CacheRedisWithRuntime 

使用Unity我想像下面那樣注入它們:

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

喔,這已經從非常begining的伎倆:[依賴(「runtimeCache」) 因爲不知道這個功能的(沒有使用之前名爲注射用),我試圖找到一個工作。 非常感謝你! –

相關問題