使用 AutoFac 的通用复杂类型注册通用类型
Registering Generic Types using Generic complex type with AutoFac
我有一个缓存存储库,它继承自与我的数据存储库相同的接口。
public interface IReadOnlyRepository<T,U>
{
T Get(U key);
IList<T> GetAll();
}
缓存存储库在其构造函数中采用与其泛型类型匹配的 IReadOnlyRepository。有了这个,我能够实现代码来检查该项目是否在缓存中,如果没有则从数据库中检索并存储到缓存中。我 运行 遇到了在 AutoFac 中注册此通用缓存的问题。我有以下代码现在正在中断
builder.RegisterGeneric(typeof(DomainObjectRepository<,>)).Keyed("iRepoLayer",typeof(IRepository<,>));
builder.RegisterGeneric(typeof(CacheRepo<,>))
.As(typeof(IRepository<,>))
.WithParameters(new List<Parameter>()
{
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(IRepository<,>),
(pi, ctx) => ctx.ResolveKeyed("iRepoLayer",typeof(IRepository<,>))),
...(Other Constructor params)
}
界面
public interface IReadOnlyRepository<T,U>
{
/// <summary>
/// Gets the specified item from the repository.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
T Get(U key);
/// <summary>
/// Gets all items in the repository
/// </summary>
/// <returns></returns>
IList<T> GetAll();
}
数据层实现
public class ReadOnlyRepository<T,U> :IReadOnlyRepository<T,U> where T:ObjectWithId<U>
{
internal ICoreRepository _coreRepository;
public ReadOnlyRepository(ICoreRepository coreRepository)
{
_coreRepository = coreRepository;
}
private static Expression<Func<T, object>> _getId = t => t.Id;
public T Get(U key)
{
return _coreRepository.GetBy(_getId, key);
}
public virtual IList<T> GetAll()
{
return _coreRepository.GetAll<T>().ToList();
}
}
缓存层
public class CacheReadOnlyRepo<T,TId> : IReadOnlyRepository<T, TId> where T :ObjectWithId<TId>
{
private IReadOnlyRepository<T, TId> _readOnlyRepo { get; set; }
protected static readonly string IdKeyPrefix = GetKeyPrefix(o => o.Id);
protected static readonly string GetAllKey = GetSetCacheKey<T>();
protected ICoreCachingRepo _coreCachingRepo;
public CacheReadOnlyRepo(IReadOnlyRepository<T,TId> readOnlyRepo, CacheEventManager cacheEventManager, ICacheClient cacheClient, ICoreCachingRepo coreCachingRepo)
{
_readOnlyRepo = readOnlyRepo;
_cacheEventManager = cacheEventManager;
_cacheClient = cacheClient;
_coreCachingRepo = coreCachingRepo;
}
public T Get(TId id)
{
var key = GetCacheKey(IdKeyPrefix, id);
//calls the cache for item and passes method to retreive items if they are not in cache
return _coreCachingRepo.GetViaCache(_readOnlyRepo.Get, key, id);
}
public virtual IList<T> GetAll()
{
//calls the cache for item and passes method to retreive items if they are not in cache
return _coreCachingRepo.GetAllViaCache(_readOnlyRepo.GetAll, GetAllKey).ToList();
}
}
谢谢!
我想你想要的是装饰器设计模式的实现。 Autofac 明确支持该模式。
这是一篇详细描述支持的文章:http://nblumhardt.com/2011/01/decorator-support-in-autofac-2-4/
相关代码片段已调整为使用您的 类:
builder.RegisterGeneric(typeof(ReadOnlyRepository<,>))
.Named("read-only-repo", typeof(IReadOnlyRepository<,>));
// Register the generic decorator so it can wrap
// the resolved named generics.
builder.RegisterGenericDecorator(
typeof(CacheReadOnlyRepo<,>),
typeof(IReadOnlyRepo<,>),
fromKey: "read-only-repo");
我有一个缓存存储库,它继承自与我的数据存储库相同的接口。
public interface IReadOnlyRepository<T,U>
{
T Get(U key);
IList<T> GetAll();
}
缓存存储库在其构造函数中采用与其泛型类型匹配的 IReadOnlyRepository。有了这个,我能够实现代码来检查该项目是否在缓存中,如果没有则从数据库中检索并存储到缓存中。我 运行 遇到了在 AutoFac 中注册此通用缓存的问题。我有以下代码现在正在中断
builder.RegisterGeneric(typeof(DomainObjectRepository<,>)).Keyed("iRepoLayer",typeof(IRepository<,>));
builder.RegisterGeneric(typeof(CacheRepo<,>))
.As(typeof(IRepository<,>))
.WithParameters(new List<Parameter>()
{
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(IRepository<,>),
(pi, ctx) => ctx.ResolveKeyed("iRepoLayer",typeof(IRepository<,>))),
...(Other Constructor params)
}
界面
public interface IReadOnlyRepository<T,U>
{
/// <summary>
/// Gets the specified item from the repository.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
T Get(U key);
/// <summary>
/// Gets all items in the repository
/// </summary>
/// <returns></returns>
IList<T> GetAll();
}
数据层实现
public class ReadOnlyRepository<T,U> :IReadOnlyRepository<T,U> where T:ObjectWithId<U>
{
internal ICoreRepository _coreRepository;
public ReadOnlyRepository(ICoreRepository coreRepository)
{
_coreRepository = coreRepository;
}
private static Expression<Func<T, object>> _getId = t => t.Id;
public T Get(U key)
{
return _coreRepository.GetBy(_getId, key);
}
public virtual IList<T> GetAll()
{
return _coreRepository.GetAll<T>().ToList();
}
}
缓存层
public class CacheReadOnlyRepo<T,TId> : IReadOnlyRepository<T, TId> where T :ObjectWithId<TId>
{
private IReadOnlyRepository<T, TId> _readOnlyRepo { get; set; }
protected static readonly string IdKeyPrefix = GetKeyPrefix(o => o.Id);
protected static readonly string GetAllKey = GetSetCacheKey<T>();
protected ICoreCachingRepo _coreCachingRepo;
public CacheReadOnlyRepo(IReadOnlyRepository<T,TId> readOnlyRepo, CacheEventManager cacheEventManager, ICacheClient cacheClient, ICoreCachingRepo coreCachingRepo)
{
_readOnlyRepo = readOnlyRepo;
_cacheEventManager = cacheEventManager;
_cacheClient = cacheClient;
_coreCachingRepo = coreCachingRepo;
}
public T Get(TId id)
{
var key = GetCacheKey(IdKeyPrefix, id);
//calls the cache for item and passes method to retreive items if they are not in cache
return _coreCachingRepo.GetViaCache(_readOnlyRepo.Get, key, id);
}
public virtual IList<T> GetAll()
{
//calls the cache for item and passes method to retreive items if they are not in cache
return _coreCachingRepo.GetAllViaCache(_readOnlyRepo.GetAll, GetAllKey).ToList();
}
}
谢谢!
我想你想要的是装饰器设计模式的实现。 Autofac 明确支持该模式。
这是一篇详细描述支持的文章:http://nblumhardt.com/2011/01/decorator-support-in-autofac-2-4/
相关代码片段已调整为使用您的 类:
builder.RegisterGeneric(typeof(ReadOnlyRepository<,>))
.Named("read-only-repo", typeof(IReadOnlyRepository<,>));
// Register the generic decorator so it can wrap
// the resolved named generics.
builder.RegisterGenericDecorator(
typeof(CacheReadOnlyRepo<,>),
typeof(IReadOnlyRepo<,>),
fromKey: "read-only-repo");