On Github paulzerkel / pres-di-ninject
11/7/2013
Ninject is an open source dependency injection framework for the .NET platform. It is powerful but also designed to be easy enough to use on all projects.
Inversion of Control (IoC) is an object oriented programming technique where collaborating objects work through abstractions and the implementation of those abstractions are not known at run-time.
Dependency Injection is a form of Inversion of Control where an object receives required objects from an external source.
public class TaskFinder { private readonly ITaskService service; public TaskFinder(ITaskService service) { this.service = service; } public IEnumerable<WorkTask> Find(string email, int minPriority) { return service.FindTasks(email) .Where(x => x.Priority >= minPriority); } }
It would be, but that is where a DI framework will help.
Ninject ...
Bind<ITaskService>().To<SimpleTaskService>();
Bind<ITaskService>>().To<ExchangeService>() .WithConstructorArgument("url", new Uri("http://anotherserver.com"));
Bind<IRetryPolicy>().To<RandomRetryPolicy>().WhenInjectedInto<LoggingRetryPolicy>();
The Kernel allows you to retrieve objects that are fully constructed from Ninject.
var taskFinder = kernel.Get<TaskFinder>();