跨多个数据库构建数据层的最佳方法是什么?

时间:2021-06-30 03:55:06

First a bit about the environment:

首先介绍一下环境:

We use a program called Clearview to manage service relationships with our customers, including call center and field service work. In order to better support clients and our field technicians we also developed a web site to provide access to the service records in Clearview and reporting. Over time our need to customize the behavior and add new features led to more and more things being tied to this website and it's database.

我们使用名为Clearview的程序来管理与客户的服务关系,包括呼叫中心和现场服务工作。为了更好地支持客户和我们的现场技术人员,我们还开发了一个网站,以便访问Clearview中的服务记录并进行报告。随着时间的推移,我们需要定制行为并添加新功能,这导致越来越多的东西被绑定到这个网站及其数据库。

At this point we're dealing with things like a Company being defined partly in the Clearview database and partly in the website database. For good measure we're also starting to tie the scripting for our phone system into the same website, which will require talking to the phone system's own database as well.

在这一点上,我们处理的事情,如公司部分在Clearview数据库中定义,部分在网站数据库中。为了更好的衡量,我们也开始将我们的电话系统的脚本绑定到同一个网站,这也需要与电话系统自己的数据库通信。

All of this is set up and working... BUT we don't have a good data layer to work with it all. We moved to Linq to SQL and now have two DBMLs that we can use, along with some custom classes I wrote before I'd ever heard of Linq, along with some of the old style ADO datasets. So yeah, basically things are a mess.

所有这些都已设置并正常工作......但我们没有一个好的数据层来处理这一切。我们转移到Linq to SQL,现在我们可以使用两个DBML,以及我在听过Linq之前编写的一些自定义类,以及一些旧式ADO数据集。所以,是的,基本上事情是一团糟。

What I want is a data layer that provides a single front end for our applications, and on the back end manages everything into the correct database.

我想要的是一个数据层,它为我们的应用程序提供单个前端,并在后端管理所有内容到正确的数据库。

I had heard something about Entity Framework allowing classes to be built from multiple sources, but it turns out there can only be one database. So the question is, how could I proceed with this?

我听说过Entity Framework允许从多个源构建类,但事实证明只能有一个数据库。所以问题是,我该如何处理?

I'm currently thinking of getting the Linq To SQL classes all set for each database, then manually writing Linq compatible front ends that tie those together. Seems like a lot of work, and given Linq's limitations (such as not being able to refresh) I'm not sure it's a good idea.

我目前正在考虑为每个数据库设置所有Linq To SQL类,然后手动编写将这些组合在一起的Linq兼容前端。看起来很多工作,并且考虑到Linq的局限性(例如无法刷新),我不确定这是一个好主意。

Could I do something with Entity Framework that would turn out better? Should I look into another tool? Am I crazy?

我可以用实体框架做一些更好的事情吗?我应该看看另一个工具吗?我疯了吗?

3 个解决方案

#1


The Entity Framework does give a certain measure of database independence, insofar as you can build an entity model from one database, and then connect it to a different database by using a different entity connect string. However, as you say, it's still just one database, and, moreover, it's limited to databases which support the Entity Framework. Many do, but not all of them. You could use multiple entity models within a single application in order to combine multiple databases using the Entity Framework. There is some information on this on the ADO.NET team blog. However, the Entity Framework support for doing this is, at best, in an early stage.

实体框架确实提供了一定的数据库独立性,只要您可以从一个数据库构建实体模型,然后使用不同的实体连接字符串将其连接到不同的数据库。但是,正如您所说,它仍然只是一个数据库,而且,它仅限于支持实体框架的数据库。许多人都这样做,但不是全部。您可以在单个应用程序中使用多个实体模型,以便使用实体框架组合多个数据库。 ADO.NET团队博客上有一些相关信息。但是,实体框架对此的支持充其量只是在早期阶段。

My approach to this problem is to abstract my use of the Entity Framework behind the Repository pattern. The most immediate benefit of this, for me, is to make unit testing very simple; instead of trying to mock my Entity model, I simply substitute a mock repository which returns IQueryables. But the same pattern is also really good for combining multiple data sources, or data sources for which there is no Entity Framework provider, such as a non-data-services-aware Web service.

我解决这个问题的方法是抽象使用Repository模式背后的Entity Framework。对我来说,最直接的好处是使单元测试变得非常简单;我只是替换一个返回IQueryables的模拟存储库,而不是试图模拟我的实体模型。但是,相同的模式对于组合多个数据源或没有实体框架提供者的数据源(例如非数据服务感知的Web服务)也非常有用。

So I'm not going to say, "Don't use the Entity Framework." I like it, and use it, myself. In view of recent news from Microsoft, I believe it is a better choice than LINQ to SQL. But it will not, by itself, solve the problem you describe. Use the Repository pattern.

所以我不会说,“不要使用实体框架。”我喜欢它,并且自己使用它。鉴于微软最近的消息,我认为它是比LINQ to SQL更好的选择。但它本身不能解决你描述的问题。使用存储库模式。

#2


if you want to use tools like Linq2SQl or EF and don't want to have to manage multiple DBMLS (or whaetever its called in EF or other tools), you could create views in your website database, that reference back to the ClearView or Phone system's DB.

如果你想使用像Linq2SQl或EF这样的工具,并且不想管理多个DBMLS(或者在EF或其他工具中调用它的whaetever),你可以在你的网站数据库中创建视图,引用回ClearView或Phone系统的DB。

This allows you to decouple your web site from their database structure. I believe Linq2Sql and EF can use a view as the source for an Entity. If they can't look at nHibernate.

这允许您将Web站点与其数据库结构分离。我相信Linq2Sql和EF可以使用视图作为实体的源。如果他们不能看nHibernate。

This will also let you have composite entities that are pulled from the various data sources. There are some limitations updating views in SQL Server; however, you can define your own Instead of trigger(s) on the view which can then do the actual insert update delete statements.

这也可以让您拥有从各种数据源中提取的复合实体。在SQL Server中更新视图存在一些限制;但是,您可以在视图上定义自己的而不是触发器,然后可以执行实际的插入更新删除语句。

#3


L2S works with views, perfectly, in my project. You only need to make a small trick: 1. Add a secondary DB table to the current DB as a view. 2. In Designer, add a primary key attribute to a id field on the view. 3. Only now, add an association to whatever other table you want in the original DB.

L2S在我的项目中完美地与视图一起工作。您只需要制作一个小技巧:1。将辅助数据库表作为视图添加到当前数据库。 2.在Designer中,将主键属性添加到视图上的id字段。 3.仅限现在,将关联添加到原始数据库中您想要的任何其他表。

Now, you might see the view available for the navigation.

现在,您可能会看到可用于导航的视图。

#1


The Entity Framework does give a certain measure of database independence, insofar as you can build an entity model from one database, and then connect it to a different database by using a different entity connect string. However, as you say, it's still just one database, and, moreover, it's limited to databases which support the Entity Framework. Many do, but not all of them. You could use multiple entity models within a single application in order to combine multiple databases using the Entity Framework. There is some information on this on the ADO.NET team blog. However, the Entity Framework support for doing this is, at best, in an early stage.

实体框架确实提供了一定的数据库独立性,只要您可以从一个数据库构建实体模型,然后使用不同的实体连接字符串将其连接到不同的数据库。但是,正如您所说,它仍然只是一个数据库,而且,它仅限于支持实体框架的数据库。许多人都这样做,但不是全部。您可以在单个应用程序中使用多个实体模型,以便使用实体框架组合多个数据库。 ADO.NET团队博客上有一些相关信息。但是,实体框架对此的支持充其量只是在早期阶段。

My approach to this problem is to abstract my use of the Entity Framework behind the Repository pattern. The most immediate benefit of this, for me, is to make unit testing very simple; instead of trying to mock my Entity model, I simply substitute a mock repository which returns IQueryables. But the same pattern is also really good for combining multiple data sources, or data sources for which there is no Entity Framework provider, such as a non-data-services-aware Web service.

我解决这个问题的方法是抽象使用Repository模式背后的Entity Framework。对我来说,最直接的好处是使单元测试变得非常简单;我只是替换一个返回IQueryables的模拟存储库,而不是试图模拟我的实体模型。但是,相同的模式对于组合多个数据源或没有实体框架提供者的数据源(例如非数据服务感知的Web服务)也非常有用。

So I'm not going to say, "Don't use the Entity Framework." I like it, and use it, myself. In view of recent news from Microsoft, I believe it is a better choice than LINQ to SQL. But it will not, by itself, solve the problem you describe. Use the Repository pattern.

所以我不会说,“不要使用实体框架。”我喜欢它,并且自己使用它。鉴于微软最近的消息,我认为它是比LINQ to SQL更好的选择。但它本身不能解决你描述的问题。使用存储库模式。

#2


if you want to use tools like Linq2SQl or EF and don't want to have to manage multiple DBMLS (or whaetever its called in EF or other tools), you could create views in your website database, that reference back to the ClearView or Phone system's DB.

如果你想使用像Linq2SQl或EF这样的工具,并且不想管理多个DBMLS(或者在EF或其他工具中调用它的whaetever),你可以在你的网站数据库中创建视图,引用回ClearView或Phone系统的DB。

This allows you to decouple your web site from their database structure. I believe Linq2Sql and EF can use a view as the source for an Entity. If they can't look at nHibernate.

这允许您将Web站点与其数据库结构分离。我相信Linq2Sql和EF可以使用视图作为实体的源。如果他们不能看nHibernate。

This will also let you have composite entities that are pulled from the various data sources. There are some limitations updating views in SQL Server; however, you can define your own Instead of trigger(s) on the view which can then do the actual insert update delete statements.

这也可以让您拥有从各种数据源中提取的复合实体。在SQL Server中更新视图存在一些限制;但是,您可以在视图上定义自己的而不是触发器,然后可以执行实际的插入更新删除语句。

#3


L2S works with views, perfectly, in my project. You only need to make a small trick: 1. Add a secondary DB table to the current DB as a view. 2. In Designer, add a primary key attribute to a id field on the view. 3. Only now, add an association to whatever other table you want in the original DB.

L2S在我的项目中完美地与视图一起工作。您只需要制作一个小技巧:1。将辅助数据库表作为视图添加到当前数据库。 2.在Designer中,将主键属性添加到视图上的id字段。 3.仅限现在,将关联添加到原始数据库中您想要的任何其他表。

Now, you might see the view available for the navigation.

现在,您可能会看到可用于导航的视图。