【发布时间】:2011-07-19 19:06:13
【问题描述】:
所以,假设我有一个接口IThingFactory:
public interface IThingFactory
{
Thing GetThing(int thingId);
}
现在,假设我有一个从数据库中检索Things 的具体实现。现在,假设我有一个具体的实现,它包装了现有的IThingFactory,并在访问包装的IThingFactory 之前检查Thing 是否存在于内存缓存中。比如:
public class CachedThingFactory : IThingFactory
{
private IThingFactory _wrapped;
private Dictionary<int, Thing> _cachedThings;
public CachedThingFactory(IThingFactory wrapped)
{
this._wrapped = wrapped;
_cachedThings = new Dictionary<int,Thing>();
}
public Thing GetThing(int thingId)
{
Thing x;
if(_cachedThings.TryGetValue(thingId, out x))
return x;
x = _wrapped.GetThing(thingId);
_cachedThings[thingId] = x;
return x;
}
}
我将如何使用依赖注入和 Ninject 之类的东西来处理这样的场景,以便我可以配置 DI 容器,以便我可以注入或删除这样的缓存代理,或者说,记录,或者(在此处插入)?
【问题讨论】:
标签: c# dependency-injection ninject ninject-2