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 ();
}
}