领域驱动设计 - 在你开始之前清理一下你的观念
-------------------------------------之前园子里面有人翻译过,在家闲的没事又整理了一遍。
Start Developing a New Application
The answer is YES and NO! Yes we are doing good in delivering our projects. But NO! We are not doing good in maintaining and extending our projects!
当我们开始一个传统的商业应用程序时我们通常都做些什么?我们阅读说明和查找相应的功能,我们分解任务,大多数情况分解的目标是形成可行性分析和工作计划。我们进行可行性分析,给团队人员分配任务。在团队领导和其他开发人员的协作下我们设计数据框架,我们开始编码。
Think about all of the projects you have worked last few years in the traditional approach. Did you ever face any of the issues below?
- Your project has the same functionality implemented in the same way or different in different places.
- You have more than one object for the same item.
- You have objects that have properties that are not actually attributes of that object.
- You have no or very poor relationship among related items.
- Looking at your objects it is not possible to understand what actually the whole application is all about.
- 你的项目中以相同或不同的的方式实现了相同的功能在不同的地方。
- 你对同一个项目用了多个对象
- 你的项目存在不属于该对象实际属性的对象。
- 你的项目没有或很少的存在相应的关系。
- 看你的对象很难理解整个整个应用程序义。
But when you design in bottom-up approach, you first design for the granular functionalities, and you have little or no knowledge how this functionality will be used from the Top level and how the Top level functionalities will actually look like.
Have you ever heard that a developer of your team is talking like he does not have the domain knowledge of the whole application? Perhaps yes! I think you can understand the reason. Cause, the design of the application does not represent the domain of the system. And so, developers know only the portions they worked. This is Sad! Isn’t it?
So, is traditional approach – “Designing the application starting from database” a throw away concept? Not really! But if you have a complex application to develop, this bottom-up design approach does not dictate you to come up with a proper object oriented design.
我确定你已经经常的面试以上这些问题,但是你知道为什么吗?原因是,传统的开发模式不会引导我们自下而上的方式去设计系统。相反它会诱导我们自下而上的方式去设计系统,可见,当我们设计系统时需要知道应用程序想要做什么?什么是客户想要实现的目标?然而,从顶层目标来看你拿出不同的小功能来最终将实现所有的用户来实现最终顶层的目标。
当我利用自下而上的方式设计时,你第一个设计的是粒状的功能模块,你很少知道或不知道这个功能从顶层如何被使用和顶层的功能实际看起来怎么使用。
你是否听说过你团队开发成员的谈话,就像他不具备整个应用程序的领域知识?或许是的,我想你应该知道原因了,理由,应用程序的设计不代表系统的领域。因此,开发人员仅仅只知道自己工作分配的部分,这是很可悲的,不是吗?
那么传统开发模式-“应用程序设计从数据库开始”一个被遗弃的概念?并不是的,但是假如你开发复杂的应用程序,这种自下而上的开发设计模式对你来说并不是真正意义的面向对象的设计。
What is the solution then?
The solution is DDD (DOMAIN DRIVEN DESIGN).
What is DDD?
Domain-driven design is not a technology or a methodology. DDD provides a structure of practices and terminology for making design decisions that focus and accelerate software projects dealing with complicated domains.什么是 DDD?
Concepts to cover in this article:
- Understanding the Domain.
- Ubiquitous Language.
- Contexts and Bounded Contexts.
- Entities and Value Objects.
- Aggregates and Aggregate Roots.
- Persistence Ignorance.
- Repository.
- Domain Service.
In this article I will try to avoid becoming too technical, rather I will try to go through different concepts of DDD being close to the real world. I will try not to show any code here. Because I believe if you understand the concept and starts thinking in DDD way, implementation is easy. The toughest part is to tune your thinking process!
本文中涉及的概念:
Understanding the Domain
A sphere of knowledge, influence, or activity. The subject area to which the user applies a program is the domain of the software.- Wikipedia
Do you get a feeling what is domain from this definition? Can you tell what is the domain of the project you are working on at this moment? Can you tell what is the domain of the famous website YouTube?
In this article I would like to go through a real world example to give you the feeling how to start analyzing your project driven by your domain. This example may not be related with application development but as the goal is to tune our thinking top to bottom manner, it will be useful. But again, we will go through the technical terms of DDD too!
Let’s say you are engaged to design a building. The requirement is:
- You have a defined amount of land
- Your building will have 6 floors.
- Each floor will have 4 apartments.
What is your domain here?
The domain is Building(?). It could be. But note that, if you consider Building as your domain you may miss few granular details for your requirement. The building you are going to design must have design for apartments where people will live. So, a general term “Building” can make us miss few details. So, we may narrow down our domain to “Residential Building”.
Now, when you talk about your work with engineers and also with the people who engaged you to design the building, the term “Residential Building” is more meaningful for everybody concerned. Did you mark very small change in language here? The contractor is telling you to design a building where there will be 4 apartments in each of the 6 floors. Now, if you send an engineer to the site telling him we will need to construct a building there, they might not consider many attributes that a residential building must have. On the other hand if you use the term “Residential Building”, most likely he will come with a valid analysis.
This is how we come to an “Ubiquitous Language”.
了解领域
- 你有一片土地
- 你的建筑要盖6层
- 每层都将有4个公寓
Ubiquitous Language
The concept is simple, that developers and the business should share a common language that both understand to mean the same things, and more importantly, that is set in business terminology, not technical terminology.通用语言
通用语言的一些实例
上下文和界定上下文
- 电力供应
- 停车场
- 公寓
- 等等
- 实体
- 值对象
- 聚合和聚合根
实体
- 你在公寓的房间
- 在Facebook的联系人
- 在CodeProject的文章
值对象
- 如果那一个空间窗户才会被定义。
- 如果下单了订单详情才会存在。
- 如果问题被提问了才会有问题描述。
聚合和聚合根
- 房间,订单和问题是我们的聚合根。
- 另一方面说,订单详情和问题描述是我们的聚合。
- 问题的描述将不会被保存除非相应的问题被保存。
- 问题的描述将不会被检索除非相应问题被检索。
Repository commonly refers to a location for storage, often for safety or preservation.
- Wikipedia
隐式持久化
仓储
仓储的实施策略
1、第一步你有一个接口--IRepository 必须通用2、你将有一个抽象类实现IRepository接口。3、你将有一个INhRepository接口用于你的持久化机制它将继承IRepository4、你将有一个实现INhRepository的像:”NhRepository“5、最好你可以有一个通用的类实现Repository默认实现Respository的所有通用方法6、像NHGenericRepository继承NhRepository和实现IGenericNhRepository7、你有一个特别的仓储用于你的聚合根,扩展NHGenericRepository8、你的应用程序将使用服务定位器找到你应用程序将使用的仓储
领域服务
End Words
In this article I have tried to introduce the basic concepts and terminologies of Domain Driven Design with examples of real world. The goal was to make you feel comfortable with DDD world. But really developing applications with DDD is a big challenge. The more you love and practice DDD concepts while you design your object model, the more accuracy you will gain in your design. As I said before the most important thing is, you must think in Domain Driven Way. If you don't you will hugely suffer when your application is a real complex one.

浙公网安备 33010602011771号