Reconstruction of petshop4.0 dalfactory Project

Source: Internet
Author: User

In the data access layer, petshop4.0 is implemented for sqlserver and Oracle databases, In order to loose coupling with these specific implementations at the business logic layer. Defines the data access interface and data access to simplify the factory, so that you only need to modify the configuration file to facilitate the migration of the database. The data access layer factory class is as follows:
Public sealed class dataaccess {
// Look up the Dal implementation we shoshould be using
Private Static readonly string Path = configurationmanager. deleettings ["webdal"];
Private Static readonly string orderpath = configurationmanager. deleettings ["ordersdal"];

Private dataaccess (){}

Public static petshop. idal. icategory createcategory (){
String classname = path + ". Category ";
Return (petshop. idal. icategory) Assembly. Load (PATH). createinstance (classname );
}

Public static petshop. idal. iinventory createinventory (){
String classname = path + ". Inventory ";
Return (petshop. idal. iinventory) Assembly. Load (PATH). createinstance (classname );
}

Public static petshop. idal. iItem createitem (){
String classname = path + ". Item ";
Return (petshop. idal. iItem) Assembly. Load (PATH). createinstance (classname );
}

Public static petshop. idal. iorder createorder (){
String classname = orderpath + ". Order ";
Return (petshop. idal. iorder) Assembly. Load (orderpath). createinstance (classname );
}

Public static petshop. idal. iproduct createproduct (){
String classname = path + ". Product ";
Return (petshop. idal. iproduct) Assembly. Load (PATH). createinstance (classname );
}

}
I think it may be better to refactor it like this. You can directly access the dalfactory class when using it. For example, dalfactory. instance. createcategory () can get the corresponding icategory database access interface. In addition, it is better to use less reflection, and it is not too late to use it when you have to use it. The Code is as follows:
Public abstract class dalfactory
{
Public static readonly dalfactory instance;

Static dalfactory ()
{
String dbprovider = configurationmanager. deleetpipeline ["dbprovider"];
Switch (dbprovider)
{
Case "sqlserverdb ":
Instance = new sqlserverdalfactory ();
Break;
Case "oracledb ":
Instance = new oracledalfactory ();
Break;
Default:
Instance = new sqlserverdalfactory ();
Break;
}
}

Public abstract petshop. idal. icategory createcategory ();

Public abstract petshop. idal. iinventory createinventory ();

Public abstract petshop. idal. iItem createitem ();

Public abstract petshop. idal. iorder createorder ();

Public abstract petshop. idal. iproduct createproduct ();
}

Public class sqlserverdalfactory: dalfactory
{
Public override petshop. idal. icategory createcategory ()
{
Return new petshop. sqlserverdal. Category ();
}

Public override petshop. idal. iinventory createinventory ()
{
Return new petshop. sqlserverdal. Inventory ();
}

Public override petshop. idal. iItem createitem ()
{
Return new petshop. sqlserverdal. Item ();
}

Public override petshop. idal. iorder createorder ()
{
Return new petshop. sqlserverdal. Order ();
}

Public override petshop. idal. iproduct createproduct ()
{
Return new petshop. sqlserverdal. Product ();
}
}

Public class implements ledalfactory: dalfactory
{
Public override petshop. idal. icategory createcategory ()
{
Return new petshop. incluledal. Category ();
}

Public override petshop. idal. iinventory createinventory ()
{
Return new petshop. incluledal. Inventory ();
}

Public override petshop. idal. iItem createitem ()
{
Return new petshop. incluledal. Item ();
}

Public override petshop. idal. iorder createorder ()
{
Return new petshop. incluledal. Order ();
}

Public override petshop. idal. iproduct createproduct ()
{
Return new petshop. incluledal. Product ();
}
}

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.