所以,让我们说我有一个接口IThingFactory
:依赖注入:如何配置接口绑定包
public interface IThingFactory
{
Thing GetThing(int thingId);
}
现在,让我们说我有一个具体的实现,从数据库中检索Thing
秒。现在,我们还要说一个具体的实现,它包装现有的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容器,这样我可以注入或删除缓存代理这样,或者说,记录的东西,或者(插入这里)?
甚至不知道存在WhenInjectedInto()方法。谢谢! – FMM