Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Visual Basic

.NET Design Patterns

Rate me:
Please Sign up or sign in to vote.
3.87/5 (10 votes)
12 Jan 2009CPOL2 min read 111.2K   2.7K   51   1
Design patterns are recurring sequences, built-in .NET patterns are scattered over the framework.

Introduction

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

In practice, following standard software patterns will help to achieve 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:

VB.NET
Imports System.Data.Common

dbPFactory = DbProviderFactories.GetFactory("FactoryName")
adapter = dbPFactory.CreateDataAdapter() 

UML design of generic Adapter class that makes use of 'DbProviderFactories' factory, and DbProviderFactory, to create a generic layer.

 Image 1

Figure 1.

In simple technical English, a factory pattern is responsible for managing and creating product class instances from a base class that has an increasing or more than one child class. 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 DBMSs in the market.

The following is a code example of creating an adapter to connect to Access database:

C#
public DataTable GetData()
{
    GenericAdapter.GenericAdapter genericAdapter = null;
    ConnectionStringInfo conInfo = new ConnectionStringInfo();
    DataTable table = null;
    //Supplying parameters to connect to Access db
    conInfo.DataBaseFilePath = @"c:\db.mdb";
    conInfo.ProviderName = "Microsoft.Jet.OLEDB.4.0";
    conInfo.DataSource = "{FilePath}";
    genericAdapter = new GenericAdapter.GenericAdapter("System.Data.OleDb");
    if (genericAdapter.SetConnectionString(conInfo))
    {
        table = genericAdapter.ExecuteTableCommand("MyTable"); //return table data
    }
    return table;
}

Singleton pattern is implemented in many places in the .NET Framework, single instance window applications. We also deal with singleton classes in remoting.

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 Framework 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 the same base class, or apply the same interface. Proxy classes are used in remoting and when communicating with services.

Such patterns define the road map for extending a framework. Using these patterns will allow your applications to benefit a great deal, in extendability and code maintenance.

This was an introduction to .NET design patterns. An example of using DBFactory to create a generic adapter can be downloaded from this page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CSP Solutions
Lebanon Lebanon
Rabeeh Abla is a Lebanese entrepreneur, who studied computer science in Lebanon at the University of Balamand, and worked there for two years in research related to artificial intelligence. After that, he worked for six years in software development at a Lebanese US based company where he held the position of a software architect.

In 2009 Rabeeh founded CSP Solution as his first and core business. CSP Solutions is founded on the idea to put optimization within clients reach, by providing innovative software solutions that save time and money.

In 2010 Rabeeh won the Berytech incubation award.

In 2011 Rabeeh was introduced to Mowgli through Berytech, were he was selected as one of Mowgli entrepreneurs.

In August 2013 one of the innovative products that CSP developed for its clients was featured as app of the month at Nuance (Speech Recognition world leader). And there are currently less than five speech recognition solutions similar to it in the world. Thus, CSP as a Lebanese company is very proud by its client’s success stories, and achievements.

CSP idea of success is to develop and provide high quality innovative software solutions that saves time and money, and consequently put optimization within clients reach, this is CSP slogan that is embedded in the company culture, software products and service, everything revolves around optimization in CSP, it is the core foundation of the business.

In mid 2014 Rabeeh started another company under CSP Solutions, and named it CSP Healthcare, with one motive to improve patient care. By providing Electronic Medical Records for clinics and hospitals, cloud based and on premise deployment. He claims “Our focus is to help healthcare institutions in Lebanon, MENA and USA in improving patient care. We are currently seven employees in Lebanon and a team of twenty developers in India, we are growing everyday and looking to scale CSP offices.”

Comments and Discussions

 
Generalgood article Pin
Donsw17-Feb-09 13:27
Donsw17-Feb-09 13:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.