简体   繁体   English

具有多个数据库的Database Factory模式

[英]Database Factory pattern with multiple databases

I'm creating a database factory pattern for an application that should be able to work with SqlServer and Oracle. 我正在为应该能够使用SqlServer和Oracle的应用程序创建数据库工厂模式。 I've used a similar approach suggested in the following article: 我使用了以下文章中建议的类似方法:

http://www.primaryobjects.com/CMS/Article81.aspx http://www.primaryobjects.com/CMS/Article81.aspx

Now, the problem is that my application communicates with multiple databases on the same server. 现在,问题是我的应用程序与同一服务器上的多个数据库进行通信。 For ex: I'm running few queries against database DB1 and few queries against DB2, both on SqlServer. 例如:我在SqlServer上对数据库DB1运行的查询很少,而对DB2的查询很少。 Using the above article, I could create an instance of one database. 使用上面的文章,我可以创建一个数据库的实例。 How can I use the same approach to work with multiple databases. 如何使用相同的方法处理多个数据库。 Can somebody help me with this? 有人可以帮我这个吗? Thanks. 谢谢。

I would suggest that you take a look at the repository pattern. 我建议你看一下存储库模式。 With the use of a repository pattern you can abstract your application away from the infrastructure. 通过使用存储库模式,您可以将应用程序从基础结构中抽象出来。 This would then allow you to communicate with one or more databases, web services, the file system, or any other datasource that is outside of your domain and applications direct control. 这将允许您与一个或多个数据库,Web服务,文件系统或域外的任何其他数据源以及应用程序直接控制进行通信。 With this you could then have an infrastructure that behind the scenes can communicate with multiple data sources. 通过这种方式,您可以拥有一个基础架构,可以在幕后与多个数据源进行通信。 An example of this was an ecommerce application that I recently worked on that allowed me to speak with my local database for the direct product catalog but also spoke with SAP behind the scenes for logging new orders/purchases. 这方面的一个例子是我最近工作的电子商务应用程序,它允许我与我的本地数据库交谈直接产品目录,但也在后台与SAP交谈以记录新订单/购买。

With this you might have code that calls into the repository that looks something like this: 有了这个,您可能拥有调用存储库的代码,如下所示:

AccountRepository _repository = new AccountRepository();
Account account = _repository.GetAccountByID(32);

Or you can say something along the lines of: 或者您可以说出以下内容:

Account account = new AccountRepository().GetAccountByID(32);

The basic idea behind a repository is that your application can make calls into it to get the data that it needs. 存储库背后的基本思想是您的应用程序可以调用它来获取所需的数据。 It would return out actual domain objects (such as an Account in the example above). 它将返回实际的域对象(例如上面示例中的Account)。 Or it could return IEnumerable<Account> if a list of Accounts is needed. 或者,如果需要帐户列表,它可以返回IEnumerable<Account>

An implementation of this where you grab data from two datasources might look like (though I don't suggest co-mingling concerns like this): 从两个数据源中获取数据的实现可能看起来像(虽然我不建议像这样混合关注):

public class AccountRepository
{
    public Account GetAccountByID(int accountID)
    {
        Account result = null;

        using(MyDataContext dc = new ConnectionFactory.GetConnection(Databases.DB1))
        {
            result = dc.Accounts.Where(a=>a.AccountID == accountID).FirstOrDefault();
        }

        //result is null...go to the other database to get an Account
        if(result == null)
        {
            using(MyDataContext dc = new ConnectionFactory.GetConnection(Databases.DB2))
            {
                result = dc.Accounts.Where(a=>a.AccountID == accountID).FirstOrDefault();
            }
        }

        return result;
    }
}

My preference for a situation such as this would be to create an application service layer that handles the application logic. 我对这种情况的偏好是创建一个处理应用程序逻辑的应用程序服务层。 A repository should technically be concerned with one data source. 从技术上讲,存储库应该关注一个数据源。 You can then have a couple different repository for the couple different datasources. 然后,您可以为几个不同的数据源提供几个不同的存储库。 In your application layer would would then new up the instance of GetAccountByID from repository 1 (database 1) and if it was null...the application layer would then dip into the second repository (database 2 for example). 然后,在您的应用程序层中,将从存储库1(数据库1)中新建GetAccountByID实例,如果它为null,则应用程序层将进入第二个存储库(例如,数据库2)。 The repository should follow the SOLID principles if possible. 如果可能,存储库应遵循SOLID原则。 Where my example clearly breaks the SOLID approach is that there is more than one reason for that implementation of GetAccountByID to change! 我的例子清楚地说明了SOLID方法的原因是GetAccountByID的实现有多个原因需要改变!

But...if this is what you need...there is a way to do it! 但是......如果这是你需要的......有办法做到这一点!

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

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