|
|
6 Aug 2007
|
Facade
Recently, I was asked what is the best method of retrieving data from database. My answer is: it depends. If you want quick and easy method, you can use TableAdapter (for .Net 2.0) at the presentation layer. However, I normally use facade to retrieve data from and write data to database.
Facade can act as a middleman between presentation layer and data access layer. For example: we have two interfaces: ICustomer (with definition such as GetCustomer, UpdateCustomer, etc) and IOrder (with defintion such as GetOrder, UpdateOrder, etc); we have two methods: SQLCustomerMethod, SQLOrderMethod which implement ICustomer and IOrder respectively. My Facade will look like this:
Public NotInheritable Class Facade
Public Shared Function CustomerMethod() As ICustomer
Return new SQLCustomerMethod(g_cnnStr)
End Function
Public Shared Function OrderMethod() As IOrder
Return New SQLOrderMethod(g_cnnStr)
End Function
End Class
g_cnnStr is the connection string which can be accessed from the presentation layer, e.g. Web.config. The above arrangement allows the presentation layer programmer to get customer by a simple command (facade.CustomerMethod.GetCustomer). If there is changes in the data layer, front end programmer does not need to change a single piece of code. If there is a major change in the back end database, e.g. change from SQL Server to Oracle Server, you can easily change SQLCustomerMethod and SQLOrderMethod to OracleCustomerMethod and OracleOrderMethod (of course, these new method need to implement ICustomer and IOrder respectively); after you change the facade to use Oracle methods, everything is done.
|