Petshop 4.0 data access layer design what I see

Source: Internet
Author: User

I personally think that the data access layer should be more streamlined in design.

The existing framework is a dal, corresponding to a helper (we can understand it as a real data engine ). The implementation of this method is really simple, but there are duplicates. For each type of Dal, the difference is only helper, and Other implementations are basically the same.

A feasible refactoring is to implement a real helper class and encapsulate different data engines as a dalengine. For some simple code implementation, see the following:
Public interface idalengine
{
Int executenonquery (commandtype parameter type, string parameter text, ilist <iparameter> parameters );
Object executenonscalar (commandtype parameter type, string parameter text, ilist <iparameter> parameters );
Idatareader executereader (commandtype primitive type, string plain text, ilist <iparameter> parameters );
}

Public abstract class dalhelper
{
Public static int executenonquery (idalengine engine, commandtype parameter type, string parameter text, ilist <iparameter> parameters)
{
Return engine. executenonquery (partition type, plain text, parameters );
}

Public static object executenonscalar (idalengine engine, commandtype parameter type, string parameter text, ilist <iparameter> parameters)
{
Return engine. executenonscalar (parameter type, parameter text, parameters );
}

Public static idatareader executereader (idalengine engine, commandtype primitive type, string plain text, ilist <iparameter> parameters)
{
Return engine. executereader (struct type, plain text, parameters );
}
}

Public class sqldalengine: idalengine
{
Private sqlconnection conn;
Public sqldalengine (string connstring)
{
Conn = new sqlconnection (connstring );
If (conn. State! = Connectionstate. open)
{
Conn. open ();
}
}
Int executenonquery (commandtype parameter type, string parameter text, ilist <iparameter> parameters)
{
If (conn. State! = Connectionstate. open)
{
// Throw exception: connection is closed!
}

Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = conn;
Cmd. commandtype = primitive type;
Cmd. commandtext = plain text;

If (parameters! = NULL)
{
Foreach (iparameter Param in parameters)
{
Cmd. Parameters. Add (PARAM); // some conversions may be required. Here is just a pseudo code
}
}

Int result = cmd. executenonquery ();

Return result;
}

Object executenonscalar (commandtype parameter type, string parameter text, ilist <iparameter> parameters)
{
// Reference implementation such as executenonquery
}

Idatareader executereader (commandtype primitive type, string plain text, ilist <iparameter> parameters)
{
// Reference implementation such as executenonquery
}
}

It seems that this is a simple conversion, but it is definitely a major improvement in the case of a large system with a large number of tables.

In addition, the original data access layer framework is not easy to test if data is not ready. With this improvement, we can isolate data access and data access engines, development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.