How the binding are done with decorators using Ninject?

对着背影说爱祢 提交于 2019-11-26 18:24:34

问题


Based on this question : Should thoses kind of service go injected in a base class ? (versus static classes).

How the binding would be done with decorators using Ninject ? or any DIContainer ?

public class CachedLoggedRepository : IRepository
{
   public IRepository repository { get; set; }
   void Add();
}

public class CachedRepository : IRepository
{
   public IRepository repository { get; set; }
   void Add();
}

public class Repository : IRepository
{
   void Add();
}

回答1:


You have to use conditional bindings e.g

Bind<IRepository>().To<Repository>().WhenInjectedInto<CachedRopsitory>();
Bind<IRepository>().To<CachedRepository>().WhenInjectedInto<CachedLoggedRepository>();
Bind<IRepository>().To<CachedLoggedRepository>();


来源:https://stackoverflow.com/questions/8447037/how-the-binding-are-done-with-decorators-using-ninject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!