.net Pet Shop Design Pattern in Practice


In Software Engineering, a Design Pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

In this Topic we will discuss one the most commonly used design pattern. it’s Pet Shop Design Pattern with Example  at the end of story!.

Let Me Go Direct to our layers ,Pet Shop contains the  following layers:

  1. 1. IDAL.Product
  2. 2. SQLServerDAL.Product   or   OracleDAL.Product
  3. 3. BLL.Product
  4. 4. DALFactory.Product

  1. IDAL.Product

This module contains All Contract and Interfaces that we will use it or Implement it through any project

Hint: Always name your contract or interface with prefix I like this    IEmployee

Ex:

using System;

using System.Collections.Generic;

using System.Text;

using Product.BLL;

namespace Product.IDAL

{

public interface IEmployee

{

#region IEmployee Members

/// <summary>

/// add new Employee

/// </summary>

/// <param name=”employee”></param>

/// <returns></returns>

Guid Add(EmployeeInfo employee);

/// <summary>

/// Edit Existing Employee

/// </summary>

/// <param name=”employee”></param>

void Edit(EmployeeInfo employee);

#endregion

}

}

Yellow Highlights:

Product.BLL   namespace for entities and info classes

EmployeeInfo  class that contain all employee details “Properties” only

As we see in the previous code snippet an interface called IEmployee contains only methods without code body to implement it in the near future!

As well we can many interfaces as the system needs IDepartment, IAction ….etc

  1. 2. SQLServerDAL.Product

This module contains classes that Inherits from Product.IDAL and Implement it with direct dealing with database

Ex:

using System;

using System.Collections.Generic;

using System.Text;

using Product.IDAL;

using DocumentCycle.BLL;

using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

namespace Product.SQLServerDAL

{

public class Employee:IEmployee

{

#region IEmployee Members

/// <summary>

/// Add New Employee

/// </summary>

/// <param name=”employee”>EmployeeInfo</param>

/// <returns>Guid</returns>

public Guid Add(EmployeeInfo employee)

{

return new Guid(SqlHelper.ExecuteScalar(Globals.connectionString,                                                              “[Product].[AddEmployee]”, employee.FullName,employee.Email,employee.Mobile).ToString());

}

/// <summary>

/// Edit Existing Employee

/// </summary>

/// <param name=”employee”>EmployeeInfo</param>

/// <returns></returns>

public void Edit(EmployeeInfo employee)

{

return new Guid(SqlHelper.ExecuteScalar(Globals.connectionString,                                                              “[Product].[AddEmployee]”, employee.FullName,employee.Email,employee.Mobile).ToString());

}

}

}

As we see in the previous code snippet Class Employee Inherit from IEmployee and Implement it’s members

AddEmploye, EditEmployee

  1. 3. BLL.Product

This module contains all business objects and Info Classes if we applied this on our example

We will find  two classes

  • Employee with namespace BLL.Product
  • EmployeeInfo

If we take a look on Employee Class

using System;

using System.Collections.Generic;

using System.Text;

using Product.IDAL;

using Product.DALFactory;

namespace Product.BLL

{

public class Employee

{

private static readonly IEmployee dal =        DataAccess.CreateProductInstance<IEmployee>(“Employee”);

#region Singleton

private static readonly Employee employee = new Employee();

/// <summary>

/// Singleton instance of Employee component.

/// </summary>

public static Employee Instance

{

get

{

return employee;

}

}

#endregion

#region IEmployee Members

/// <summary>

/// Add New Employee

/// </summary>

/// <param name=”employee”>EmployeeInfo</param>

/// <returns>Guid</returns>

public  Guid Add(EmployeeInfo employee)

{

return dal.Add(employee);

}

/// <summary>

/// Edit Existing Employee

/// </summary>

/// <param name=”employee”>EmployeeInfo</param>

/// <returns></returns>

public  void Edit(EmployeeInfo employee)

{

dal.Edit(employee);

}

}

}

Yellow Highlights:

First one :

It’s create a new instance class employee

Second One:

Singleton: used to guarantee that we use only one object of class Employee

Third One: use the DataAccess object to call actual implementation methods

  1. 4. DALFactory.Product

This module contains only a Factory Class to create instances from all system classes as the following snippet :

using System;

using System.Collections.Generic;

using System.Web;

using System.Configuration;

using System.Reflection;

using Product.IDAL;

namespace Product.DALFactory

{

public sealed class DataAccess

{

#region Private Proparites

private static readonly string DbType = ConfigurationManager.AppSettings[“Product.DBType”];

#endregion

/// <summary>

/// Constractor

/// </summary>

private DataAccess() { }

/// <summary>

/// Create Instance By the Class type and Class Path(Name).

/// </summary>

/// <typeparam name=”T”></typeparam>

/// <param name=”classPath”></param>

/// <returns></returns>

public static T CreateProductInstance<T>(string classPath)

{

string className = String.Format(“Product.{0}.{1}”, DbType,                                                                                 classPath);

return (T)Assembly.Load(String.Format(“Product.{0}”,                                                                 DbType)).CreateInstance(className);

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s