简体   繁体   English

数据集应该位于 n 层(多层)架构中的什么位置?

[英]where should datasets reside in a n-tier (multi layered) architecture?

We are currently having a discussion if dataset should go in the data or business layer?我们目前正在讨论数据集应该放在数据层还是业务层?

My friend thinks all ADO.NET components should go in data layer.我的朋友认为所有 ADO.NET 组件都应该放在数据层中。 For me this does not seem right for the following reasons:对我来说,这似乎不正确,原因如下:

  • If you create a fat Data Layer Client, it will be much more difficult to for example migrate everything to a different data source.如果您创建一个胖数据层客户端,例如将所有内容迁移到不同的数据源将更加困难。
  • You can't have bound controls unless you skip the business layer logic.除非跳过业务层逻辑,否则无法绑定控件。

I think that datasets and datatables should be in the business logic since they are generic to all data providers.我认为数据集和数据表应该在业务逻辑中,因为它们对所有数据提供者都是通用的。 The data layer should have a Provider Factory for instantiating the right provider's objects (Connection, DataAdapters, Transactions, DataReaders, etc.).数据层应该有一个提供者工厂来实例化正确的提供者的对象(连接、数据适配器、事务、数据读取器等)。 For me this is the way to go for the following reasons:对我来说,这是要走的路,原因如下:

  • Migrating to a different data layer is as easy as it gets.迁移到不同的数据层非常容易。
  • You can bind your controls to rich business objects您可以将控件绑定到丰富的业务对象

Can some n-tier guru help us clear out which way to go?一些 n 级大师可以帮助我们弄清楚该走哪条路吗?

In my opinion, don't use DataSet at all.在我看来,根本不要使用 DataSet。 Don't even use typed DataSet.甚至不要使用类型化数据集。 These are old constructs created before LINQ.这些是在 LINQ 之前创建的旧结构。 Skip right over ancient history, and get into the present tense: use LINQ to Entities and the Entity Framework (EF).跳过古代历史,进入现在时:使用 LINQ to Entities 和实体框架 (EF)。 The two are closely related but not the same.两者密切相关,但并不相同。

Don't expose an EF entity across a service boundary.不要跨服务边界公开 EF 实体。 Unfortunately Microsoft chose to expose implementation details when serializing an entity.不幸的是,Microsoft 选择在序列化实体时公开实现细节。 Other than that, use EF and have a lot more fun than you would have had with DataSet.除此之外,使用 EF 会比使用 DataSet 获得更多乐趣。

Well, isolating data access is not new : we does it 15 years ago (yes, 15 years ago !).好吧,隔离数据访问并不是什么新鲜事:我们 15 年前就这样做了(是的,15 年前!)。

I have worked in a lot of places and I saw a lot of isolated data layers.我在很多地方工作过,我看到了很多孤立的数据层。

But I never -- ever !但我从来没有——从来没有! -- seen a data source being replaced ! - 看到一个数据源被替换了!

Yes, I saw it twice : and twice, we also replace the oudated data layer and all the toping software...是的,我看了两次:还有两次,我们还替换了旧数据层和所有打顶软件...

My answer is pretty simple : unless you are working for shelve softwares, you can isolate as much as you want the data layer, you will do it for nothing.我的回答很简单:除非你为搁置软件工作,否则你可以根据需要隔离尽可能多的数据层,你将一事无成。

For nothing because nobody will change SQL Server or Oracle just for changing.一无所获,因为没有人会为了改变而改变 SQL Server 或 Oracle。 And for nothing because the day someone will do it, either they will also rewrite their softwares or they will make shure that the product they are buying is compatible to the product they are lefting.而且一无所获,因为有一天有人会这样做,要么他们也会重写他们的软件,要么他们会确保他们购买的产品与他们留下的产品兼容。

In my books, any data layer is stupid.在我的书中,任何数据层都是愚蠢的。

If you do not agree with it, just tell me when in your life this layer save $$$ to someone...如果您不同意,请告诉我您生命中这一层何时为某人节省了$$$...

Where I'm at we return datasets, datatables, datarows, and datareaders from the data layer to the business layer.我在哪里,我们将数据集、数据表、数据行和数据读取器从数据层返回到业务层。

The rationale is that these types are not db-flavor-specific.理由是这些类型不是特定于 db-flavor 的。 Whether you use mysql, access, sql server, oracle, or whatever a dataset is a dataset, and therefore okay to return from a root level data layer.无论您使用 mysql、access、sql server、oracle,还是任何数据集都是数据集,因此可以从根级数据层返回。

A business layer then takes this raw data and turns it into strongly-typed business objects — applying any necessary business rules — to hand to the presentation layer.然后,业务层获取这些原始数据并将其转换为强类型业务对象——应用任何必要的业务规则——交给表示层。


Edit: As I look through some code, we don't use a full dataset much.编辑:当我查看一些代码时,我们并没有太多使用完整的数据集。 It's mostly datatables and datareaders.它主要是数据表和数据读取器。

一种典型的方法是在您的业务逻辑层/域中公开一个聚合根(如客户)存储库接口,并在您的数据访问层/基础设施中实现具体的存储库。

The fundamental issue I have with DataSets is that their structures are an exact mirror of the database schema.我对 DataSet 的基本问题是它们的结构是数据库模式的精确镜像。

If you expose the DataSet to the actual page rendering code, you are effectively exposing the database schema (the ultimate backend of the product) to the presentation layer.如果您将 DataSet 公开给实际的页面呈现代码,您实际上是将数据库架构(产品的最终后端)公开给表示层。 Now the obvious problem can happen: later down the track you will want to restructure the underlying data schema, and because of the design you will need to apply changes to all of the other layers in the system.现在可能会出现明显的问题:稍后您将需要重构底层数据模式,并且由于设计的原因,您需要将更改应用于系统中的所有其他层。 It's a prime example of encapsulation not being used when it really should be.这是封装没有在真正应该使用的时候使用的一个主要例子。

If you are going to use DataSets at all, keep the DataSets buried right down in the data access layer, and expose a conceptual set of business objects to the presentation layer.如果您打算使用 DataSet,请将 DataSet 直接隐藏在数据访问层中,并向表示层公开一组概念性的业务对象。 The set of business objects you expose should be designed according to good object oriented principals, which is completely different to good relational database design principals.您公开的业务对象集应该根据良好的面向对象原则设计,这与良好的关系数据库设计原则完全不同。

I would have to agreee about not using dataSets at all.我必须同意根本不使用数据集。 One of the applications that I worked on there were DataSets in both Data layer and in the Application layer.我在那里工作的应用程序之一是数据层和应用程序层中的数据集。 The DataLayer DataSets matched the DataBase where the Application layer datasets denormalized the information to make it more consumable to the frontend. DataLayer DataSets 与DataBase 匹配,其中应用层数据集对信息进行非规范化处理,使其更易于前端使用。

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

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