Use properties and reflection transition from data access layer to business objects - III

xiaoxiao2021-03-06  70

Introduction

The last article of this series. How to describe the first part, the second part has been described. Now we have to create a DAL library to make our headline feasible.

Design DAL library

I want to create class libraries support SQL Server and OLEDB. I divide the library into the following sections:

Utility Classes

Class DalQueryBuilder

Generate a SQL statement update item.

Class Dalparameter

The generated parameter is saved in the stored procedure.

Class DalException

Inherited in System.Exception, more information will be provided when there is an exception.

Attribute Classes

See the first one.

DAL itself

Class Dalengine

This abstract class is used for database operations, which is the database program easier. Its virtual and abstract methods have different implementations.

Class Dalsqlengine

Class DaloledBenngine

耧 一 一 一 眼 d 类

Public Abstract Class Dalengine: IDisposable

{

//

// Private Data MEMBERS

//

IDBConnection conn = NULL;

String connectionString = ""

ArrayList parameters = new arraylist ();

Bool caclose = true;

// conncture

Public Dalengine (String ConnectionString);

Public Bool CANClose;

Public String Connectionstring;

Protected IDBConnection Connection;

Protected ArrayList Parameters;

Public void close ();

Public void dispose ();

//

//Methods That Must Be Override with a specific Data Province

// Implementation please See the importation of dalsqlengine

// or DaloledBengine

//

Protected Abstract IDBConnection getConnection ();

Protected Abstract IDBCommand CreateCommand (String SPName);

Public abstract void execSp_dataset (String SpName, Dataset DataSet,

String Tablename);

Public abstract void execquuery_dataset (String Query, DataSet DataSet,

String Tablename);

//

// related to stored procedure parameters

//

Public Dalparameter getParameter (String name);

Void UpdateOutputParameters (IDBCommand CMD);

Public Void AddParameter (Dalparameter Param);

Public void cleanparameters ();

//

// for those That use stored procedures

//

Public iDataReader EXECSP_DATAREADER (STRING SPNAME);

Public iDataReader EXECSP_DATAREADER (String SPNAME,

Commandbehavior behavior;

Public Object Execsp_scalar (string spname); public int execsp_nonquery; SPNAME;

//

//Methods for those That Use Plain SQL Statements

//

Public iDataReader EXECQUERY_DATAREADER (String Query,

Commandbehavior behavior;

Public iDataReader EXECQUERY_DATAREADER (String Query);

Public Object ExecQuery_scalar (String Query);

Public int execQuery_nonquery (String query);

//

// Business Objects Methods

//

Public Static Object CreateFromReader (iDataReader Reader, Type Objtype);

Public Object RetrieveObject (Object KeyValue, Type ObjType);

Public int RETRIEVECHILDOBJECTS (Object ForeignKeyValue, ArrayList Objects,

Type childType;

Void UpdateObjectsql (Object O, DataTableAttribute DataTable);

Void UpdateObjectStRedProcedure (Object O, DataTableAttribute DataTable);

Public Void UpdateObject (Object O);

Public Void UpdateObjects (IENUMERABLE ENUMOBJECTS)

}

Public Class Dal: Dalsqlengine

{

Const string conn_string = "server = localhost; uid = sa; pwd =; database = pubs";

Public Dal (): Base (conn_string)

{

}

Public ArrayList GetCustomerDependents (Customer Customer)

{

ArrayList Result = new arraylist ();

RetrievechildObjects (Customer.ID, Result, TypeOf (CustomerDepend));

Return Result;

}

Public Void UpdateCustomerDependents (Customer Customer)

{

UpdateObjects (Customer.Dependents);

}

}

Watch an example:

Public static void main ()

{

Dal Dal = new dal ();

Try

{

Contact Contact = new contact ();

Contact.name = "Joao Cardoso";

Contact.age = 23;

Contact.address = "AV. RIO BRANCO, 202/121";

Contact.address2 = "centro";

Contact.postalcode = "09029-901";

Contact.city = "sao paulo";

Contact.State = "sp";

Contact.country = "Brazil";

Dal.UpdateObject (Contact);

Console.writeLine (Contact);

Contact Joaocardoso = (Contact) Dal.RetrieveObject (1, Typeof (Contact));

JOAOCARDOSo.age ;

Console.writeLine (Joaocardoso);

Console.writeline ("");

Customer Customer = New Customer ();

Customer.name = "Paul Noyter";

Customer.Age = 34;

Customer.address = "All ST, 2202/2121";

Customer.Address2 = "Downville";

Customer.Postalcode = "90931";

Customer.city = "los angeles";

Customer.State = "CA";

Customer.country = "United States";

Customer.totalpurchaseD = 1900.87M;

Customer.NumberOfPurchases ;

Dal.UpdateObject (Customer);

Customer Paul = (Customer) Dal.RetrieveObject (1, Typeof (Customer));

Console.writeLine (Paul);

Paul.TotalpurchaseD = 100m;

Paul.Numberofpurchases ;

Dal.UpdateObject (Paul);

IF (paul.dependents.count == 0)

{

Customerdependent Dependent = paul.newdepend ();

Dependent.name = "marie noter";

Dependent.age = 31;

Paul.dependents.Add (dependent);

Dependent = paul.newdependent ();

Dependent.name = "mark noyter";

Dependent.age = 10;

Paul.dependents.Add (dependent);

Dependent = paul.newdependent ();

Dependent.name = "Claudia Snorg";

Dependent.age = 32;

Dependent.ReranceShip = CustomerReranceShip.Friend;

Paul.dependents.Add (dependent);

Dal.UpdateCustomerDependents (Paul);

}

Else

{

Console.WriteLine ("Dependents of {0}", Paul.name);

Moreach (CustomerDependent Dependent in Paul.dependents)

{

Console.writeline (" {0} - {1} [{2}]", dependent.id, dependent.name, defendent.relationship;

Dependent.ReranceShip = CustomerReranceShip.FAMILY;

}

Dal.UpdateCustomerDependents (Paul);

}

}

Finally

{

Dal.dispose ();

}

}

Conclusion

There are older limitations, we need to further implement thinking, but you understand these, we can make NHibernate's understanding and application, I wish you good luck.

转载请注明原文地址:https://www.9cbs.com/read-90891.html

New Post(0)