skip to the main content area of this page
Patterns and Practices

 

ASP.NET MVC Framework and Composite Web Application Block ( CWAB )


The Composite Web Application Block ( CWAB ) in the Web Client Software Factory can be used in the ASP.NET MVC Framework for business module discovery and initialization as well as dependency injection of Controllers. In this quick tutorial I will show you code snippets for doing this from my presentation at the South Florida Code Camp. We will also use the StateValue<T> Class to abstract out the fact that we store our Shopping Cart for our green tea store in Session State.

 

Our Green Tea Store

As my example at the South Florida Code Camp, I created a ficticious green tea store website that sells Gyokuro, Sencha, and Matcha Green Tea. It comes complete with shopping cart.

 

Green Tea Store

 

Gyokuro

 

Green Tea Store

 

Composite Web Application Block

You can wire up the Composite Web Application Block and ObjectBuilder by referencing them in your ASP.NET MVC Website as well as adding the appropriate hooks into the Global.asax file and Web.config.

 

public class Global : WebClientApplication
{
    protected override void Start()
    {
        // Specify Custom Controller Factory
        ControllerBuilder.Current.
            SetDefaultControllerFactory(typeof(ControllerFactory));
            
        // Add Routes
        AddRoutes();
    }
}

 

<sectionGroup name="compositeWeb">
    <section
        name="modules"
        type="Microsoft.Practices.CompositeWeb.Configuration...."/>
</sectionGroup>

<compositeWeb>
    <modules>
        <module
            name="Ecommerce"
            assemblyName="Ecommerce"
            virtualPath="~/Products"/>
        <module
            name="Blog"
            assemblyName="Blog"
            virtualPath="~/News"/>
    </modules>
</compositeWeb>

 

I suspect the virtualPath functionality used in conjunction with the AuthorizationService, AuthorizationRulesService, and WebClientAuthorizationModule may need customization to work appropriately to provide URL-based security based on user permissions. I haven't dug into this yet but plan on doing so in the near future.

 

ModuleInitializers to Register ProductCatalog and Blog Services

Each Business Module, Ecommerce and Blog, will have a ModuleInitializer to load their ProductCatalog and BlogDataSource Services.

They are not any different than what you would use in an ASP.NET Webforms Application:

 

public class EcommerceModuleInitializer : ModuleInitializer
{
    public override void Load(CompositionContainer container)
    {
        base.Load(container);
        container.Parent.Services.Add<IProductCatalog>(new ProductCatalog());
    }
}


public class BlogModuleInitializer : ModuleInitializer
{
    public override void Load(CompositionContainer container)
    {
        base.Load(container);
        container.Parent.Services.Add<IBlogDataSource>(new BlogDataSource());
    }
}

 

ControllerFactory for Dependency Injection

Create your custom ControllerFactory for injecting dependencies into your Controller Classes of the ASP.NET MVC Framework.

 

public class ControllerFactory : IControllerFactory
{
    public IController CreateController
            (RequestContext context, Type controllerType)
    {
        object controller = Activator.CreateInstance(controllerType);
        WebClientApplication.BuildItemWithCurrentContext(controller);
        return controller as IController;
    }
}

 

Inject the Shopping Cart From Session State Using StateValue<T> Class

Let's store our green tea in a shopping cart that is persisted to SessionState. Notice the StateValue<Cart> Cart Property on the CartController.

 

public class CartController : Controller
{
    [SessionStateKey("Cart")]
    public StateValue<Cart> Cart;

    [ControllerAction]
    public void Index()
    {
        Cart cart = GetCart();
        RenderView("Index", cart);
    }

    [ControllerAction]
    public void AddItem()
    {
        CartItem item = new CartItem();
        item.UpdateFrom(Request.Form);

        Cart cart = GetCart();

        cart.Items.Add(item);

        RedirectToAction("Index");
    }

    [ControllerAction]
    public void DeleteItem(int productId)
    {
        Cart cart = GetCart();
        cart.RemoveItem(productId);

        RedirectToAction("Index");
    }

    private Cart GetCart()
    {
        if (Cart.Value == null)
            Cart.Value = new Cart();

        return Cart.Value;
    }
}

 

Conclusion

Although I have not completely investigated every aspect of the Composite Web Application Block with the ASP.NET MVC Framework, much of the functionality in the CWAB appears to work fine with the ASP.NET MVC Framework.

The Unity Dependency Injection Container in Enterprise Library 4.0 would be a natural evolution for the dependency injection done with the Composite Web Application Block. We will have to wait and see if the Web Client Team evolves the WCSF to include new functionality in Enterprise Library 4.0.


Tags: CompositeWebApplicationBlock, StateValue, DependencyInjection, IoC, ModuleInitializer, IControllerFactory, Unity


Topics



 

Popular Tags



Recent Links