Team blog of MCS @ Middle East and Africa

This blog is created by Microsoft MEA HQ near shoring team, and it aims to share knowledge with the IT community.With its infrastructure and development sides,It brings to you the proven best practices and real world experiences from Subject Matter Experts
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

Modular Web Applications with MEF

Modular Web Applications with MEF

  • Comments 1
  • Likes

A lot of times we are faced with a challenge of building a web application that should evolve with time. As the web application grows, maintaining and adding new functionality becomes harder and harder. Also allowing multiple teams to work on the same web application has its own challenges.

A number of solutions were created to tackle this problem, an example would be ASP.NET webparts, which can be plugged into a web application to add functionality.

In my previous post, I gave a small introduction about the Managed Extensibility Framework (MEF), and how it can be used to build an application that supports plugins. In this post I am going to talk about using MEF to create a pluggable web application.

As explained in the previous post, MEF uses the concept of “Exports” and “Imports”. Each export exports an interface type, and each import imports an interface type. Exports and imports are matched at runtime by their interface types.

We can define an interface that will be used by the web application and the modules to agree on what information is required to be supplied by the module so that the web application can load it and make it available for the website users.

An example of this interface could be as follows

public interface IModule
    {
        string ModuleName { get; }
        string DefaultPage { get; }
        string Version { get; }
        bool ShowInMenu { get; }
        List<string> ModulePermissions { get; }
    }

 

The interface defines some metadata about the module, information like module, landing page or default page of the module, version, what if it should be available through the web application’s main menu, and who can access this module. Of course more data can be added depending on the web application being implemented.

As sample implementation of the above would be

[Export(typeof(IModule))]
    public class StudentsModule:MCS.Common.Interfaces.IModule
    {
        public string ModuleName
        {
            get {
                return "StudentsModule";
            }
        }
        public string DefaultPage
        {
            get {
                return "~/Students/Default.aspx";
            }
        }
        public string Version
        {
            get {
                return "1.0";
            }
        }
        public bool ShowInMenu
        {
            get {
                return true;
            }
        }
        public List<string> ModulePermissions
        {
            get {
                return new List<string> { "AddStudent", "DeleteStudent", "UpdateStudent" };
            }
        }
    }

 

Loading these modules into the main web application would be as simple as importing the types that implement the IModule interface. This could be done in a base page class that is used by all the site pages

 [ImportMany]
 public List<IModule> Modules { get; set; }

After the application loads, MEF will look into the bin folder of the web application for any assemblies that contain a class that implements the IModule type, and load them into the collection.

From this point on the Modules collection can be used to create a site menu and do other custom logic to allow the modules to be accessible to site users.

Deploying the module would be as easy as copying the website files into a subfolder inside the web application, and the dlls into the main application’s bin folder.

Using this method, although very simple, can allow different teams to work on separate modules independently.

Comments
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment