2017-03-18 45 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”) 因为不知道这个功能的(没有使用之前名为注射用),我试图找到一个工作。 非常感谢你! –

相关问题