数据访问层或具有带有 CRUD 方法的对象?

发布于 2024-08-06 11:23:16 字数 199 浏览 2 评论 0原文

我曾经有一个数据访问层,它通过参数获取对象并抽象处理所需的持久性类型。在我的新工作中,架构师正在考虑将 CRUD 操作(加载..保存..删除..更新)实现到所有模型对象中。这些方法将有一个通过参数来处理对象保存的对象。示例:加载(IPersistence 持久性)。我对可扩展性有一些疑问,并且每个模型对象都必须实现所有加载、保存、删除、更新方法。

最好的方法是什么?

I used to have a Data Access Layer that take the object by parameter and handle with abstraction the type of persistence required. At my new job, the architect is thinking to implement CRUD operation (load..save..delete..update) into all model object. Those method would have an object by parameter that will handle the saving of the object. Exemple : load(IPersistence persistence). I have some doubt about the extensibility and that each model object would have to implement all load,save,delete,update method.

What is the best approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残月升风 2024-08-13 11:23:16

我想这是一个永恒的问题,这两种方法都有其优点和缺点,并且有很多追随者坚信它们。

您似乎喜欢使用存储库方法(拥有存储库/网关,无论您如何称呼它)来处理 CRUD 操作,使您的实际业务类更小、更精简,因为它们可能只包含数据和可能的验证/业务规则。

在这种情况下,您将实现一次 CRUD 操作 - 但很可能为您正在处理的每种类型的对象实现一次。

另一方面,智能业务对象方法可能会认为给定实体上的 CRUD 操作无论如何都是特定于该实体的,因此选择、更新、删除此类实体的代码始终是具体的,所以它也可能驻留在该对象本身内部。

在这种情况下,您将为每个对象类实现一次 CRUD 操作 - 在这种情况下,我确实没有看到与存储库方法相比有任何大的缺点。

我个人今天倾向于存储库方法,但我也看到了“智能业务对象”方法的好处 - 两者都是有效的,我想您只需要说服您的新架构师您的立场,或者开始术语采用不同的方法。

马克

I guess that's the eternal question and both approaches do have their pros and cons and lots of followers who swear by them.

The repository approach that you seem to favor (having a Repository/Gateway whatever you call it) to handle the CRUD operations makes your actual business classes smaller and leaner, since they probably only contain data and possibly validation / business rules.

In this case, you'd implement the CRUD operations once - but most likely once for each type of object you're dealing with.

On the other hand, the smart business object approach might argue that CRUD operation on a given entity are specific to that entity anyway, so the code to select, update, delete such an entity is always going to be specific, so it might as well reside inside that object itself.

In this case, you'd implement the CRUD operations once for each object class - I don't see any big disadvantage over the repository approach in this case, really.

I personally lean towards the repository approach myself today, but I do also see benefits in the "smart business object" approach - both are valid, and I guess you'll just have to either convince your new architect of your position, or get to terms with a different approach.

Marc

懷念過去 2024-08-13 11:23:16

达尔一路走来。

您希望能够隔离事务,以便对象不应该知道它们的持久性。否则,代码可能会成为一场难以维护的噩梦,其中对象可以激活数据库往返,并且很难将多个事务滚动到一个原子操作中。

我通过艰难的方式发现了这一点。 :)

DAL all the way.

You want to be able to isolate your transactions so the objects shouldn't be aware of their persistence. Otherwise the code can tend to an unmaintainable nightmare where objects can activate database round trips and it is difficult to roll a number of transactions into one atomic action.

I found out this the hard way. :)

我不咬妳我踢妳 2024-08-13 11:23:16

我认为,在这两种情况下,实现都不应该重复,而应该只实现一次并根据需要继承(例如)。
仅非标准作业(例如带有自定义参数的自定义查询)需要子类及其方法。


现在问题就变成了POJO 哲学辩论。让我尝试用自己的话来表达它;-):

  1. 考虑到模型特定于每个特定问题,因此每个应用程序
  2. 都考虑到模型需要许多方面 >:持久性是模型所需的一方面,此外还有验证、文档、用户界面组件和消息、用户建议、版本之间的迁移……
  3. 考虑到模型本身就足够困难单独理解、维护等等,而不合并所有方面,
  4. 您可以推断出任何方面都应该从模型对象中外部化

实际上,我们只将复杂的东西外部化(通常需要一些编码),而保留非常简单的 Pojo 东西(通常是声明,经常使用注解)。


模型没有技术超类的另一个巨大优势是,模型可以用作自己的“数据传输对象”,在系统之间传输该信息:

  1. JVM 之间的层之间
  2. (通过序列化),例如机器之间的示例

如果我们的模型类具有技术超类,那么它们在如此不同的上下文中将没有用处。例如:

  1. 模型对象的持久性通常仅在某些层中可用(在架构选择中)。例如,只有业务层才能访问数据层以进行持久化。
  2. 在数据从一台机器迁移到另一台机器的过程中,每台机器都可以在自己的数据库上工作。因此,如果模型对象不携带指向数据库的指针,则可以自由传输;每个服务器应该使用自己的数据库。 对于同一模型对象,其他方面可能存在变化,如果这些方面不是由同一对象承载,则这会很方便。

I think that, in both cases, the implementation should not be repetitive, but implemented only once and inherited (for example) as needed.
Subclasses and their methods would only be needed for non-standard jobs (like custom queries, with their custom parameters).


Now the question amounts to the POJO philosophical debate. Let me try to phrase it in my own words ;-) :

  1. considering that the model is specific to each specific problem and therefore each application
  2. considering that many aspects are required for the model : persistance is one aspect required for a model, along with validation, documentation, user-interface components and messages, user-suggestions, migration between versions ...
  3. considering that the model alone is hard enough to understand, maintain and so on alone, without all aspects be merged
  4. you deduce that any aspect should be externalized from the model objects.

Actually, we only externalize things that are complex (typically, require some coding), and keep on the Pojo things that are very simple (typically declarations, often using Annotations).


Another huge advantage of having no technical superclass for the model is that the model can be used as its own "data transfer object" to carry that information between systems :

  1. between layers
  2. between JVMs (via Serialization), for example between machines

If our model classes would have technical superclasses, they wouldn't be useful in such various contexts. For example:

  1. Persistance on an model object is often usable only in some layer (in the architectural choices). For example, only the Business Layer would have access to the Data Layer to persist.
  2. During data migration from one machine to another, each machine could work on its own database. Thus, the model objects can be transfered freely if they don't carry pointers to the database ; each serveur should use its own database. For the same model object, variation is possible on other aspects, which is facilitated if the aspects are not carried by the same object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文