在洋葱型架构中,实体是否应该跨越外层? [英] In an Onion type architecture, should the entities cross the outer layer?

查看:24
本文介绍了在洋葱型架构中,实体是否应该跨越外层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试理解这种新的架构,其名称可以是洋葱架构、清洁架构、端口和适配器等.

I've been trying to understand this new kind of architecture which names can be Onion architecture, Clean architecture, Ports and Adapters, etc.

如果我采用端口和适配器的抽象,当我为特定端口调整我的应用程序时,我可以从我的应用程序内部为端口提供一个实体吗?还是我总是应该调整实体以适应端口?

If I take the abstraction of Ports and Adapters, when I adapt my application for a particular port, is it ok for me to give the port an entity from inside my application? Or am I always supposed to adapt the entities also, to fit the port?

示例:

假设我有一个 Customer 实体.我有一个使用我的应用程序的 UI.我的 UI 通过适配器调用 getCustomerById(123).反过来,我的适配器将调用我的应用程序,使用注入的存储库有效地检索客户,并且它将对其执行某种格式设置和日志记录,一旦客户准备好,它将返回到我的 UI.我的问题是,我的 Customer 对象按原样返回到我的 UI.这意味着我的 UI 引用了我的 Core 项目中的 Customer 类.然后我的 UI 继续使用那个 Customer 对象来做一些事情,可能会改变它的名称等,并最终再次调用适配器来更新客户(客户).

Say I have a Customer entity. I have a UI that uses my application. My UI calls through an Adapter to getCustomerById(123). In turn, my adapter will call through to my application, effectively retrieving a Customer using the injected Repository, and it will perform some sort of formatting on it and logging and what not, once the Customer is ready, it is returned to my UI. My question here is, my Customer object is returned as is to my UI. This means my UI has a reference to the Customer class from my Core project. My UI then goes on using that Customer object to do things, maybe change it's name, etc. and eventually calls the adapter again to updateCustomer(customer).

这样可以吗?我的 UI 可以使用我的应用程序核心内部的 Customer 类吗?或者我应该让我的 Customer 适应一个新的 Customer 对象,比如 UICustomer,然后让我的 UI 使用它,在适配器级别在 Customer 和 UICustomer 之间来回映射?

Is this ok to do? Is it ok that my UI uses the Customer class from inside my application core. Or should I instead adapt my Customer to a new Customer object say UICustomer and have my UI work with that instead, mapping back and forth between Customer and UICustomer at the adapter level?

推荐答案

很好的问题.我有一个可能有用的例子.https://bitbucket.org/jeffreypalermo/onion-architecture

Great question. I have an example that might be useful. https://bitbucket.org/jeffreypalermo/onion-architecture

对于简单的应用程序,使用 Core 域模型对象就可以了.这些被设计成不会有讨厌的依赖关系,所以它们工作得很好并且非常便携.它们可以跨层移动而不会造成任何问题.

For simple applications using the Core domain model objects can be just fine. These are designed to not have nasty tentacles of dependencies hanging off of them, so they work very well and are quite portable. They can travel across the layers without causing any issues.

这篇关于在洋葱型架构中,实体是否应该跨越外层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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