Ninject with Sharepoint

Ninject is one of the best tool for dependency injection that i  use. I tell about how to use with sharepoint in this article.   

Prerequists.
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.https://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.https://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.
http://ninject.org/assets/dist/Ninject-1.0-src.zip   

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.   

namespace Ninject.Framework.Web.Sharepoint   

{   

    /// <summary>   

    /// A <see cref=”HttpApplication”/> that creates a <see cref=”IKernel”/> for use throughout   

    /// the application.   

    /// </summary>   

    public abstract class NinjectSPHttpApplication : SPHttpApplication   

    {   

          ….Same as NinjectHttpApplication   

    }   

}    

 

And build the all solution. 

You can download source code of my project :
http://rapidshare.com/files/354665567/NinjectSource.rar 

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” %> 
 

<SCRIPT language=”C#” runat=”server”>    
    protected override IKernel CreateKernel()
    { 
        IKernel kernel = new StandardKernel(new SharePointUnitTestModule() );
        return kernel;
    }
    public class SharePointUnitTestModule : StandardModule
    {
        public override void Load()
        {
          ...Your codes here.
        }
    }
</SCRIPT>

Keep going with Part 2:
https://blog.bugrapostaci.com/2010/02/27/ninject-with-sharepoint-2/

Advertisement