Introduction
As discussed in the 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 in 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 development of these common tasks. The Dynamic Decorator is used to address these tasks by following these guidelines. A WinForm 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 application - by using the Dynamic Decorator.
Choose Application Type
There are several application types in the .NET world: WinForm, ASP.NET, ASP.NET MVC and Silverlight. WinForm application offers rich user interactions and client processing power. ASP.NET application has advantages of deployment and maintenance via the Web. ASP.NET MVC offers testability plus advantages of ASP.NET. Silverlight application offers 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 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 the 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 application by using the Dynamic Decorator.
The same problem discussed in the Components, Aspects and Dynamic Decorator as example is used here. This time, we choose ASP.NET application as the application type instead of the WinForm application. For convenience, I state the problem again as follows.
Problem
Displays employees based on 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 as follows:
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 kinds of sorting to support for this component at this time. In applications, an object of the component may need to sort by last name. Another may need to sort by birthday. A third may not need to sort 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.
HRSite
HRSite
is an ASP.NET application, which uses the above components to display employees based on selection of a department. Since it is an ASP.NET application, it follows ASP.NET's application programming model and event model. The code is listed as follows:
public partial class DepEmp : System.Web.UI.Page
{
private IRepository<IEmployee> rpEmployee = null;
private IRepository<IDepartment> rpDepartment = null;
private int selectedDepId = 0;
private static int iStaticDep = 0;
public DepEmp()
{
iStaticDep = 0;
rpEmployee = new RepositoryEmployee();
rpDepartment = new RepositoryDepartment();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (DropDownList1.Items[0].Value == "")
selectedDepId = DropDownList1.SelectedIndex - 1;
else
selectedDepId = DropDownList1.SelectedIndex;
}
try
{
rpDepartment.GetAll();
DropDownList1.DataSource = rpDepartment.RepList;
DropDownList1.DataValueField = "DepartmentID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();
if (!IsPostBack)
DropDownList1.Items.Insert(0, new ListItem(""));
else
DropDownList1.SelectedIndex = selectedDepId;
rpEmployee.GetAll();
GridView1.DataSource = rpEmployee.RepList;
GridView1.DataBind();
}
catch (Exception ex)
{
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
IDepartment dpSel = rpDepartment.RepList[selectedDepId];
iStaticDep = dpSel.DepartmentID.Value;
List<IEmployee> empSel = null;
if (rpEmployee.RepList != null)
{
empSel = rpEmployee.RepList.FindAll(
(IEmployee emp) => { return emp.DepartmentID.Value == iStaticDep; });
}
GridView1.DataSource = empSel;
GridView1.DataBind();
}
}
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 the rpEmployee.GetAll()
in the event handler Page_Load
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 HRSite 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 need to sort the employee list by employee's last name. So, we extended this object to sort employee list by employee's last name. 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 the only one instance of RepositoryEmployee
component which has sorting functionality, independently of other instances created by RepositoryEmployee
.
Design Aspects
Say, you want your HRSite
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 parameters are used to enhance your aspects, please go 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 the rpDepartment.GetAll()
in the page loading event handler Page_Load
.
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 entering log and exiting log to the GetAll
method of the object rpEmployee
of RepositoryEmployee
component, just insert the following code before the rpEmployee.GetAll()
in the page loading event handler Page_Load
.
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 department selection event handler DropDownList1_SelectedIndexChanged
just before using the department ID property of 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 HRSite
application.
Note that the aspects are added to the objects when needed. There is no change in the component classes. And only the 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, the SysConcerns.EnterLog
is used to for rpDepartment
(an object of RepositoryDepartment
), rpEmployee
(an object of RepositoryEmployee
) and dpSel
(an object of Department
).
HRSite Extended
For your convenience, the HRSite
code after extending the component and adding the aspects is listed as follows:
public partial class DepEmp : System.Web.UI.Page
{
internal class EmployeeAgeComparer : IComparer<IEmployee>
{
public int Compare(IEmployee e1, IEmployee e2)
{
return DateTime.Compare(e1.DateOfBirth, e2.DateOfBirth);
}
}
private IRepository<IEmployee> rpEmployee = null;
private IRepository<IDepartment> rpDepartment = null;
private int selectedDepId = 0;
private static int iStaticDep = 0;
public DepEmp()
{
iStaticDep = 0;
rpEmployee = new RepositoryEmployee();
rpDepartment = new RepositoryDepartment();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (DropDownList1.Items[0].Value == "")
selectedDepId = DropDownList1.SelectedIndex - 1;
else
selectedDepId = DropDownList1.SelectedIndex;
}
try
{
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();
DropDownList1.DataSource = rpDepartment.RepList;
DropDownList1.DataValueField = "DepartmentID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();
if (!IsPostBack)
DropDownList1.Items.Insert(0, new ListItem(""));
else
DropDownList1.SelectedIndex = selectedDepId;
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();
GridView1.DataSource = rpEmployee.RepList;
GridView1.DataBind();
}
catch (Exception ex)
{
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
IDepartment dpSel = rpDepartment.RepList[selectedDepId];
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; });
}
GridView1.DataSource = empSel;
GridView1.DataBind();
}
}
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 a different logging mechanism. In that case, you may need to make corresponding changes.
void Application_Start(object sender, EventArgs e)
{
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;
}
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 DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 70
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 104
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e) in
c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 70
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 104
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
at DepEmp.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 123
Entering ThirdPartyHR.Department.get_DepartmentID()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 70
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 104
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 70
Entering ThirdPartyHR.RepositoryDepartment.GetAll()
Has right to call
Exiting ThirdPartyHR.RepositoryDepartment.GetAll()
at DepEmp.Page_Load(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 104
Entering ThirdPartyHR.RepositoryEmployee.GetAll()
Exiting ThirdPartyHR.RepositoryEmployee.GetAll()
at DepEmp.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
in c:\CBDDynDecoratorSite\HRSiteExtended\DepEmp.aspx.cs:line 123
Entering ThirdPartyHR.Department.get_DepartmentID()
In the source code download, the project HRSite
contains the initial code prior to extending the component and adding aspects. The project HRSiteExtended
contains the code after extending the component and adding aspects.
The application was debugged in Visual Studio 2010 Default Web Server. If you plan to host the applications in IIS, you may need to turn on Windows Integrated Security and give ASPNET account sufficient right to test them.
Points of Interest
The philosophy of "Add aspects to objects as needed" and "Extend objects as needed" is applied to ASP.NET application. The ASP.NET developers may find the following principles useful for addressing the common tasks with the 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