Tuesday, January 15, 2008

.Net Design patterns

Design patterns are recurring sequences, built-in .Net patterns are scattered over the framework.

In practice following standard software patterns will help to achieve a more standard code, that is manageable, and understandable by a bigger set of software programmers/developers and architects.

For example the data adapter factory, that enables the creation of specific DBMS adapters. This centralized class helps in establishing a strong data layer, that supplies a more generic way to communicate with business layer.

For example:

Imports System.Data.Common

dbPFactory = DbProviderFactories.GetFactory(“FactoryName”)
adapter = dbPFactory.CreateDataAdapter()

In simple technical english, a factory pattern is responsible for managing and creating product class instances from a base class that has increasing or more than one number of child classes. The DbProviderFactories is a factory of factories, that manages the dbms adapters in .Net 2.0. Building a class wrapper over this architecture and supplying the correct parameters will enable the direct generation of specified data adapters to communicate with most of the standard DBMS in the market.

Singleton pattern is implemented in many places in the .net framework, single instance window applications, also in remoting we deal with singleton classes.

A singleton pattern can be achieved by forbidding the creation of direct class instance, using the constructors. This is done by making constructors private, and preventing the user from creating an instance of the class.

This enables only the creation of instances from class scope. To enable single instance in memory we use static or shared reference to the class, we make it private to prevent public access, and we set it to NULL.

We add a static or shared function that checks if the class static reference is NULL, if NULL an instance is created, otherwise the previous instance is returned.

The .Net frame makes good use of the proxy pattern , that is a mediator between the real object and the client object. When applying proxy design pattern, the proxy class provides the same services as the real object. That is they inherit from same base class, or apply the same interface. Proxy classes are used in remoting, and when communicating with services.

Such patterns define the extensions road map for the framework, using these patterns will allow your application benefit a great deal, for extendability and code maintenance.

This was an introduction to .Net Design patterns, I an example of using DBFactory to create a generic adapter can be downloaded using the following link http://www.codeproject.com/KB/dotnet/dotNetDesignPatterns_.aspx.

Regards
Rabeeh Abla

No comments: