23/02/2010 1 Comment
Ninject is one of the best tool for dependency injection that i use. I tell about how to use with sharepoint in this article.
Ninject Source Code,
Microsoft Sharepoint Library.
First i want to say that i m using Sharepoint with Framework 3.5 and you can find how to configure sharepoint with working Sharepoint in this article of mine.http://blog.bugrapostaci.com/2009/11/12/step-by-step-configure-your-sharepoint-running-with-net-framework-3-5/
Thats why one of the reason i recompile with Ninject Library with Framework 3.5 for this but if you use Framework 2.0 its not necessery for you. if you mind you learn about mantioned in my another article.http://blog.bugrapostaci.com/2010/02/22/solved-ninject-system-runtime-compilerservices-extensionattribute-is-defined-in-multiple-assemblies/
For working with Ninject in Asp.net you should inherit Global class with Ninject.Framework.Web.NinjectHttpApplication but in sharepoint standart Global class is derived from Microsoft.Sharepoint.ApplicationRuntime.SPHttpApplication. the second reason that i recompile ninject source.
Example of Standart Sharepoint Global.asax:
[Drive]:\inetpub\wwwroot\wss\VirtualDirectories\[Your app port]\Global.asax
<%@ Assembly Name=”Microsoft.SharePoint”%>
<%@ Application Language=”C#” Inherits=”Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication” %>
You can download Ninject Source Code from project site of Ninject.
Than i created a new project in Framework folder. And add references of Microsoft.Sharepoint library.
My project’s namespace is Ninject.Framework.Web.Sharepoint.I create a class named “NinjectSPHttpApplication”
that just inherits SPHttpApplication inspite of HttpApplication. All members code as same as Ninject.Framework.Web.NinjectHttpApplication class.
/// A <see cref=”HttpApplication”/> that creates a <see cref=”IKernel”/> for use throughout
/// the application.
public abstract class NinjectSPHttpApplication : SPHttpApplication
….Same as NinjectHttpApplication
And build the all solution.
You can download source code of my project :
And i also add functionalities of UserControlBase and WebControlBase classes for injection about a good case.
Thats the page:http://stackoverflow.com/questions/1274026/ninject-asp-net-and-custom-controls
After your ninject dlls ready you should add this dlls to your 80/BIN folder and register with GAC and add to your sharepoint application web.config file. I don’t tell how to register custom assemblies in sharepoint asume that you know already.
After you complete this you can change that your Global.asax file in your Sharepoint wss root folder.
[Drive:]\inetpub\wwwroot\wss\VirtualDirectories\[Your app port]\Global.asax Like:
<%@ Import Namespace=”Ninject.Framework.Web.Sharepoint” %>
<%@ Import Namespace=”Ninject.Core” %>
<%@ Application Language=”C#” inherits=”Ninject.Framework.Web.Sharepoint.NinjectSPHttpApplication” %>
protected override IKernel CreateKernel()
IKernel kernel = new StandardKernel(new SharePointUnitTestModule() );
public class SharePointUnitTestModule : StandardModule
public override void Load()
...Your codes here.
Keep going with Part 2: