How To: Enable Unity in a SharePoint application guidance just released

A new guidance to help developers enable Unity in a SharePoint application has just been released by the Patterns & Practices Sustained Engineering team. You can download the release from the  Web Client Software factory codeplex site.

In the release, the base SPHttpApplication class is customized by using it as the parent class of the SPUnityHttpApplication class.

The main feature this class has, to enable the Unity container, is the BuildItemWithCurrentContext method. Below is the code for this method:

public static void BuildItemWithCurrentContext(object obj)
{
      HttpContext context = System.Web.HttpContext.Current;
      Unity.SharePoint.SPUnityHttpApplication app =
         (Unity.SharePoint.SPUnityHttpApplication)context.ApplicationInstance;
      app.Container.BuildUp(obj.GetType(), obj);
}

The objective of  enabling the Unity container is holding the different elements of your application to perform dependency injection into your classes.

Custom Web Part and User Control classes are created in the release to enable them for Dependency Injection. This will give your classes a low coupling level, since there will be no hard references between them. Given that, you can perform unit tests in an easy manner.

To increase the testing capabilities even more, the Model-View-Presenter pattern is used in this example. The objective of this pattern is placing all complex UI logic in a presenter class, thus it is easier to test. The image below (from the MSDN Web Client Software Factory site) shows the relationship between the different components in the pattern.

MVP

We also posted the complete guidance here for online reference. It contains the following topics:

  • How To: Enable Unity in a SharePoint Application
  • How To: Create a Web Part that uses Dependency Injection
  • How To: Unit test the Web Part
  • How To: Deploy the Web Part

If you want more info about SharePoint you can check:

The next post will cover the guidance to enable CWAB in a SharePoint Application, stay tuned.