简体   繁体   English

Grails:当我不想保存任何内容时,我可以使用域对象吗?

[英]Grails: Can I use domain objects when I don't want to save anything?

Some of my domain classes are getting rather rich: They implement an interesting comparable, might have plus, minus, multiply and div, many have some convenient getters which call services and determine complicated things. 我的一些域类变得相当丰富:它们实现了一个有趣的可比较,可能有加号,减号,乘法和div,许多都有一些方便的getter来调用服务并确定复杂的东西。 And most of all, they have the right properties. 最重要的是,他们拥有合适的属性。 I am using these both for the normal "database transactions" and also in times when I just want an object that has all those methods but may not want to save it. 我正在将这两者用于正常的“数据库事务”,并且在我只想要一个具有所有这些方法但可能不想保存它的对象的时候。

My teammates are convinced that this is very bad, and advise that I should be using DTO (Data Transfer Objects) which, as I understand, would be a POGO/POJO with one of the domain class' code copy/pasted in. This seems really un-dry, and I can't see the benefit. 我的队友确信这非常糟糕,并建议我应该使用DTO(数据传输对象),据我所知,这将是一个POGO / POJO,其中一个域类'代码复制/粘贴。这似乎真的不干,我看不出有什么好处。 Is there something wrong with using domain objects as regular objects from time to time? 将域对象不时用作常规对象有什么问题吗? Am I missing the point of DTO's? 我错过了DTO的观点吗?

Grails domain classes are somewhat misnamed, since an application's domain tier is usually made up of persistent and non-persistent classes. Grails域类有点错误,因为应用程序的域层通常由持久类和非持久类组成。 But Grails domain classes are always persistent. 但是Grails域类总是持久的。 You can have non-persistent domain (in the traditional sense) classes but they would have to be in src/groovy or src/java. 您可以拥有非持久域(在传统意义上)类,但它们必须位于src / groovy或src / java中。 That can be frustrating because then the domain tier is split in the application into two places. 这可能令人沮丧,因为域应用程序在应用程序中分为两个部分。 We've had requests for non-persistent domain classes, eg something like static persistent = false or something similar, but it's not implemented yet. 我们已经有了非持久域类的请求,例如static persistent = false或类似的东西,但它还没有实现。

I think if you want to take advantage of the non-persistent features of domain classes (eg validation, dependency injection, etc.) then it's fine to have some classes that could be database-backed but aren't. 我想如果你想利用域类的非持久性特性(例如验证,依赖注入等),那么可以使用一些可以由数据库支持但不支持的类。 You just need to document that in the code or have some sort of convention, eg a special package structure or naming convention. 您只需要在代码中记录它或具有某种约定,例如特殊的包结构或命名约定。 If you never call GORM methods like save() , list() , findAllByFoo() , etc. then there won't be any database access. 如果你从不调用诸如save()list()findAllByFoo()等GORM方法,那么就不会有任何数据库访问。

As far as DTOs go, they can be un-DRY but there's a plugin that helps - see http://grails.org/plugin/dto . 就DTO而言,它们可以是非干的,但有一个插件可以提供帮助 - 请参阅http://grails.org/plugin/dto It hasn't been updated in a while but I'm pretty sure it still works. 它有一段时间没有更新,但我很确定它仍然有效。 It has a nice feature where it will create a DTO instance from a persistent domain class instance with the syntax domainObj as DTO . 它有一个很好的功能,它将使用语法domainObj as DTO从持久域类实例创建DTO实例。 You do need to keep changes synced between the classes, but the initial DTO generation is automatic via a script. 您确实需要在类之间保持同步更改,但是通过脚本自动生成初始DTO。

I think you're in the right path. 我认为你走的是正确的道路。

1 - The moment you have to create a class only to take care of a domain class, you make your model more coupled. 1 - 只需要创建一个类来处理域类,就可以使模型更加耦合。 You are creating more dependencies, which is clearly bad. 您正在创建更多依赖项,这显然很糟糕。 Your objects should be capable of taking care of themselves. 你的物品应该能够照顾好自己。

2 - The model your friends are talking about is actually known as the Anemic Domain Model , where you have data separated from the logic of your program and it was first described by Martin Fowler as an Anti-Pattern. 2 - 您的朋友正在谈论的模型实际上被称为贫血领域模型 ,您可以将数据与程序的逻辑分开,并且首先由Martin Fowler将其描述为反模式。 This separation between logic and data is very used in procedural programming but not in Object Oriented Programming (the purpose of OOP is the exact opposite). 逻辑和数据之间的这种分离在程序编程中非常常用,但在面向对象编程中却没有(OOP的目的恰恰相反)。

3 - Reduces code reuse. 3 - 减少代码重用。

4 - Is harder to test, once you have to initialize the logic separated from your data. 4 - 一旦必须初始化与数据分离的逻辑,就更难测试。 At the same time, data leaks through your system. 同时,数据通过您的系统泄漏。

DTO is something you could use, for sure. DTO是你可以使用的东西,当然。 But it is not advised that you do though. 但不建议您这样做。 It was initially designed to carry data(objects) through processes or tiers. 它最初设计用于通过进程或层来传输数据(对象)。 Then, for some reason, people started using it between layers, which is not worthy. 然后,由于某种原因,人们开始在层之间使用它,这是不值得的。 Makes your program more complex (once you have these entities spread across your application) and it allows global access. 使您的程序更复杂(一旦您将这些实体分布在您的应用程序中),它就允许全局访问。

What you are doing is called Rich Domain Model, and there's no problem of using it. 你正在做什么被称为富域模型,并没有使用它的问题。 But, of course you should be careful with that. 但是,当然你应该小心。 If you see your class carrying too much responsibilities, then maybe it's time to design another classes to help (maybe you're breaking the SRP principle). 如果你看到你的班级承担了太多的责任,那么也许是时候设计另一个课程来帮助(也许你打破了SRP原则)。

Take a look at how domain objects are designed in Grails. 看看如何在Grails中设计域对象。 They encourage you to write rich models (validation, DB transaction etc.). 他们鼓励您编写丰富的模型(验证,数据库事务等)。

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

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