简体   繁体   English

DDD架构 - 放置常用方法/助手的位置

[英]DDD Architecture - Where To Put Common Methods/Helpers

According to this question on Stack Overflow, in DDD architecture "helper" classes can be in different layers depending on their purpose. 根据Stack Overflow上的这个问题 ,在DDD架构中,“helper”类可以根据其目的在不同的层中。 For example a helper that formats something in a user friendly way would go in the UI. 例如,以用户友好的方式格式化某些内容的助手将进入UI。 A database helper would go in infrastructure. 数据库帮助程序将进入基础结构。

But what about helpers that can be used by more than one layer? 但是可以由多个层使用的助手呢? For example age calculation. 例如年龄计算。 Age might be required in the model layer for business logic. 业务逻辑的模型层可能需要年龄。 It is used by more than one entity, so it should not be in a specific entity. 它由多个实体使用,因此不应该在特定实体中。 Also there are places where age is required merely for display purposes in the UI. 此外,还有一些地方需要年龄仅用于UI中的显示目的。 Similarly, I have string functions that can be used by more than one layer. 同样,我有多个字符串函数,可以由多个图层使用。 For example my custom Right and Left methods could be used for formatting in the UI, but they might also be used in the model, for example conditional logic based on a prefix. 例如,我的自定义Right和Left方法可以用于UI中的格式化,但它们也可以在模型中使用,例如基于前缀的条件逻辑。

So where should these common methods go? 那么这些常用方法应该去哪里? My setup is like this: 我的设置是这样的:

  • UI UI
  • Application 应用
  • Model 模型
  • Infrastructure 基础设施

Model is core and has no dependencies on infrastructure, so the common helpers cannot go in infrastructure. 模型是核心,并且不依赖于基础架构,因此常见的帮助程序无法进入基础架构。 I am considering two options: 我正在考虑两种选择:

1) Have another layer called Common or similar that can be used by any layer. 1)具有可以由任何层使用的另一个名为Common或类似的层。 This would create a dependency between Model and Common. 这将在Model和Common之间创建依赖关系。

2) Duplicate the helper logic in whatever layer is needed. 2)在需要的任何层中复制辅助逻辑。 Eg Have an Age helper in UI AND have an Age helper in the Model. 例如,在UI中有一个Age帮助器,并且在Model中有一个Age帮助器。 This would violate DRY, but would not require the domain to have a dependency on a "Common" layer. 这会违反DRY,但不会要求域依赖于“公共”层。

Which option is better? 哪个选项更好? Is it OK for the Model layer to have a dependency on a "common" layer? 模型层是否可以依赖“公共”图层?

UPDATE: 更新:

In the 2.5 years since this question was asked I have concluded: 在问这个问题后的2。5年里,我得出结论:

  1. Things like Right, Left, etc that compensate for limitations in the framework, belong in a "Common" utilities / helper component that even the Model/Domain layer has a dependency on. 像Right,Left等补偿框架中的限制的东西属于“公共”实用程序/辅助组件,即使模型/域层依赖于它。
  2. The "Common" utilities / helper component should not be very big. “Common”实用程序/帮助程序组件不应该很大。 With more experience I found that many things I thought of as helpers actually belong in the domain model. 凭借更多的经验,我发现许多我认为是帮助者的东西实际上都属于领域模型。
  3. Age belongs in its own class in the domain layer. Age属于域层中自己的类。 Much like things like address, phone number and money I consider these things as value objects. 就像地址,电话号码和金钱一样,我把这些东西视为价值对象。 Understanding value objects really was key to my understanding of creating reusable domain classes that can be incorportated into other classes. 理解值对象确实是我理解创建可以包含在其他类中的可重用域类的关键。

The string helpers are slightly different I think - in that case your custom Right and Left methods are actually compensating for a limitation in the built-in string type of your language/platform - it doesn't have anything to do with your application as such so in this case it's fine to have it globally accessible. 我认为字符串帮助器略有不同 - 在这种情况下,您的自定义Right和Left方法实际上补偿了您的语言/平台的内置字符串类型的限制 - 它与您的应用程序没有任何关系所以在这种情况下,可以全局访问它。

The age calculation example is a little more interesting as it encapsulates behaviour/logic which is very much an intrinsic part of your application/domain. 年龄计算示例更有趣,因为它封装了行为/逻辑,这是应用程序/域的固有部分。 In that case it may be worth evaluating on a case-by-case basis whether it's worth duplicating the behaviour in each layer (violating DRY in favour of SRP). 在这种情况下,可能值得根据具体情况评估是否值得重复每一层中的行为(违反DRY以支持SRP)。 That's a tough decision to make. 这是一个艰难的决定。 Although they may be identical now, by duplicating the behaviour you are giving the two methods the chance to diverge from each other in the future. 虽然它们现在可能完全相同,但通过复制行为,您可以使这两种方法在未来相互分离。 This would typically happen if they had different reasons to change . 如果他们有不同的改变理由,通常会发生这种情况。

From my experience, having a CommonInfrastructure (or some such) component/layer which contains all the helper classes/methods is the right way to go. 根据我的经验,拥有包含所有帮助类/方法的CommonInfrastructure(或某些此类)组件/层是正确的方法。 both String helpers and Age helpers as you described can go in there (along with DateTime helpers, dictionary, dynamics, Linq, whatever). 如你所描述的String助手和Age助手都可以进入(与DateTime助手,字典,动态,Linq等)。

it should be a library that could easily be carried on to the next project, as it would be so generic and without any dependency on current domain logic, and contain useful code for all-purposes. 它应该是一个可以轻松传递到下一个项目的库,因为它非常通用,不依赖于当前的域逻辑,并且包含用于所有目的的有用代码。

the only reason to violate DRY would be if you needed Javascript code on the UI and C# code on your backend, in which case you'd need to duplicate that common infrastructure code (unless you come up with a more elaborate solution to keep it DRY). 违反DRY的唯一原因是,如果您需要在UI上使用Javascript代码并在后端使用C#代码,在这种情况下,您需要复制该通用基础架构代码(除非您想出一个更精细的解决方案来保持DRY) )。

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

相关问题 在DDD架构中放置辅助类的位置? - Where to put a helper class in a DDD architecture? 在 DDD 架构中,我应该在哪里放置与按角色用户过滤数据相关的查询逻辑 - Where should I put query logic related to filtering data by role user in DDD architecture 在 Nx + Angular monorepo 架构中,我应该在哪里放置特定于一个库的测试助手? - In Nx + Angular monorepo architecture, where should I put testing helpers that are specific to just one library? DDD放置图像转换的位置 - DDD Where to put image conversion 何处在DDD +分层架构中实现Automapper - Where to implement Automapper in a DDD + layered architecture 在DDD架构中,我在哪里保留帮助类? - In DDD Architecture where do I keep helper classes? 我应该在 DDD 架构中的什么地方放置读取模型? - Where should I place read models in the DDD architecture? 具有洋葱架构的 DDD 端口和适配器,何去何从? - DDD Ports and Adapters with Onion architecture, what goes where? 我应该将常见的实用程序方法放在 Asp.Net Core MVC 项目中的什么位置? - Where should I put common utility methods in an Asp.Net Core MVC-project? SOA(ish)架构,在哪里放置公共代码? - SOA (ish) architecture, where to place common code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM