如何在多个功能中重用验证码? - 洋葱建筑

How to reuse validation code in multiple features? - Onion architecture

根据我对洋葱架构的理解,域必须包含所有业务逻辑。强制执行数据库验证通常是通过使用服务来完成的。

我的代码受到此 repo https://github.com/asadsahi/AspNetCoreSpa 的启发,他们在其中使用功能,其中每个文件夹都包含应用程序层内特定功能的所有验证规则和逻辑。

共享多个功能的特定验证的最佳方式是什么?我应该创建服务并将其用于每个功能吗?

为什么他们将所有业务逻辑都移到了应用层,而领域实体没有任何逻辑?

我在这里找到了一篇很好的文章,讲述了我需要什么Dealing with Duplication in MediatR Handlers

Excluding sub-handlers or delegating handlers, where should my logic go? Several options are now available to me:

Its own class (named appropriately) Domain service (as was its original purpose in the DDD book) Base handler class Extension method Method on my DbContext Method on my aggregate root/entity As to which one is most appropriate, it naturally depends on what the duplicated code is actually doing. Common query? Method on the DbContext or an extension method to IQueryable or DbSet. Domain behavior? Method on your domain model or perhaps a domain service. There’s a lot of options here, it really just depends on what’s duplicated and where those duplications lie. If the duplication is within a feature folder, a base handler class for that feature folder would be a good idea.

In the end, I don’t really prefer any approach to the another. There are tradeoffs with any approach, and I try as much as possible to let the nature of the duplication to guide me to the correct solution.