Ask a small question about overload of generic methods.

Source: Internet
Author: User
I implemented a three-tier architecture in a way similar to petshop. The data access layer has a base class dataaccessobjectbase. The implementation idea is as follows:

1. Two entity classes: orderinfo and orderdatailinfo. (Model, used as the data object passed between layers)
2. base classes of the data access layer:
Public abstract class dataaccessobjectbase <tdomainobject> where tdomainobject: New ()
{
Public String objectid;
Public String parentobjectid;
Public String childobjectid;

# Region common storedprocedure Name List

Public String selectbyidstoredprocedurename;

Public String selectdetailobjectstoredprocedurename;
Public String selectmasterobjectstoredprocedurename;
# Endregion

Public abstract void loaddomainobjectinfo (tdomainobject domainobject, system. Data. idatareader Dr );
Public tdomainobject selectbyid (system. guid ID)
{
Database DB = databasefactory. createdatabase ();
Dbcommand cmd = dB. getstoredproccommand (selectbyidstoredprocedurename );
Dataaccesshelper. populateidparamter (dB, CMD, ID );

Tdomainobject domainobject = new tdomainobject ();

Try
{
Using (idatareader DR = dB. executereader (CMD ))
{
If (dr. Read ())
{
Loaddomainobjectinfo (domainobject, Dr );
}
}
}
Catch (invalidoperationexception ex)
{
// If (exceptionpolicy. handleexception (ex, Sr. exceptionreplacepolicy) Throw;
}
Catch (dataexception ex)
{
// If (exceptionpolicy. handleexception (ex, Sr. exceptionwrappolicy) Throw;
}
Return domainobject;
}

Public Virtual void loaddetailobjectinfo <tdetailobject> (tdetailobject detailobject, system. Data. idatareader Dr)
{
Throw new exception ("the method or operation is not implemented .");
}

//


// return the sub-objects of the current object class, such as the order class, the returned order details are
///
///
// /
//
public list selectdetailobject (System. guid ID) Where tdetailobject: New ()
{< br> If (string. isnullorempty (selectdetailobjectstoredprocedurename) return NULL;

Database DB = databasefactory. createdatabase ();
Dbcommand cmd = dB. getstoredproccommand (selectdetailobjectstoredprocedurename );

Dataaccesshelper. populateidparamter (dB, CMD, ID, objectid );

List <tdetailobject> domainobjectlist = new list <tdetailobject> ();
Try
{
Using (idatareader DR = dB. executereader (CMD ))
{
While (dr. Read ())
{
Tdetailobject detailobject = new tdetailobject ();
Loaddetailobjectinfo <tdetailobject> (tdetailobject) detailobject, Dr );

Domainobjectlist. Add (detailobject );

}
}
}
Catch (invalidoperationexception ex)
{
// If (exceptionpolicy. handleexception (ex, Sr. exceptionreplacepolicy) Throw;
}
Catch (dataexception ex)
{
// If (exceptionpolicy. handleexception (ex, Sr. exceptionwrappolicy) Throw;
}
Return domainobjectlist;
}

Order data category: inherited from dataaccessobjectbase,
Abstract and virtual methods are overloaded, as follows:
//////////////////////////////////
Public override void loaddomainobjectinfo (orderinfo order, system. Data. idatareader Dr)
{
Loadadminobjectinfo. loadorderinfo (Order, Dr );
}

// Ask: How should I reload this method? <====================================
Public override void loaddetailobjectinfo <tdetailobject> (tdetailobject detailobject, idatareader Dr)
{

Loadadminobjectinfo. loadorderiteminfo (detailobject, Dr );
}

Loadadminobjectinfo is a helper class. There are two methods:
Public static void loadorderinfo (orderinfo order, system. Data. idatareader Dr)
{
Order. orderid = (system. guid) Dr ["orderid"];
Order. sequencenumber = (system. int32) Dr ["sequencenumber"];
Order. Name = (system. String) Dr ["name"];

}
Public static void loadorderiteminfo (orderiteminfo orderitem, system. Data. idatareader Dr)
{
Orderitem. orderitemid = (system. guid) Dr ["orderitemid"];
Orderitem. orderid = (system. guid) Dr ["orderid"];
Orderitem. localid = (system. String) Dr ["localid"];
}

For specific data access objects, how should loaddetailobjectinfo be reloaded?

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.