background preloader

Ninject

Facebook Twitter

Dependency injection for filters · ninject/ninject.web.mvc Wiki. MVC3 introduced a completely new pattern to configure filters for controllers and its actions. While injection of filter attributes is still supported it is recommended using this new pattern for filter configuration because it has the advantage to support constructor injection and does not require the InjectAttribute anymore. First of all you have to create your filter class by implementing one of the filter interfaces e.g. IActionFilter. All the dependencies are added to the constructor. The following example of a logging filter has a logger as dependency and can be configured with the level that is used to log. To apply this filter to an action or controller we need to specify a binding. In the example below the LogFilter is applied to every action and configured with the log level info.

Further Information on this topic: Ninject ef repository example CodeCamper. Ninject and Entity Framework Code First: Easy repository patterns | My Tech World. When I was still learning the basics of Dependency Injection (DI), Repositories and Object Relational Mappers (ORM), it was a pretty long struggle to get my head around when much of my professional career had revolved around web forms and .Net 2. It was only through playing around with code in my spare time that things gradually fell into place. I’m not a gifted developer savant like Scott Hanselman, Steve Sanderson or Rob Conery. Stuff doesn’t just “make sense” as soon as I read it. I need to walk through slowly and see stuff working first before trying to understand how it works.

So having gone through many failed or flawed implementations of the repository pattern and almost-but-not-quite getting DI to work correctly (let alone trying to get my head around Inversion of Control), I believe I’ve finally hit upon a clean, simple repository structure that allows good separation of concerns, highly-testable code and makes coding up my MVC3 sites an absolute doddle. Getting Started.

Ninject - Home. With many dependency injection frameworks, your XML mapping files can quickly grow large and difficult to navigate. With Ninject, your type bindings are collected into groups called modules. Each of these modules represents an independent segment of your application. They can be organized however you like. Modules just need to implement the IModule interface, but most should extend the StandardModule class for simplicity. Here's an example: class WarriorModule : StandardModule { public override Load() { Bind<IWeapon>().To<Sword>(); Bind<Samurai>().ToSelf(); } } Public Class WarriorModule Inherits StandardModule Public Overrides Sub Load() Bind(Of IWeapon).To(Of Sword)() Bind(Of Samurai).ToSelf() End SubEnd Class Once you create modules, you load them into a container called the kernel.

This example illustrates how to create a kernel, and activate and use an instance of the Samurai type: After Ninject works its magic, the result of the call to Get() is a Samurai armed with a Sword. Adding Ninject to Web API - Peter Provost's Geek Noise. In my last post I focused on how to unit test a new Visual Studio 2012 RC ASP.NET Web API project. In general, it was pretty straightforward, but when I had Web API methods that needed to return an HttpResponseMessage, it got a little harder. If you recall, I decided to start with the Creating a Web API that Supports CRUD Operations tutorial and the provided solution that came with it. That project did not use any form of dependency inversion to resolve the controller’s need for a ProductRepository. My solution in that post was to use manual dependency injection and a default value.

But in the real world I would probably reach for a dependency injection framework to avoid having to do all the resolution wiring throughout my code. In this post I am going to convert the manual injection I used in the last post to one that uses the Ninject framework. Getting Started The default constructor provides the default value for the repository, while the second constructor lets us provide one.