Common Design Approach For Generic Data Access Layer?
- Download source (Revision: 16thursday Baronial, 2013) - 49.6 KB
Introduction
A data admission layer tin be an important part of a software awarding. Applications nigh always need access to information and we should pattern a separate layer for this purpose. Now we want to use the generics in C# and pattern a really powerful data admission layer for use in all applications without requiring whatever changes. For more information about Generics in C#, you tin follow one of these links:
- Link I: An Introduction to C# Generics
- Link II: Introduction to Generics (C# Programming Guide)
Of class, it should be noted that all of these ideas have been implemented in the Entity Framework in the all-time possible way, but the problem is that the Entity Framework an non to be used with the some DBMS same as MS Access. And the other hand is aimed at showing the power of generics and facilities in programming.
Groundwork
After reading the electric current version of this article, yous can read the new version from here. In the new version, I solved some problems which are the same as SQL injection and multiple database support.
Data Admission Layer Overview
In this article, we introduce our data admission layer architect (follow the below picture show). As you run across in the picture, nosotros have two top level namespaces, Model
and DataAccessHandler
, Model
is a class view of database and DataAccessHandler
is a wrapper for the ADO.Internet to the building communication with DBMS.
Model
Each table in our database should have an implemented class in Model
with the same field names and field data types. Pay attention that each form must inherit from IEntity
(meet the beneath flick). For example in this sample, Personnel
is a grade view of tblPersonnel
in database and it is inherited from IEntity
.
Context
Context
is implemented with singleton blueprint pattern as a model container, using this container to insert, update and delete the entities makes Collections
synchronized with the database. Thus the changes are not required to update the Collections once again and it tin reduce a toll of data access layer. Insert
, Update
and Delete
methods in IEntity
are implemented to consider this requirement. Delight pay attention to the following codes:
public void AddPersonel(Mode.Personnel personnel) { Mode.Context.Example.Personnel.Insert(personnel); } public void UpadtePersonel(Manner.Personnel personnel) { Manner.Context.Example.Personnel.Update(personnel); } public void RemovePersonel(Mode.Personnel personnel) { Mode.Context.Instance.Personnel.Delete(personnel); }
DataAccessHandler
In this projection, our peak level proper name space is DataAccessHandler
and so this container has DataAccessAdapter
, IEntity
, Entities
, SqlCommand
enumerator and PrimaryKey
data-structure.
Class | Description | Namespace |
IEntity | It is an interface for our entities in entity model. For example, if nosotros want the use of this DAL in our Personnel Management Organisation, the personnel object is a target entity that should implement this interface. Each entity contains a method to insert and update and remove a collection of its own, if information technology will be inherited from IEntity . | DataAccessHandler |
Entities | Information technology is a collection of entities, this is a generic list and can be really useful for u.s., then each entity has its own list. | DataAccessHandler.Collection |
PrimaryKey | This property is used to specify principal key for each entity. information technology is a construction with two chemical element Proper noun and Value , Proper name is a chief central proper noun and value is a primary key value | DataAccessHandler.IEntity |
DataAccessAdapter | Information technology is a wrapper to the working with ADO.Net objects, then we expect this object to communicate with our database. (Execute the sql commands same equally insert , update , delete and select on the target database). This object is to do your job that requires a sqlcommand enumerator. | DataAccessHandler |
SqlCommand | This is an enumerator object and is used to specify our DAL SQL control to transact with database. | DataAccessHandler |
DataAccessAdapter Backbone Methods
Initialize Method
This method is used to initialize DataAccessAdapter
connectedness cord and password for connecting to the database. We used this method only in one case.
static void Chief(string[] args) { #region Initialize DataAccessAdapter string datasource = @" ../../../Testdb.mdb"; cord countersign = " "; DataAccessHandler.DataAccessAdapter.Initialize(datasource, countersign);
0 Response to "Common Design Approach For Generic Data Access Layer?"
Post a Comment