如何避免使用域驱动设计有非常大的对象 [英] How to avoid having very large objects with Domain Driven Design

查看:88
本文介绍了如何避免使用域驱动设计有非常大的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在关注域驱动设计来实现一个大型网站。



然而,通过将行为放在域对象上,我们最终得到了一些非常大的类。



例如在我们的WebsiteUser对象上,我们有很多方法 - 例如处理密码,订单历史记录,退款,客户细分。所有这些方法都与用户直接相关。许多这些方法将内部委托给其他子对象,但是

这仍然导致一些非常大的类。



我很想避免暴露许多孩子对象
,例如user.getOrderHistory()。getLatestOrder()。



可以使用什么其他策略来避免这个问题?

解决方案

您所看到的问题不是由域驱动设计引起的,而是由于缺乏关注点的分离。域驱动设计不仅仅是将数据和行为放在一起。



我建议的第一件事是花一天时间,阅读域驱动设计快速可从Info-Q免费下载。这将提供不同类型的域对象的概述:实体,价值对象,服务,存储库和工厂。



我建议的第二件事是去阅读在单一责任原则上。



第三件事我建议您开始沉浸在测试驱动开发中。首先通过编写测试来学习设计不一定会使您的设计变得很好,他们往往会引导您进行松散耦合的设计,并在早期发现设计问题。



在示例中你提供的,WebsiteUser绝对有太多的责任。实际上,您可能根本不需要 WebsiteUser ,因为用户通常由 ISecurityPrincipal 表示。 p>

鉴于缺乏业务背景,我们建议您如何应对自己的设计,但我会首先建议您通过创建一些索引卡来代表每个您的系统中的主要名词(例如客户,订单,收据,产品等)。在顶部写下候选人的名字,你觉得什么责任是左派的固有的,以及与它合作的课程。如果某些行为不觉得它属于任何对象,那么它可能是一个很好的服务候选(即AuthenticationService)。把你的大学的卡片放在桌子上,并讨论。不要做太多的事情,因为这真的只是作为头脑风暴的设计练习。这可能比使用白板更容易一些,因为你可以移动东西。



长期来看,你应该真正拿起这本书域驱动设计由埃里克·埃文斯。这是一个很大的阅读,但很值得你的时间。我还建议您选择
敏捷软件开发,原理,模式和实践
C#中的敏捷原则,模式和实践,具体取决于您的语言偏好。


We are following Domain Driven Design for the implementation of a large website.

However by putting the behaviour on the domain objects we are ending up with some very large classes.

For example on our WebsiteUser object, we have many many methods - e.g. dealing with passwords, order history, refunds, customer segmentation. All of these methods are directly related to the user. Many of these methods delegate internally to other child object but
this still results in some very large classes.

I'm keen to avoid exposing lots of child objects e.g. user.getOrderHistory().getLatestOrder().

What other strategies can be used to avoid this problems?

解决方案

The issues you are seeing aren't caused by Domain Driven Design, but rather by a lack of separation of concerns. Domain Driven Design isn't just about placing data and behavior together.

The first thing I would recommend is taking a day or so and reading Domain Driven Design Quickly available as a free download from Info-Q. This will provide an overview of the different types of domain objects: entities, value objects, services, repositories, and factories.

The second thing I would recommend is to go read up on the Single Responsibility Principle.

The third thing I would recommend is that you begin to immerse yourself in Test Driven Development. While learning to design by writing tests first won't necessarily make you designs great, they tend to guide you toward loosely coupled designs and reveal design issues earlier.

In the example you provided, WebsiteUser definitely has way too many responsibilities. In fact, you may not have a need for WebsiteUser at all as users are generally represented by an ISecurityPrincipal.

It's a bit hard to suggest exactly how you should approach your design given the lack of business context, but I would first recommend doing some brain-storming by creating some index cards representing each of the major nouns you have in your system (e.g. Customer, Order, Receipt, Product, etc.). Write down candidate class names at the top, what responsibilities you feel are inherent to the class off to the left, and the classes it will collaborate with to the right. If some behavior doesn't feel like it belongs on any of the objects, it's probably a good service candidate (i.e. AuthenticationService). Spread the cards out on the table with your colleges and discuss. Don't make too much of this though, as this is really only intended as a brainstorming design exercise. It can be a little easier to do this at times than using a whiteboard because you can move things around.

Long term, you should really pick up the book Domain Driven Design by Eric Evans. It's a big read, but well worth your time. I'd also recommend you pick up either Agile Software Development, Principles, Patterns, and Practices or Agile Principles, Patterns, and Practices in C# depending on your language preference.

这篇关于如何避免使用域驱动设计有非常大的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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