Using attribute and reflection transitions from the data access layer to the business object-III

Source: Internet
Author: User
Tags abstract bool reflection
Introduction to Data
The last article in this series. How to describe the first part, two parts how to obtain the description. Now we're going to create the DAL library to make our title workable.

Design the DAL library
The class library that I want to create supports SQL Server and OLE DB. I divided the library into the following sections:

Utility classes
Class Dalquerybuilder
Generates an SQL statement to update an object.

Class Dalparameter
The build parameters are saved in the stored procedure.

Class Dalexception
Inherited from System.Exception, more information will be provided when there is an exception to the database.

Attribute classes
See the first article.

DAL itself
Class Dalengine
This abstract class is used for database operations and is simpler for database programs. Its virtual and abstract methods have different implementations.

Class Dalsqlengine


Class Daloledbengine
Columbine One eye Dalengine class
Public abstract class Dalengine:idisposable
{
//
Private data members
//
IDbConnection conn = null;
String connectionString = "";
ArrayList parameters = new ArrayList ();
bool Canclose = true;


Constructor
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 is override with a specific data provider
Implementation please implementation 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 Execquery_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 clearparameters ();


//
For those this 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 (string spname);


//
Methods for those this 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 Updateobjectstoredprocedure (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=;d atabase=pubs";

Public DAL (): Base (conn_string)
{

}

Public ArrayList getcustomerdependents (customer customer)
{
ArrayList result = new ArrayList ();

Retrievechildobjects (Customer. Id, result, typeof (Customerdependent));

return result;
}

public void Updatecustomerdependents (Customer customer)
{
Updateobjects (Customer. dependents);
}
}
Look at an example:

public static void Main ()
{

Dal dal = new Dal ();

Try
{

Contact contacts = 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. Newdependent ();
Dependent. Name = "Marie noyter";
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. Relationship = Customerrelationship.friend;
Paul. Dependents.add (dependent);

Dal. Updatecustomerdependents (Paul);
}
Else
{
Console.WriteLine ("Dependents of {0}", Paul.) Name);

foreach (Customerdependent dependent in Paul.) Dependents)
{
Console.WriteLine ("<dependent>{0}-{1} [{2}]", Dependent. Id
Dependent. Name, dependent. Relationship);
Dependent. Relationship = customerrelationship.family;
}

Dal. Updatecustomerdependents (Paul);
}

}
Finally
{
Dal. Dispose ();
}
}
Conclusion
There are many limitations, we need to further implement thinking, but after you understand these, we can conduct nhibernate understanding research and application, wish you good luck.




Related Article

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.