我有两个仓库AlbumRepository
与接口IAlbumRepository
和CachedAlbumRepository
用具有构造接口IAlbumRepository
接口IAlbumRepository
。我需要注入Ninject ICachedAlbumRepository
与CachedAlbumRepository
和构造函数与AlbumRepository
。如何使用战略格局与Ninject
如何用Ninject实现它?
与结构图同样的方法
x.For<IAlbumRepository>().Use<CachedAlbumRepository>()
.Ctor<IAlbumRepository>().Is<EfAlbumRepository>();
public class CachedAlbumRepository : IAlbumRepository
{
private readonly IAlbumRepository _albumRepository;
public CachedAlbumRepository(IAlbumRepository albumRepository)
{
_albumRepository = albumRepository;
}
private static readonly object CacheLockObject = new object();
public IEnumerable<Album> GetTopSellingAlbums(int count)
{
string cacheKey = "TopSellingAlbums-" + count;
var result = HttpRuntime.Cache[cacheKey] as List<Album>;
if (result == null)
{
lock (CacheLockObject)
{
result = HttpRuntime.Cache[cacheKey] as List<Album>;
if (result == null)
{
result = _albumRepository.GetTopSellingAlbums(count).ToList();
HttpRuntime.Cache.Insert(cacheKey, result, null,
DateTime.Now.AddSeconds(60), TimeSpan.Zero);
}
}
}
return result;
}
}
好的提示史蒂文。谢谢! – Reno 2013-04-23 08:14:16