简体   繁体   English

数据库工厂类设计

[英]Database factory class design

Hi i have this class to instantiate DAL classes: 嗨,我有此类来实例化DAL类:

public class Factory
{
    public static T GetInstance<T>() where T : new()
    { 
        return new T();
    }
}

I want to make my application capable of using multiple databases. 我想使我的应用程序能够使用多个数据库。 I was planning on setting the database in my web.config and then pass in that setting possibly to the factory class where it will return the correct DAL class. 我打算在web.config中设置数据库,然后将该设置传递给可能返回正确DAL类的工厂类。 I think my methodology is ok im just a bit stuck on how to implement it whilst keeping it generic. 我认为我的方法还可以,只是在保持通用性的同时,还停留在如何实现它上。

Maybe something like this: 也许是这样的:

public class Factory
{
    private static readonly string dbType = ConfigurationSettings.Appsettings["SqlServer"];
    public static T GetInstance<T>() where T : new()
    { 
        switch(dbType)
        {
             case "SqlServer":
                return new T(); //Not sure what to put here.
             break;
             case: "MySql":
                return new T(); 
             break;
             default: "No datasource";
        }
    }
}

If anyone could help or point me in the right direction that would be great. 如果有人可以帮助或指出正确的方向,那将是很好的。

Thanks in advance. 提前致谢。

Don't use generics on your GetInstance() method. 不要在GetInstance()方法上使用泛型。 Have all your data access classes implement an interface instead, and have that interface be the return value from the function. 让您的所有数据访问类都实现一个接口,并将该接口作为函数的返回值。

public class Factory
{
    private static readonly string dbType = ConfigurationSettings.Appsettings["SqlServer"];
    public static IDataAccess GetInstance()
    { 
        switch(dbType)
        {
             case "SqlServer":
                return new SqlServerDataAccess(); //SqlServerDataAccess should implement IDataAccess
             break;
             case: "MySql":
                return new MySqlDataAccess(); //MySqlDataAccess should implement IDataAccess
             break;
             default: "No datasource";
        }
    }
}

Separate interfaces from implementation! 接口与实现分开!

You should look at the System.Data.Common NameSpace. 您应该查看System.Data.Common命名空间。 This namespace uses structures like DbConnection, DbReader, and so on, and itself uses a factory method to great the required DbProvider. 该名称空间使用DbConnection,DbReader等结构,并且本身使用工厂方法扩展所需的DbProvider。

So instead of going down your current path, I would suggest letting the current .net data framework do the lifting for you. 因此,我建议不要让当前的.net数据框架为您效劳,而不是沿着当前的道路走下去。 here's a quick example. 这是一个简单的例子。

DbProviderFactory m_factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
    DbConnection m_connection = m_factory.CreateConnection();
    m_connection.ConnectionString = _connstrbldr.ConnectionString;
    m_connection.Open();
    using (DbCommand cmd = m_connection.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "";
        cmd.ExecuteNonQuery();
    }

The GetFactory call can accept any Provider that installed on the machine, Oracle, MySql, Sql, etc. You can also Get all providers that are installed on a machine by making a call to static DataTable GetFactoryClasses() this returns a datatable object. GetFactory调用可以接受安装在计算机上的任何提供程序,Oracle,MySql,Sql等。您还可以通过调用静态DataTable GetFactoryClasses()来获取安装在计算机上的所有提供程序,这将返回一个数据表对象。

The idea behind this would be to avoid provider specific implementations and rely on a generic implementation that would accommodate all your needs. 其背后的想法是避免提供程序特定的实现,而依赖于可满足您所有需求的通用实现。

Writing Provider Independent Code in ADO.NET 在ADO.NET中编写提供程序独立代码

I hope that you find this helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM