In this article we are going to discuss the Proxy design pattern. It is one
among the 23 design patterns by Gof. As usual we can start with the Challenge
and Solution style.
Challenge
You are working on automating Excel COM objects. The business logic has to think
too much about instantiating COM, doing ground works before calling the actual
functionalities etc.
The same code could get replaced by DCOM tomorrow. So the Excel access code is
spread throughout your application. How to do a better design?
Definition
"Provide a surrogate or placeholder for another object to control access to it."
Implementation
We can solve the above problem by using a Proxy pattern. As the definition says,
we will have to create a placeholder or wrapper around the original object to
control access to it. In this way we can make the following advantages:
- Talk to COM or DCOM Excel object by
changing configuration in one place
- Give application a simple interface to
talk with Excel COM/DCOM
- Feel application think like it is talking
to a local object
Application
Following is the application with data:
Old Code
Following is the old code where application is forced to think too much about
the Excel COM object and method of assigning values to the cells.
//Start
Excel and get Application object.
Microsoft.Office.Interop.Excel.Application
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
//Get a new
workbook.
Microsoft.Office.Interop.Excel._Workbook oWB
= (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
Microsoft.Office.Interop.Excel._Worksheet
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
//Add table
headers going cell by cell.
oSheet.Cells[1,
1] = "Name";
oSheet.Cells[1, 2] = "Address";
oSheet.Cells[1, 3] = "Salary";
//Format A1:D1
as bold, vertical alignment = center.
oSheet.get_Range("A1",
"C1").Font.Bold =
true;
oSheet.get_Range("A1",
"C1").VerticalAlignment =
Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
int
i = 2;
foreach (Employee
employee in _list)
{
string[] values =
new string[3];
values[0] = employee.Name;
values[1] = employee.Address;
values[2] = employee.Salary.ToString();
oSheet.get_Range("A"
+ i.ToString(), "C" + i.ToString()).Value =
values;
i++;
}
oXL.Visible =
true;
oXL.UserControl = true;
The problem with above code is too much Excel logic is mixed with the
application logic. Now we can see the new code with proxy pattern implemented.
New Code with Proxy Pattern
ExcelProxy proxy
= new ExcelProxy();
proxy.Save(_list);
The code is only 2 lines and the Excel COM object creation, cell value assigning
etc are taken care by the ExcelProxy class.
Application Execution
On executing the application we can see the results inside Microsoft Excel.
Other Examples of Proxy Pattern
We can have many real world examples which implement the Proxy pattern. When we
add a WCF reference a Proxy is created. This class takes care of the connection
details, serialization etc.
Summary
In this article we have seen the usage of Proxy design pattern along with an
example. The source code attached contains the example we have discussed.