2017-03-18 5 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}インターフェースを注入したい:Unityと

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"); 
      }); 
     })); 

最初のものを解決するために、あなたはあなたが例えば属性を持つコンストラクタにICacheManagerインターフェースを注入することができ

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

を使用すると思います。

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

} 
+0

おお、これは非常に初めからトリックされています:[依存関係(「runtimeCache」)] この機能を知らないの(前の名前の注射を使用していない)ので、私は見つけるしようとしていました回避する。 ありがとう、もう一度! –

関連する問題