Java 业务类和领域类有什么区别?持久类是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1674209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:02:03  来源:igfitidea点击:

What is the difference between business class and domain class? What is meant by persistent classes?

javaooppersistence

提问by sevugarajan

What is the difference between business class and domain class? What is meant by persistent classes?

业务类和领域类有什么区别?持久类是什么意思?

回答by Kevin Babcock

A "domain" class is one that models your data. It is typically used to map data from your data store (e.g. a database) to an in-memory object. A business class is one that your application works with. It can be the same as the domain class, and usually performs some sort of business logic or processing.

“域”类是对数据建模的类。它通常用于将数据从数据存储(例如数据库)映射到内存对象。业务类是您的应用程序可以使用的类。它可以与域类相同,并且通常执行某种业务逻辑或处理。

Persistence refers to the ability of the object state to be saved to some data store (e.g. xml, a text file, or more commonly, a database). So the state of this kind of object "persists" whether the application is running or not.

持久性是指将对象状态保存到某个数据存储(例如 xml、文本文件或更常见的数据库)的能力。因此,无论应用程序是否正在运行,这种对象的状态都会“持续存在”。

It sounds to me like you're curious about Object/Relational Mapping. I recommend reading this wikipedia articleto get started.

在我看来,您对对象/关系映射很好奇。我建议您阅读这篇维基百科文章以开始使用。

回答by NawaMan

Business classesare classes that are designed and used for specific project and hardly reusable to other project.

业务类是为特定项目设计和使用的类,几乎不可重用于其他项目。

Domain classesare classes that are designed and used for specific domain which can be used by multiple projects that do similar thing.

域类是为特定域设计和使用的类,可以由多个执行类似操作的项目使用。

For example, JDBC classes are domain classes for Database access. Swing classes are domain classes for GUI construction and manipulation. Form1which contains a set of component used to display "Hello World" messsage is business class.

例如,JDBC 类是用于数据库访问的域类。Swing 类是用于 GUI 构建和操作的域类。Form1其中包含一组用于显示“Hello World”消息的组件是业务类。

回答by Mark Seemann

The terms Businessand Domainare often used synonymously. Personally, I prefer the terms Domain Model, Domain Object, etc. since they aren't constrained to deal with business(whatever that is).

业务领域这两个术语经常作为同义词使用。就个人而言,我更喜欢Domain ModelDomain Object等术语,因为它们不受限于处理业务(无论是什么)。

As an example, in Safewherewe work mainly with Claims-based Identity and Authorization. The concepts we model have very little in common with traditional 'business', so in such cases, I think Domain Modelis a more appropriate term.

例如,在Safewhere 中,我们主要使用基于声明的身份和授权。我们建模的概念与传统的“业务”几乎没有共同之处,因此在这种情况下,我认为领域模型是一个更合适的术语。

回答by extraneon

A business class provides domain specific logic.

业务类提供特定于领域的逻辑。

A domain class is an entity representing part of the domain.

域类是代表域的一部分的实体。

A persistent class, or better, a persistent instance, has the same data after a server reboot (the data is typically stored on a database of some kind).

持久化类,或者更好的持久化实例,在服务器重启后具有相同的数据(数据通常存储在某种数据库中)。

Let's take a banking transaction as an example. You'd have a persistent domain class Account. My Account(an instance) would have a field Balancewith value 1000.

我们以银行交易为例。您将拥有一个持久域类Account。我的Account(一个实例)会有一个Balance值为 1000的字段。

The transaction business logic which handles the debit is something like AccountDebitand has as method debitAccount(Account account, int amount), and as business logic

处理借方的交易业务逻辑类似于AccountDebit并具有 as 方法debitAccount(Account account, int amount)和业务逻辑

int balance = account.getBalance();
if (balance < amount) {
   throw exception("You're too poor");
}
balance = balance - amount;
account.setBalance(balance);
account.commit(); // save the values to persistent store

And I do know that money is typically not an int, and that the debited amount is normally credited to someone else :)

而且我确实知道钱通常不是整数,借记金额通常贷记给其他人:)

回答by Pascal Thivent

A domain classis a class from the Domain Modelthat Martin Fowler describes as follow in Patterns of Enterprise Application Architecture:

一个域类是从一个类领域模型,马丁·福勒介绍如下的企业应用架构模式

An object model of the domain that incorporates both behavior and data.

alt text

At its worst business logic can be very complex. Rules and logic describe many different cases and slants of behavior, and it's this complexity that objects were designed to work with. A Domain Model creates a web of interconnected objects, where each object represents some meaningful individual, whether as large as a corporation or as small as a single line on an order form.

包含行为和数据的域的对象模型。

替代文字

在最坏的情况下,业务逻辑可能非常复杂。规则和逻辑描述了许多不同的情况和行为倾向,而对象设计用于处理的正是这种复杂性。领域模型创建了一个互连对象的网络,其中每个对象代表一些有意义的个人,无论是大到公司还是小到订单上的一行。

And to me, there is no difference with a business class: a business object doesn't perform more or less business logic than a domain object (a domain model where business logic is implemented outside the domain objects is called an Anemic Domain Model, which is a pejorative term), domain objects and business objectsarethe same thing.

对我来说,业务类没有区别:业务对象执行的业务逻辑不会比域对象更多或更少(业务逻辑在域对象之外实现的域模型称为贫血域模型,它是贬义词),领域对象和业务对象一回事。

Finally, a persistent classis a class that can be... persisted which means transferring an in memory representation of information to a physical storage that will persist beyond the live of the JVM. Often, persistence is implemented using a database (but this is not the only solution, see for example object prevalence). Typical persistence operations include create, read, update and delete which are known as CRUD operations. Domain objects are very frequently persistent i.e you can perform CRUD operations on them through an API that hides the underlying details of the chosen persistence engine.

最后,持久类是一个可以……持久化的类,这意味着将内存中的信息表示传输到物理存储,该物理存储将在 JVM 运行后持续存在。通常,持久性是使用数据库来实现的(但这不是唯一的解决方案,例如,请参见对象普遍性)。典型的持久性操作包括创建、读取、更新和删除,这些操作称为 CRUD 操作。域对象经常是持久的,即您可以通过隐藏所选持久引擎的底层细节的 API 对它们执行 CRUD 操作。