Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the binding are done with decorators using Ninject?

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();
}
like image 958
Rushino Avatar asked Dec 09 '11 14:12

Rushino


1 Answers

You have to use conditional bindings e.g

Bind<IRepository>().To<Repository>().WhenInjectedInto<CachedRopsitory>();
Bind<IRepository>().To<CachedRepository>().WhenInjectedInto<CachedLoggedRepository>();
Bind<IRepository>().To<CachedLoggedRepository>();
like image 162
Remo Gloor Avatar answered Oct 18 '22 04:10

Remo Gloor