Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Components, Aspects, and Dynamic Decorator for ASP.NET MVC Applications

0.00/5 (No votes)
17 May 2011 1  
Apply components, aspects, and dynamic decorator principles to ASP.NET MVC applications.

Introduction

As discussed in Components, Aspects and Dynamic Decorator, application development involves tasks specific to an application type and tasks independent of an application type. This separation of tasks is significant in not only helping you decide on processing strategy, deployment strategy, test strategy, UI strategy, etc., based on specific technologies associated with an application type, but also helping you address the application-type-independent, a.k.a. common, tasks like designing/extending components and addressing cross-cutting concerns systematically. Several guidelines are discussed for the development of these common tasks. The Dynamic Decorator is used to address these tasks by following these guidelines. A WinForms application is used as an example.

In this article, I demonstrate how the principles and guidelines are applied to a different application type - ASP.NET MVC application - by using the Dynamic Decorator.

Choose Application Type

There are several application types in the .NET world: WinForms, ASP.NET, ASP.NET MVC, and Silverlight. A WinForms application offers rich user interactions and client processing power. An ASP.NET application has advantages of deployment and maintenance via the Web. ASP.NET MVC offers testability plus the advantages of ASP.NET. A Silverlight application offers a rich user interface over the Web. Different types of applications have different technology specifics, which may or may not meet your requirements. You should choose your application type based on your processing strategy, deployment strategy, maintenance strategy, test strategy, UI strategy, etc.

Different application types also have different application programming models, UI elements, state management, event processing models, etc. You need to address the tasks related to an application type once you select the application type for your application.

Address Common Tasks

In addition to the tasks specific to an application type, there are also tasks independent of a particular application type in application development. For instance, regardless of application types, you face tasks like designing, extending components, and addressing cross-cutting concerns. They are common for all application types.

A set of principles are given for the development of these common tasks in the article Components, Aspects and Dynamic Decorator. They are listed here again as follows:

  • Design components to meet business requirements in a general way
  • Design aspects as global methods in their own modules
  • Add aspects to objects as needed
  • Extend objects as needed

Please refer to Components, Aspects and Dynamic Decorator for details of the discussion about these principles. You may also need to read the article Dynamic Decorator Pattern to understand the Dynamic Decorator, and read the article Add Aspects to Object Using Dynamic Decorator to understand aspects programming using Dynamic Decorator.

Example

In the following sections, an example application is discussed to demonstrate how the above principles are applied to an ASP.NET MVC application by using the Dynamic Decorator.

The same problem discussed in Components, Aspects and Dynamic Decorator as example is used here. This time, we choose an ASP.NET MVC application as the application type instead of a WinForms application. For convenience, I state the problem again as follows.

Problem

Display employees based on the selection of a department.

Components

Assume there are two components: Employee and Department. For Employee, there is a corresponding RepositoryEmployee component to contain a collection of objects of Employee. For Department, there is a corresponding RepositoryDepartment component to contain a collection of objects of Department. The code of these components is listed here:

public interface IEmployee
{
    System.Int32? EmployeeID { get; set; }
    System.String FirstName { get; set; }
    System.String LastName { get; set; }
    System.DateTime DateOfBirth { get; set; }
    System.Int32? DepartmentID { get; set; }
    System.String FullName();
    System.Single Salary();
}

public class Employee : IEmployee
{
    #region Properties

    public System.Int32? EmployeeID { get; set; }
    public System.String FirstName { get; set; }
    public System.String LastName { get; set; }
    public System.DateTime DateOfBirth { get; set; }
    public System.Int32? DepartmentID { get; set; }

    #endregion

    public Employee(
        System.Int32? employeeid
        , System.String firstname
        , System.String lastname
        , System.DateTime bDay
        , System.Int32? departmentID
    )
    {
        this.EmployeeID = employeeid;
        this.FirstName = firstname;
        this.LastName = lastname;
        this.DateOfBirth = bDay;
        this.DepartmentID = departmentID;
    }

    public Employee() { }

    public System.String FullName()
    {
        System.String s = FirstName + " " + LastName;
        return s;
    }

    public System.Single Salary()
    {
        System.Single i = 10000.12f;
        return i;
    }
}

public interface IDepartment
{
    System.Int32? DepartmentID { get; set; }
    System.String Name { get; set; }
}

public class Department : IDepartment
{
    #region Properties

    public System.Int32? DepartmentID { get; set; }
    public System.String Name { get; set; }

    #endregion

    public Department(
        System.Int32? departmentid
        , System.String name
    )
    {
        this.DepartmentID = departmentid;
        this.Name = name;
    }

    public Department() { }
}

public interface IRepository<T>
{
    List<T> RepList { get; set; }
    void GetAll();
}

public class RepositoryEmployee : IRepository<IEmployee>
{
    private List<IEmployee> myList = null;

    public List<IEmployee> RepList
    {
        get { return myList; }
        set { myList = value; }
    }

    public RepositoryEmployee()
    {
    }

    public void GetAll()
    {
        myList = new List<IEmployee> { 
            new Employee(1, "John", "Smith", new DateTime(1990, 4, 1), 1), 
            new Employee(2, "Gustavo", "Achong", new DateTime(1980, 8, 1), 1), 
            new Employee(3, "Maxwell", "Becker", new DateTime(1966, 12, 24), 2), 
            new Employee(4, "Catherine", "Johnston", new DateTime(1977, 4, 12), 2), 
            new Employee(5, "Payton", "Castellucio", new DateTime(1959, 4, 21), 3), 
            new Employee(6, "Pamela", "Lee", new DateTime(1978, 9, 16), 4) };
    }
}

public class RepositoryDepartment : IRepository<IDepartment>
{
    private List<IDepartment> myList = null;

    public List<IDepartment> RepList
    {
        get { return myList; }
        set { myList = value; }
    }

    public RepositoryDepartment()
    {
    }

    public void GetAll()
    {
        myList = new List<IDepartment> { new Department(1, "Engineering"), 
            new Department(2, "Sales"), 
            new Department(3, "Marketing"), 
            new Department(4, "Executive") };
    }
}

In this application, the data for employees and departments are hard-coded in two lists to simplify our discussion. In a real world application, these data are normally persisted in a relational database. Then, you will need to create a data layer to retrieve them and put them in the lists.

It is worth noting that the employee list is populated by the inserting order without any kind of sorting in place. It is difficult to anticipate what kind of sorting to support for this component at this time. In applications, an object of the component may need to be sorted by last name. Another may need to be sorted by birth day. A third may not need any sorting at all. So, it is better to defer the implementation of sorting until the component is used in an application. By designing the component RepositoryEmployee without concerning about sorting, the principle "Design components to meet business requirements in a general way" is followed. This way, the component is stable and closed.

HRMVC

HRMVC is an ASP.NET MVC application which uses the above components to display the employees based on the selection of a department. Since it is an ASP.NET MVC application, it follows ASP.NET MVC's application programming model and event model. The Controller code is listed as follows.

public class DepEmployeesController : Controller
{
    private IRepository<IEmployee> rpEmployee = null;
    private IRepository<IDepartment> rpDepartment = null;

    private static int iStaticDep = 0;

    public DepEmployeesController()
    {
        rpEmployee = new RepositoryEmployee();
        rpDepartment = new RepositoryDepartment();
    }

    public ActionResult Index()
    {
        rpDepartment.GetAll();

        rpEmployee.GetAll();
            
        if (Request.Form.GetValues("depSel") == null)
        {
            List<SelectListItem> depList = new List<SelectListItem>();
            SelectListItem sli = null;
            sli = new SelectListItem();
            sli.Value = "";
            sli.Text = "";
            depList.Add(sli);

            foreach (IDepartment d in rpDepartment.RepList)
            {
                sli = new SelectListItem();
                sli.Value = d.DepartmentID.Value.ToString();
                sli.Text = d.Name;
                depList.Add(sli);
            }

            ViewData["depSel"] = new SelectList(depList, "Value", "Text");
            return View(rpEmployee.RepList);
        }
        else
        {
            string selVal = "";
            selVal = Request.Form.GetValues("depSel")[0];

            List<SelectListItem> depList = new List<SelectListItem>();
            SelectListItem sli = null;
            foreach (IDepartment d in rpDepartment.RepList)
            {
                sli = new SelectListItem();
                sli.Value = d.DepartmentID.Value.ToString();
                sli.Text = d.Name;
                depList.Add(sli);
            }

            IDepartment dpSel = rpDepartment.RepList[Convert.ToInt16(selVal) - 1];
            iStaticDep = dpSel.DepartmentID.Value;

            List<IEmployee> empSel = null;
            if (rpEmployee.RepList != null)
            {
                empSel = rpEmployee.RepList.FindAll(
                    (IEmployee emp) => { return emp.DepartmentID.Value == iStaticDep; });
            }

            ViewData["depSel"] = new SelectList(depList, "Value", "Text", selVal);

            return View(empSel);
        }
    }
}

When it runs, all employees are displayed as follows:

When a department is selected, the employees for that department are displayed.

Sort Employee List

Now, let's say you want the object rpEmployee, an instance of the RepositoryEmployee component, to have functionality of sorting employees by last name. Here is what you need to do.

First, you create a comparer class for the sorting as follows:

internal class EmployeeLastnameComparer : IComparer<IEmployee>
{
    public int Compare(IEmployee e1, IEmployee e2)
    {
        return String.Compare(e1.LastName, e2.LastName);
    }
}

Then, call the Dynamic Decorator just before rpEmployee.GetAll() in the action Index, as follows:

rpEmployee = (IRepository<IEmployee>)ObjectProxyFactory.CreateProxy(
    rpEmployee,
    new String[] { "GetAll" },
    null,
    new Decoration((x, y) =>
    {
        object target = x.Target;
        if (target.GetType().ToString() == "ThirdPartyHR.RepositoryEmployee")
        {
            List<IEmployee> emps = ((IRepository<IEmployee>)target).RepList;
            IEnumerable<IEmployee> query = emps.OrderByDescending(emp => emp,
                new EmployeeLastnameComparer()).ToList<IEmployee>();
            ((IRepository<IEmployee>)target).RepList = (List<IEmployee>)query;
        }
    }, null));

That's it. Now, your HRMVC displays employees sorted by their last names. Build it and run it. You will see that the employees are displayed and ordered by their last names as follows:

When you select a department, the employees associated with this department are displayed and ordered by their last names.

Note that a Lambda expression is used to provide an anonymous method for this employee repository object to add sorting capability. Of course, you can use a normal method for the sorting logic. However, since this sorting logic is particularly for the employee repository object rpEmployee and not shared by other objects, it is more concise to keep it in an anonymous method.

There are a few points worth noting here. First, the principle "Extend objects as needed" is followed. When we designed the component RepositoryEmployee, the sorting requirements were not clear yet. By the time we used the object rpEmployee in the application, it was clear that we needed to sort the employee list by employee last names. So, we extended this object to sort the employee list by employee last names. Second, the sorting capability was attached to the object rpEmployee without modifying its component or deriving from it. Third, the object rpEmployee is the one and only one instance of the RepositoryEmployee component which has sorting functionality, independently of other instances created by RepositoryEmployee.

Design Aspects

Say, you want your HRMVC application to address cross-cutting concerns of entering/exiting logging and security checking. By following the principle "Design aspects as global methods in their own modules", these aspects are put in one class SysConcerns as individual public methods and packed in their own module. The following is the code for these concerns.

public class SysConcerns
{
    public static void EnterLog(AspectContext ctx, object[] parameters)
    {
        StackTrace st = new StackTrace(new StackFrame(4, true));
        Console.Write(st.ToString());
            
        IMethodCallMessage method = ctx.CallCtx;
        string str = "Entering " + ctx.Target.GetType().ToString() + "." + method.MethodName +
            "(";
        int i = 0;
        foreach (object o in method.Args)
        {
            if (i > 0)
                str = str + ", ";
            str = str + o.ToString();
        }
        str = str + ")";

        Console.WriteLine(str);
        Console.Out.Flush();

    }

    public static void ExitLog(AspectContext ctx, object[] parameters)
    {
        IMethodCallMessage method = ctx.CallCtx;
        string str = "Exiting " + ctx.Target.GetType().ToString() + "." + method.MethodName +
            "(";
        int i = 0;
        foreach (object o in method.Args)
        {
            if (i > 0)
                str = str + ", ";
            str = str + o.ToString();
        }
        str = str + ")";

        Console.WriteLine(str);
        Console.Out.Flush();
    }

    public static void AdminCheck(AspectContext ctx, object[] parameters)
    {
        Console.WriteLine("Has right to call");
        return;
    }
}

The EnterLog writes entering logs while ExitLog writes exiting logs. The AdminCheck writes a log and returns.

You may need to modify these methods based on your system requirements. You can also enhance them by accessing various information in the context, the target, and the input parameters. To see how the context, target, and input parameters are used to enhance your aspects, please refer to Add Aspects to Object Using Dynamic Decorator.

Use Aspects

With aspects defined, you can add them to objects as needed in the application.

Say you want to add the security checking aspect before calling the GetAll method of the repository object rpDepartment of the component RepositoryDepartment. You also want to add the entering log and exiting log to the same object. You add the following code right before rpDepartment.GetAll() in the action Index.

rpDepartment = (IRepository<IDepartment>)ObjectProxyFactory.CreateProxy(
    rpDepartment,
    new String[] { "GetAll" },
    new Decoration(new DecorationDelegate(SysConcerns.AdminCheck), 
                   new object[] { Thread.CurrentPrincipal }),
                   null);

rpDepartment = (IRepository<IDepartment>)ObjectProxyFactory.CreateProxy(
    rpDepartment,
    new String[] { "GetAll" },
    new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
    new Decoration(new DecorationDelegate(SysConcerns.ExitLog), null));

Then, assume you want to add the entering log and exiting log to the GetAll method of the object rpEmployee of the RepositoryEmployee component. Just insert the following code before rpEmployee.GetAll() in the action Index.

rpEmployee = (IRepository<IEmployee>)ObjectProxyFactory.CreateProxy(
    rpEmployee,
    new String[] { "GetAll" },
    new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
    new Decoration(new DecorationDelegate(SysConcerns.ExitLog), null));

Finally, assume you want to track which department is accessed. You can add the following code in the action Index just before using the department ID property of the selected object dpSel of the Department component iStaticDep = dpSel.DepartmentID.Value.

dpSel = (IDepartment)ObjectProxyFactory.CreateProxy(
    dpSel,
    new String[] { "get_DepartmentID" },
    new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
    null);

Now, the cross-cutting concerns are addressed for the HRMVC application.

Note that the aspects are added to the objects when needed. There is no change in the component classes. And only objects decorated with Dynamic Decorator have the aspects, independent of other objects of the component classes. Also, an aspect can be applied to different objects, of either the same type or different types. For example, SysConcerns.EnterLog is used for rpDepartment (an object of RepositoryDepartment), rpEmployee (an object of RepositoryEmployee), and dpSel (an object of Department).

HRMVC Extended

For your convenience, the HRMVC code, after extending the component and adding the aspects, is listed as follows:

public class DepEmployeesController : Controller
{
    internal class EmployeeLastnameComparer : IComparer<IEmployee>
    {
        public int Compare(IEmployee e1, IEmployee e2)
        {
            return String.Compare(e1.LastName, e2.LastName);
        }
    }

    private IRepository<IEmployee> rpEmployee = null;
    private IRepository<IDepartment> rpDepartment = null;

    private static int iStaticDep = 0;

    public DepEmployeesController()
    {
        rpEmployee = new RepositoryEmployee();
        rpDepartment = new RepositoryDepartment();
    }

    public ActionResult Index()
    {
        rpDepartment = (IRepository<IDepartment>)ObjectProxyFactory.CreateProxy(
            rpDepartment,
            new String[] { "GetAll" },
            new Decoration(new DecorationDelegate(SysConcerns.AdminCheck), 
                           new object[] { Thread.CurrentPrincipal }),
            null);

        rpDepartment = (IRepository<IDepartment>)ObjectProxyFactory.CreateProxy(
            rpDepartment,
            new String[] { "GetAll" },
            new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
            new Decoration(new DecorationDelegate(SysConcerns.ExitLog), null));

        rpDepartment.GetAll();

        rpEmployee = (IRepository<IEmployee>)ObjectProxyFactory.CreateProxy(
            rpEmployee,
            new String[] { "GetAll" },
            null,
            new Decoration((x, y) =>
            {
                object target = x.Target;
                if (target.GetType().ToString() == "ThirdPartyHR.RepositoryEmployee")
                {
                    List<IEmployee> emps = ((IRepository<IEmployee>)target).RepList;
                    IEnumerable<IEmployee> query = emps.OrderByDescending(emp => emp,
                        new EmployeeLastnameComparer()).ToList<IEmployee>();
                    ((IRepository<IEmployee>)target).RepList = (List<IEmployee>)query;
                }
            }, null));

        rpEmployee = (IRepository<IEmployee>)ObjectProxyFactory.CreateProxy(
            rpEmployee,
            new String[] { "GetAll" },
            new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
            new Decoration(new DecorationDelegate(SysConcerns.ExitLog), null));

        rpEmployee.GetAll();

        if (Request.Form.GetValues("depSel") == null)
        {
            List<SelectListItem> depList = new List<SelectListItem>();
            SelectListItem sli = null;
            sli = new SelectListItem();
            sli.Value = "";
            sli.Text = "";
            depList.Add(sli);

            foreach (IDepartment d in rpDepartment.RepList)
            {
                sli = new SelectListItem();
                sli.Value = d.DepartmentID.Value.ToString();
                sli.Text = d.Name;
                depList.Add(sli);
            }

            ViewData["depSel"] = new SelectList(depList, "Value", "Text");
            return View(rpEmployee.RepList);
        }
        else
        {
            string selVal = "";
            selVal = Request.Form.GetValues("depSel")[0];

            List<SelectListItem> depList = new List<SelectListItem>();
            SelectListItem sli = null;
            foreach (IDepartment d in rpDepartment.RepList)
            {
                sli = new SelectListItem();
                sli.Value = d.DepartmentID.Value.ToString();
                sli.Text = d.Name;
                depList.Add(sli);
            }

            IDepartment dpSel = rpDepartment.RepList[Convert.ToInt16(selVal) - 1];
            dpSel = (IDepartment)ObjectProxyFactory.CreateProxy(
                dpSel,
                new String[] { "get_DepartmentID" },
                new Decoration(new DecorationDelegate(SysConcerns.EnterLog), null),
                null);

            iStaticDep = dpSel.DepartmentID.Value;

            List<IEmployee> empSel = null;
            if (rpEmployee.RepList != null)
            {
                empSel = rpEmployee.RepList.FindAll(
                    (IEmployee emp) => { return emp.DepartmentID.Value == iStaticDep; });
            }

            ViewData["depSel"] = new SelectList(depList, "Value", "Text", selVal);

            return View(empSel);
        }
    }
}

One thing to notice is that the object returned by ObjectProxyFactory.CreateProxy is assigned back to the variable originally pointed to the target. For example, rpEmployee originally was assigned an object of RepositoryEmployee - the target. After calling ObjectProxyFactory.CreateProxy, it is assigned the returned object, which is a proxy of the target. It is subtle but important. The returned object of ObjectProxyFactory.CreateProxy is a proxy of the target. By using the same variable for both the target and its proxy, the original code is intact. That means that the target and its proxy are interchangeable. If the variable is pointed to the target, the target is used as is. If the variable is pointed to the proxy of the target, additional functionality is executed before or after the target is used. Actually, if you remove all the code that calls ObjectProxyFactory.CreateProxy, you get the original code before you extended the object and added the aspects to objects.

Last, before running the application, you need to modify the Global.asax method to redirect console output to a file hrlog.txt. These modifications are for this application only since the entering/exiting logging aspects use the console. Your application may use different logging mechanismd. In that case, you may need to make the corresponding changes. The modified application class is listed as follows.

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "DepEmployees", action = "Index", 
            id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        //StreamWriter swLog = null;
        FileStream fileStream = null;

        string path = Path.GetDirectoryName(Server.MapPath("~"));
        if (!File.Exists(path + "\\hrlog.txt"))
        {
            fileStream = new FileStream(path + "\\hrlog.txt", FileMode.Create);
        }
        else
            fileStream = new FileStream(path + "\\hrlog.txt", FileMode.Truncate);

        TextWriter tmp = Console.Out;
        Application["origOut"] = tmp;

        StreamWriter sw1 = new StreamWriter(fileStream);
        Console.SetOut(sw1);

        Application["logStream"] = sw1;
    }

    protected void Application_End(object sender, EventArgs e)
    {
        TextWriter origStrm = (TextWriter)Application["origOut"];
        Console.SetOut(origStrm);

        StreamWriter tmp = (StreamWriter)Application["logStream"];
        Stream fileStream = tmp.BaseStream;

        tmp.Close();
        fileStream.Close();
    }
}

When the application runs, you will see the following output in the file hrlog.txt.

   at HRMVCExtended.Controllers.DepEmployeesController.Index() in 
      C:\CBDDynDecoratorMVC\HRMVCExtended\Controllers\DepEmployeesController.cs:line 57
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
   at HRMVCExtended.Controllers.DepEmployeesController.Index() in 
      C:\CBDDynDecoratorMVC\HRMVCExtended\Controllers\DepEmployeesController.cs:line 82
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
   at HRMVCExtended.Controllers.DepEmployeesController.Index() in 
      C:\CBDDynDecoratorMVC\HRMVCExtended\Controllers\DepEmployeesController.cs:line 57
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
   at HRMVCExtended.Controllers.DepEmployeesController.Index() in 
      C:\CBDDynDecoratorMVC\HRMVCExtended\Controllers\DepEmployeesController.cs:line 82
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
   at HRMVCExtended.Controllers.DepEmployeesController.Index() in 
      C:\CBDDynDecoratorMVC\HRMVCExtended\Controllers\DepEmployeesController.cs:line 126
Entering ThirdPartyHR.Department.get_DepartmentID()

In the source code download, the project HRMVC contains the initial code prior to extending the component and adding aspects. The project HRMVCExtended contains the code after extending the component and adding aspects.

Points of Interest

The philosophy of "Add aspects to objects as needed" and "Extend objects as needed" is applied to an ASP.NET MVC application. ASP.NET MVC developers may find the following principles useful for addressing the common tasks with Dynamic Decorator:

  • Design components to meet business requirements in a general way
  • Design aspects as global methods in their own modules
  • Add aspects to objects as needed
  • Extend objects as needed

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here