How to: Enable CWAB in a SharePoint application guidance released

A couple of weeks ago a new guidance to enable the Composite Web Application Block 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.

EnablingĀ  CWAB in Sharepoint applications will allow developers to use features like dependency injection.

The main responsibility of enabling CWAB in Sharepoint applications lays with the SPWebClientApplication and the RootContainerLocatorService classes.

While the SPWebClientApplication's BuildItemWithCurrentContext method runs different code depending on the application implementation (WebClient/Sharepoint), hence allowing Web Part reusability, the RootContainerLocatorService always returns the same instance of the root CompositionContainer.

This is the code of the BuildItemWithCurrentContext method:

public static void BuildItemWithCurrentContext(object obj)
{
   IHttpContext context = CurrentContext;
   if (context.ApplicationInstance is SPWebClientApplication)
   {
      SPWebClientApplication app = (SPWebClientApplication)context.ApplicationInstance;
      IBuilder<WCSFBuilderStage> builder = app.PageBuilder;
      CompositionContainer container = app.GetModuleContainer(context);
      CompositionContainer.BuildItem(builder, container.Locator, obj);
   }
   else if (context.ApplicationInstance is WebClientApplication)
   {
      WebClientApplication.BuildItemWithCurrentContext(obj);
   }
}

As mentioned in my post about the guidance to Enable Unity in Sharepoint, 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 making theĀ  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 CWAB in a SharePoint Application
  • How To: Reuse WCSF User Controls in SharePoint
  • How To: Use CWAB to Build Testable Sharepoint Web Parts
  • How To: Reuse Foundational Modules in Sharepoint

If you want more info about SharePoint you can check:

Hope you find the guidance useful.