如何在域对象中存储业务逻辑? [英] How to store business logic in domain object?

查看:18
本文介绍了如何在域对象中存储业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将显示一个视图模型的网格 class M {string A, int B, float C, int D, .....}:

I will display a grid of a view model class M {string A, int B, float C, int D, .....}:

  1. 如果数字为负数,则将其呈现为红色.
  2. 如果 A 在 'xxx', 'zzz' 和 B = 0 或 D > 中,突出显示单元格的背景.200...
  3. 如果 E > 将行的背景设置为灰色100和F<0....

这些被认为是业务逻辑吗?如果有这些逻辑,应该把它们放在哪里?对于(3),我猜可以在视图模型中创建一个新的只读属性?但它让 M 不再是 POJO/POCO 了吗?我读到业务逻辑应该在域对象中.域对象是用POJO/POCO实现的吗?

Are these considered business logic? Where to put these logic if they are? For (3), I guess a new read-only property can be created in in the view model? But it make the M not POJO/POCO any more? I read that the business logic should be in domain object. Is the domain object implemented using POJO/POCO?

是否有任何代码示例显示业务逻辑如何存储在域对象中?

Is there any code sample shows how the business logic is stored in domain objects?

推荐答案

有几种方法可以实现您的目标.您在描述中多次提到术语视图-模型",这让我觉得您正在尝试使用设计模式MVVM"(模型-视图-视图模型),该模式在使用 WPF 或 Silverlight 技术时很流行,因为它支持针对视图的数据绑定,但也可以扩展到其他技术.

There are several ways to achieve what you are trying to do. You mention the term 'View-Model' a few times in your description, which makes me think you are trying to use the design pattern 'MVVM' (Model-View-ViewModel), which is popular when using WPF or Silverlight technology due to things like it's support for databinding against the view, but can be extended to other technologies too.

在 MVVM 中,层分为模型、视图模型和视图.

In MVVM, the tiers are split into the Model, the View-Model and the View.

模型本质上是您的域,并且仅用于特定于域的建模.您的应用程序实体应位于此处,但不应进行域实体的计算或操作.例如.如果是自行车领域,则您的自行车"和骑手"类应该位于此处,但不应有与计算比赛获胜者或在应用程序 GUI 中显示获胜者的颜色相关的代码.

The Model is essentially your domain, and is used only for Domain specific modelling. Your applications entities should live here, but there should be no calculations, or manipulation of the domain entities. eg. If it was a cycling domain, your 'Bike' and 'Rider' classes should live here, but there should be no code relating to calculating the race winners, or what colour to display the winner in your application GUI.

View-Model 是您准备域实体以显示在您的视图(用户界面)中的地方.您几乎可以通过将域实体包装到新类中来实现这一点,这些类在构造函数中使用域对象,然后公开或将现有属性转换为您想要在视图中显示的属性.在自行车类比中,您的 Bike 对象可能具有 Make、Model、Cost、RRP、TimeBuilt 属性,因此在视图模型中,我将公开 Make、Model 属性,然后翻译Cost 和 RRP 以及零售价的保证金(如前端所示).

The View-Model is where you prepare your domain entities to be presented into your View (User interface). You pretty much achieve this by wrapping your domain entities into new classes, that take the domain object in the constructor, but then expose, or translate the existing properties into the ones you want to be displayed in the View. In the cycling analogy your Bike object might have Make, Model, Cost, RRP, TimeBuilt properties, so in the view model I would expose the Make, Model properties, but then translate the Cost and RRP along with margin to come up with the retail price (as seen on the front end).

视图,正如您可能已经猜到的那样,是显示此信息的位置.这可能是桌面、移动或 Web 前端 - 这并不重要.这也是应该实现 UI 渲染的地方.如果我想向我的客户强调超值优惠,并将任何零售价非常优惠的自行车涂上绿色 - 我会在这里做.

The View, as you might have guessed is where this information is displayed. This might be a desktop, mobile or web front end - it doesn't really matter. This is also where any rendering of the UI should be achieved. If I wanted to highlight great deals to my customers, and colour any Bikes with very good retail prices in green - I would do it here.

因此,当涉及到您的示例时,应该在视图中实现对象显示方式的格式化.即使您不使用 MVVM,您也可以通过将域对象扩展到要部署和操作的 Wrapper 类中来获得非常相似的结果.你的规则如

So, when relating to your example, formatting how the object is displayed should be achieved in the View. Even if your not using MVVM, you can achieve very similar results by extending your domain objects into Wrapper classes to be deployed and manipulated. Your rules such as

如果 A 在 'xxx'、'zzz' 和 B = 0 或 D > 200 中,突出显示单元格的背景

Highlight the background of the cell if A is in 'xxx', 'zzz' and B = 0 or D > 200

可以在您的视图模型类中以布尔值建模,为您提供所需的效果,而不会用非特定代码污染您的域对象.

can be modeled in a boolean on your view model class, giving you the effect you need, without polluting your domain objects with non-specific code.

无论架构选择(MVC、MVP、MVVM 等)如何,这些都是很好的开发指南,因为它们在您的应用程序层之间部署关注点分离.

Regardless of architectural choice, (MVC, MVP, MVVM etc) these are good guidelines to develop by, as they deploy separation of concerns between the layers of your application.

这篇关于如何在域对象中存储业务逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆