面向服务与面向对象——它们可以共存吗?

发布于 2024-07-11 10:43:34 字数 1263 浏览 9 评论 0原文

最近,我的公司对面向服务的架构 (SOA) 产生了浓厚的兴趣。 每当我试图了解如何使用它时,我总是会遇到心理障碍。 粗略地说:

  • 面向对象说:“将数据和操纵数据(业务流程)的方法放在一起”;

  • 面向服务说:“将业务流程保留在服务中,并向其传递数据”。

    面向服务说

以前开发 SOA 的尝试最终是将面向对象的代码转换为数据结构和操作它们的单独过程(服务),这似乎是一种倒退。

我的问题是:什么模式、架构、策略等允许 SOA 和 OO 一起工作?


编辑:“OO 用于内部,SOA 用于系统边界”的答案是很棒而且很有用,但这并不是我想要的。

假设您有一个 Account 对象,该对象具有名为 Merge 的业务操作,可将其与另一个帐户合并。 典型的 OO 方法如下所示:

Account mainAccount = database.loadAccount(mainId);
Account lesserAccount = database.loadAccount(lesserId);

mainAccount.mergeWith(lesserAccount);

mainAccount.save();
lesserAccount.delete();

而我见过的 SOA 等效方法如下所示:

Account mainAccount = accountService.loadAccount(mainId);
Account lesserAccount = accountService.loadAccount(lesserId);

accountService.merge(mainAccount, lesserAccount);
// save and delete handled by the service

在 OO 情况下,业务逻辑(以及得益于 ActiveRecord 模式的实体感知)被烘焙到 Account 中班级。 在 SOA 情况下,Account 对象实际上只是一个结构,因为所有业务规则都隐藏在服务中。

我可以同时拥有丰富的功能类和可重用的服务吗?

There's been a lot of interest in Service-Oriented Architecture (SOA) at my company recently. Whenever I try to see how we might use it, I always run up against a mental block. Crudely:

  • Object-orientation says: "keep data and methods that manipulate data (business processes) together";

  • Service-orientation says: "keep the business process in the service, and pass data to it".

Previous attempts to develop SOA have ended up converting object-oriented code into data structures and separate procedures (services) that manipulate them, which seems like a step backwards.

My question is: what patterns, architectures, strategies etc. allow SOA and OO to work together?


Edit: The answers saying "OO for internals, SOA for system boundaries" are great and useful, but this isn't quite what I was getting at.

Let's say you have an Account object which has a business operation called Merge that combines it with another account. A typical OO approach would look like this:

Account mainAccount = database.loadAccount(mainId);
Account lesserAccount = database.loadAccount(lesserId);

mainAccount.mergeWith(lesserAccount);

mainAccount.save();
lesserAccount.delete();

Whereas the SOA equivalent I've seen looks like this:

Account mainAccount = accountService.loadAccount(mainId);
Account lesserAccount = accountService.loadAccount(lesserId);

accountService.merge(mainAccount, lesserAccount);
// save and delete handled by the service

In the OO case the business logic (and entity awareness thanks to an ActiveRecord pattern) are baked into the Account class. In the SOA case the Account object is really just a structure, since all of the business rules are buried in the service.

Can I have rich, functional classes and reusable services at the same time?

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

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

发布评论

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

评论(6

坠似风落 2024-07-18 10:43:34

我的观点是,SOA 在宏观层面上很有用,但每个服务可能仍然足够大,需要多个内部组件。 内部组件可能会受益于 OO 架构。

SOA API 应该比内部 API 更仔细地定义,因为它是外部 API。 此级别传递的数据类型应尽可能简单,没有内部逻辑。 如果存在某种属于该数据类型的逻辑(例如验证),则最好应该有一个服务负责在该数据类型上运行该逻辑。

My opinion is that SOA can be useful, at a macro level, but each service probably still will be large enough to need several internal components. The internal components may benefit from OO architecture.

The SOA API should be defined more carefully than the internal APIs, since it is an external API. The data types passed at this level should be as simple as possible, with no internal logic. If there is some logic that belongs with the data type (e.g. validation), there should preferably be one service in charge of running the logic on the data type.

眉目亦如画i 2024-07-18 10:43:34

SOA 是一种用于系统或应用程序之间通信的良好架构。

每个应用程序都定义一个“服务”接口,其中包含它将处理的请求和预期的响应。

这里的关键点是定义良好的服务和定义良好的接口。 就 SOA 而言,您的服务的实际实现方式并不重要。

因此,您可以自由地使用所有最新、最好的 OO 技术或任何其他适合您的方法来实现您的服务。 (我见过极端的情况,其中“服务”是由实际的人类在屏幕上输入数据来实现的——但一切仍然是教科书上的 SOA!)。

SOA is a good architecture for communicating between systems or applications.

Each application defines a "service" interface which consists of the requests it will handle and the responses expected.

The key points here are well defined services, with a well defined interface. How your services are actually implemented is irrelevant as far as SOA is concerned.

So you are free to implement your services using all the latest and greatest OO techniques, or any other methodology that works for you. ( I have seen extreme cases where the "service" is implemented by actual humans entering data on a screen -- yet everything was still text book SOA!).

鹿童谣 2024-07-18 10:43:34

我确实认为 SOA 仅对外部接口有用(一般来说,对于公司外部的接口),即使如此,只有在性能并不真正重要的情况下,您才不需要按顺序传递消息。

有鉴于此,我认为它们可以共存。 使用 OO 哲学让您的应用程序保持工作和通信,并且仅当需要外部接口(向第三方)时,才通过 SOA 公开它们(这不是必需的,但它是一种方式)。

我确实觉得 SOA 被过度使用,或者至少 SOA 架构被频繁地提出。 我确实不知道有哪个大型系统在内部使用 SOA,而且我怀疑它们可以。 它看起来更像是一种你可能只是用来做混搭或简单的天气预报类型请求的东西,而不是在其上构建严肃的系统。

I really think SOA is only useful for external interfaces (generally speaking, to those outside your company), and even then, only in cases when performance doesn't really matter, you don't need ordered delivery of messages.

In light of that, I think they can coexist. Keep your applications working and communicating using the OO philosophy, and only when external interfaces (to third parties) are needed, expose them via SOA (this is not essential, but it is one way).

I really feel SOA is overused, or at least architectures with SOA are getting proposed too often. I don't really know of any big systems that use SOA internally, and I doubt they could. It seems like more of a thing you might just use to do mashups or simple weather forecast type requests, not build serious systems on top of.

﹏雨一样淡蓝的深情 2024-07-18 10:43:34

我认为这是对面向对象的误解。 即使在 Java 中,方法通常也不是对象的一部分,而是的一部分(甚至这种“成员资格”对于面向对象来说也不是必需的,但这是一个不同的方法)主题)。 类只是类型的描述,因此这实际上是程序的一部分,而不是数据。

SOA 和 OO 并不矛盾。 服务可以接受数据,在内部将它们组织成对象,对它们进行处理,最后以所需的任何格式返回它们。

I think that this is a misunderstanding of object orientation. Even in Java, the methods are generally not part of the objects but of their class (and even this "membership" is not necessary for object orientation, but that is a different subject). A class is just a description of a type, so this is really a part of the program, not the data.

SOA and OO do not contradict each other. A service can accept data, organize them into objects internally, work on them, and finally give them back in whatever format is desired.

执妄 2024-07-18 10:43:34

我听 James Gosling 说过可以用 COBOL 实现 SOA。

如果您读过艾伦·凯 (Alan Kay) 自己对 OOP 起源的描述,它描述了一堆小型计算机通过交互来执行一些有用的操作。

考虑这个描述:

你的X应该由Y组成。 每个 Y 应该负责一个概念,并且应该可以根据其接口进行完整描述。 一个 Y 可以通过消息交换(根据其指定的接口)要求另一个 Y 做某事。

在某些情况下,X 可以由 Z 实现,Z 根据其接口进行管理。 不允许任何 X 直接访问另一个 X 的 Z。

我认为以下替换是可能的:

Term      Programing       Architecture
----    ---------------    ------------
  X         Program           System
  Y         Objects          Services
  Z      Data structure      Database
----    ---------------    ------------
result        OOP              SOA

如果您主要考虑封装、信息隐藏、松散耦合和黑盒接口,那么两者之间有相当多的相似之处。 如果您陷入多态性、继承等困境,那么您正在考虑编程/实现而不是架构,恕我直言。

I've heard James Gosling say that one could implement SOA in COBOL.

If you read Alan Kay's own description of the origins of OOP, it describes a bunch of little computers interacting to perform something useful.

Consider this description:

Your X should be made up of Ys. Each Y should be responsible for a single concept, and should be describable completely in terms of its interface. One Y can ask another Y to do something via an exchange of messages (per their specified interfaces).

In some cases, an X may be implemented by a Z, which it manages according to its interface. No X is allowed direct access to another X's Z.

I think that the following substitutions are possible:

Term      Programing       Architecture
----    ---------------    ------------
  X         Program           System
  Y         Objects          Services
  Z      Data structure      Database
----    ---------------    ------------
result        OOP              SOA

If you think primarily in terms of encapsulation, information hiding, loose coupling, and black-box interfaces, there is quite a bit of similarity. If you get bogged down in polymorphism, inheritance, etc. you're thinking programming / implementation instead of architecture, IMHO.

始终不够爱げ你 2024-07-18 10:43:34

如果您允许服务记住状态,那么它们就可以被视为调用时间可能很慢的大对象。

如果不允许他们保留状态,那么他们就像你所说的那样——数据操作员。

听起来您可能将系统划分为太多服务。 你们是否有书面的、双方同意的划分标准?

采用 SOA 并不意味着抛弃所有对象,而是将系统划分为可重用的大块。

If you allow your services to remember state, then they can just be considered to be big objects with a possibly slow invocation time.

If they are not allowed to retain state then they are just as you've said - operators on data.

It sounds like you may be dividing your system up into too many services. Do you have written, mutually agreed criteria for how to divide?

Adopting SOA does not mean throw out all your objects but is about dividing your system into large reusable chunks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文