关注点分离 - DAO、DTO 和 BO

发布于 2024-08-19 04:37:54 字数 752 浏览 2 评论 0原文

所以我有一个 DAO、DTO 和 BO。以下代码是结果:

// Instantiate a new user repository.
UserRepository rep = new UserRepository();

// Retrieve user by ID (returns DTO) and convert to business object.
User user = rep.GetById(32).ToBusiness<User>();

// Perform business logic.
user.ResetPassword();
user.OtherBusinessLogic("test");
user.FirstName = "Bob";

// Convert business object back to a DTO to save to the database.
rep.Save(user.ToDataTransfer<Data.DTO.User>());

所以我试图分离关注点,但我想摆脱此代码中的“转换”。 “转换”实际上作为扩展对象位于业务逻辑层(DTO 层对业务逻辑层一无所知)。 DTO 本身显然只存储数据,没有任何业务逻辑。 UserRepository 调用 DAO,并在 GetById 结束时使用 AutoMapper 从 DAO 映射到 DTO。 “转换”(ToBusiness 和 ToDataTransfer)完全按照他们的说法进行。

我的一位同事认为我可能必须有一个业务存储库,但认为它可能有点笨重。有什么想法吗?

So I have a DAO, DTO, and BO. The following code is the result:

// Instantiate a new user repository.
UserRepository rep = new UserRepository();

// Retrieve user by ID (returns DTO) and convert to business object.
User user = rep.GetById(32).ToBusiness<User>();

// Perform business logic.
user.ResetPassword();
user.OtherBusinessLogic("test");
user.FirstName = "Bob";

// Convert business object back to a DTO to save to the database.
rep.Save(user.ToDataTransfer<Data.DTO.User>());

So I am trying to separate concerns, but I want to get rid of the "converts" in this code. The "converts" are actually located in the business logic layer (DTO layer knows nothing of the business logic layer) as an extension object. The DTO itself obviously only stores data and has no business logic what-so-ever. The UserRepository calls the DAO and at the end of GetById uses AutoMapper to map from the DAO to DTO. The "converts" (ToBusiness and ToDataTransfer) do exactly as they say.

A colleague of mine thought I may have to have a Business Repository, but thought it might be a bit clunky. Any thoughts?

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

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

发布评论

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

评论(3

撩人痒 2024-08-26 04:37:54

我唯一困惑的地方是为什么需要调用 ToBusiness()ToDataTransfer()

存储库的职责是处理数据管理。它应该隐藏实现细节(以及业务对象和数据对象之间的转换)。

UserRepository 应该返回一个 User,而不需要任何转换。

UserRepository 还应该能够在不进行强制转换的情况下保留 User

如果所有转换都在存储库中处理,并且您的代码如下所示,那么代码会更加清晰:

UserRepository rep = new UserRepository();

User user = rep.GetById(32);

// Do Work Here

rep.Save(user);

My only source of confusion here is why the calls to ToBusiness<User>() and ToDataTransfer<Data.DTO.User>() are necessary.

The responsibility of the Repository is to handle data management. It should hide the implementation details (as well as the conversions between Business Objects and Data Objects).

A UserRepository should return a User without any casting needed.

The UserRepository should also be able to persist a User without casting.

The code would be much cleaner if all the casting was handled in the Repository and your code read as:

UserRepository rep = new UserRepository();

User user = rep.GetById(32);

// Do Work Here

rep.Save(user);
花开柳相依 2024-08-26 04:37:54

我通过创建业务服务层解决了这个问题。这样我就可以通过业务服务层访问功能,业务服务层又使用查询 DAL 并返回 DTO 的存储库。 DTO 通过由 DAL 填充来实现其目的,并帮助将数据传输到业务层(转换为业务对象)。

所以图如下:

DAL ->存储库(返回DTO)->服务(返回 BO)

它工作得很好,我能够将业务逻辑放在服务层中,该层将其从存储库本身中抽象出来。示例代码:

// UserService uses UserRepository internally + any additional business logic.
var service = new UserService();
var user = service.GetById(32);

user.ResetPassword();
user.OtherBusinessLogic("test");
user.FirstName = "Bob";

service.Save(user);

I resolved this by creating a Business Service layer. This way I can access functionality through the Business Service layer which in turn uses the Repositories which query the DAL and returns DTOs. The DTOs serve their purpose by being populated by the DAL and help transfer the data to the business layer (converted to business objects).

So the diagram is as follows:

DAL -> Repository (returns DTO) -> Service (returns BO)

It works very well and I am able to put business logic in the Service layer which abstracts it from the Repository itself. Sample code:

// UserService uses UserRepository internally + any additional business logic.
var service = new UserService();
var user = service.GetById(32);

user.ResetPassword();
user.OtherBusinessLogic("test");
user.FirstName = "Bob";

service.Save(user);
人疚 2024-08-26 04:37:54

这是我第一次看到 DTO 被转换为 BO,我通常发送 DTO 由 BO 类或方法使用。当 BO 完成并想要保存对 DTO 的修改时,它将其发送到 DAL 并使其持久化。

This is the first time I see a DTO being transformed into a BO, I normally send a DTO to be consumed by a BO class or method. When the BO is done and wants to save modifications to the DTO it sends it to the DAL and have it persisted.

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