简体   繁体   English

LINQ实体作为业务对象-pro / cons

[英]LINQ Entities as business objects - pro/cons

the dbml file generated by Visual Studio (sqlmetal) comes with entities mapped to database tables. Visual Studio(sqlmetal)生成的dbml文件带有映射到数据库表的实体。 In your opinion, these clases are suitable to be used as domain model classes? 您认为这些分类适合用作域模型类吗? Or should we avoid them and isolate them into the data access layer only? 还是应该避免使用它们,而仅将它们隔离到数据访问层中?

Thanks 谢谢

In most cases, the layer of sugary goodness between the entity objects and writing the SQL is your DAL. 在大多数情况下,实体对象和编写SQL之间的甜味层就是您的DAL。 The entire purpose of LINQ is to allow much more expressive constructs with your DAL than has been possible in the past. LINQ的全部目的是允许您的DAL使用比过去更多的表达性构造。

Without LINQ, in your BL you might be limited to method calls such as GetCustomersByLastName(string) against your DAL. 如果没有LINQ,则在您的BL中,您可能仅限于针对DAL的方法调用,例如GetCustomersByLastName(string) The only reason you write that method is because somewhere in your BL, you need to get customers by last name. 编写该方法的唯一原因是因为在BL中某处,您需要按姓氏来吸引客户。 This means your DAL contracts are explicitly driven by the needs of the BL. 这意味着您的DAL合同明确地由BL的需求驱动。 Whereas with LINQ, you are freed from the concrete dependency between the needs of the BL and the contracts of the DAL. 而使用LINQ,您可以摆脱BL需求和DAL合同之间的具体依赖。 The DAL can be completely agnostic as to specific usage; DAL可以完全不了解特定的用法。 it merely exposes the contracts of the entities and their relationships, and the BL uses them at-will without concern for their data implementation. 它仅公开实体及其关系的合同,而BL则随意使用它们,而无需担心其数据的实现。 That is true separation of concerns. 这是真正的关注点分离。

If you hide the power of LINQ behind a traditional DAL, what's the point of using LINQ at all? 如果您将LINQ的功能隐藏在传统DAL的后面,那么完全使用LINQ的意义何在?

For a simple enough application, the big plus of using the LINQ entities directly is - simplicity. 对于足够简单的应用程序,直接使用LINQ实体的最大好处是-简单性。 You can avoid having Yet Another Layer in your code, you can avoid having to write a lot of boilerplate logic to map from your business object to your LINQ entities (although there are tools like AutoMapper which can help a lot here). 您可以避免在代码中包含“另一层”,可以避免编写大量的样板逻辑以从业务对象映射到LINQ实体(尽管像AutoMapper这样的工具可以在此处提供很多帮助)。

On the other hand, as you say - your database probably isn't a spitting image of your domain model. 另一方面,正如您所说-数据库可能不是您的域模型的虚假图片。 Then again, if you need this capability to map between a given physical database model and your logical domain model, maybe you should have a look at the Entity Framework instead? 再说一次,如果您需要这种功能在给定的物理数据库模型和逻辑域模型之间进行映射,也许您应该看看实体框架? That's exactly the mainstay of EF - these two models and the mapping layer between them. 这正是EF的支柱-这两个模型以及它们之间的映射层。

Marc

It Depends 这取决于

If you're domain objects are very simple and your project small, then using those classes isn't a problem. 如果您的领域对象非常简单并且您的项目很小,那么使用这些类就不成问题。 It would be a waste of time to write an entire extra layer that just repeats those classes. 编写重复这些类的整个额外的层会浪费时间。

If the objects are complicated and don't map in a straitforward way, another layer is essential to separating those details from the data access layer. 如果对象很复杂并且不能以直截了当的方式进行映射,则必须使用另一层将这些细节与数据访问层分开。

If you really aren't sure, you can start off using them directly and then add in another layer once it is apparent that you will need it. 如果确实不确定,则可以直接使用它们,然后在明显需要时再添加另一层。

I think it depends on scenarios. 我认为这取决于场景。

I am now writing a WCF service, my data model is not very complicated but all tables have foreign keys and relationships between tables make linq create a quite complex objects tree. 我现在正在编写WCF服务,我的数据模型不是很复杂,但是所有表都具有外键,并且表之间的关系使linq创建了一个相当复杂的对象树。

From the client point of view it doesnt make sense to retrieve all this objects tree, and also the return xml message would be quite heavy (so if I dont change the message size max allowed value I get errors). 从客户端的角度来看,检索所有这些对象树没有任何意义,而且返回的xml消息也很繁重(因此,如果我不更改消息大小的最大允许值,则会出错)。

In this case I had to create custom views on LINQ objects to return just data client is expecting to retrieve. 在这种情况下,我必须在LINQ对象上创建自定义视图,以仅返回客户端希望检索的数据。 This is quite a pain as I had to re-write most of entities but in the end I have full control of the communication with client. 这很痛苦,因为我不得不重新编写大多数实体,但最终我可以完全控制与客户端的通信。

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

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