当前位置:网站首页>Introduction to Domain Driven Design - danhaywood

Introduction to Domain Driven Design - danhaywood

2021-05-04 14:02:13 Jiedao jdon

Today's enterprise applications are undoubtedly complex , We need to rely on some expertise ( persistence ,AJAX,Web Service etc. ) To finish their work . As a developer , We tend to focus on these technical details , It's understandable . But the truth is , A system that can't solve the business needs doesn't work for anyone , No matter how beautiful it looks or how well it builds its infrastructure .

Domain-driven design (DDD) Idea - First of all Eric Evans In his book of the same name - It's about focusing our attention on the core of the application , Focus on the inherent complexity of the business area itself . We will also focus on the core domain ( Unique business ) And support subdomains ( It's usually generic , Like money or time ) Distinguish , And put more design work on the core .

Domain driven design consists of a set of patterns for building enterprise applications from a domain model . In your software career , You may have come across many of these ideas , Especially if you are OO Language experienced developers . But applying them together will allow you to build systems that really meet the needs of the business .

In this paper , I'll introduce you to DDD Some of the main patterns of , Get to know some problems that novices seem hard to solve , And highlight some tools and resources , To help you apply DDD.

Code and models ......

Use DDD, We want to create a model of the problem domain , persistence , User interface and messaging can be created later , This is the business area that needs to be understood , Because in the system being built , You can tell the business of a company 、 Core competitiveness and competitors .( If not , So consider buying a packaged product ).

According to the model , We don't mean a chart or a group of charts ;  exactly , Charts are useful , But they're not models , It's just different models View ( See chart ). A model is a set of concepts that we choose to implement in software , In code , And any other software artifacts used to build the delivery system . let me put it another way , Code is the model . The text editor provides a way to use this model , Although modern tools also offer a lot of other visualizations (UML Class diagram , Entity relation diagram ,Spring beandocs,Struts / JSF Flow, etc. ).

chart 1: Models and views of models

This is a DDD The first concept in the pattern : Model Driven Design . This means being able to map the concepts in the model to the design / The concept of code ( Ideally ), A change in the model means a change in the code ;  Changing the code means that the model has changed .DDD You are not forced to use object orientation to build domains - for example , We can use the rules engine to build the model - But consider that the major enterprise programming languages are based on OO Of , Most models are essentially OO. After all ,OO Based on the modeling paradigm . The concept of the model will be represented as classes and interfaces , As a member of a class .

Talk about language

Now let's look at another fundamental principle of Domain Driven Design . Take a look back. : We want to capture the domain model of a problem domain , And we're going to be in the code / Software artifacts are expressed as some kind of understanding . To help us do this ,DDD It is advocated that domain experts and developers consciously use the concepts in the model to communicate . therefore , Domain experts don't describe new user story requirements based on fields or menu items on the screen , It's about the basic properties or behaviors that domain objects need . Similarly , Developers don't discuss data columns and new field types in database tables .

DDD We are strictly required to develop a Language everywhere . If an idea can't be expressed easily , So it actually has a concept behind the hint , This concept is missing from the domain model , And the team needs to work together to find out what the missing concept is . Once this is established , Then the results such as the screen in the database table or the new field on the column of the data table will be generated naturally .

image DDD equally , The idea of finding language everywhere is not new :XPers be called “ Name System ”, these years DBA Put data dictionaries together . But language everywhere yes A memorable term , It can be sold to business and technical people . Now? ,“ The whole team ” Agile practices are becoming mainstream , It also makes sense .

Model and context ......

Whenever we talk about models , It's always under certain circumstances ( In a certain context ), This context can usually be inferred from the usage of the end users using the system . such as , We have a trading system deployed to the front desk of the trader , Or the point of sale system used by supermarket cashiers , These users relate to the concept of the model in a specific way , And the terminology of the model makes sense to these users , But it doesn't have to mean anything to anyone outside that context .DDD be called Bounded context (BC). Each domain model exists in only one BC in ,BC Contains only one domain model .

I have to admit , When I first read about BC when , I don't see the point : If BC Just like the domain model , Why introduce a new term ? If only the end user and BC There was interaction , So maybe we don't need the term . However , Different systems (BC) And interact with each other , Send a file , The message , call API etc. . If we knew there were two BC Interact with each other , So we know that we have to pay attention to the conversion between concepts : Between this domain and other domains .

Setting clear boundaries around the model also means that we can start talking about these BC The relationship between . actually ,DDD To determine the BC A whole set of relationships between , So when we need to put different BC When they're linked together , We can reasonably determine what to do :

  • Published language published language: Interactive BC It's a common language ( For example, a bunch on the enterprise service bus XML Pattern ) To reach an agreement , They can interact with each other ;
  • Open host service open host service:BC Specify any other BC Protocols that can use their services ( for example RESTful Web service );
  • Shared kernel shared kernel: Two BC Using a common code kernel ( For example, a library ) As a common language , But whether they do something else in their own particular way ;
  • Release / subscribe customer/supplier: One BC Use another BC Service for , And it's another BC Our stakeholders ( client ). therefore , It can affect the BC Services provided ;
  • Follower conformist: One BC Use another BC Service for , But nothing else BC Our stakeholders . therefore , It USES “ Original sample ”( accord with )BC Provide an agreement or API;
  • Anticorrosive coating anti-corruption layer: One BC Using another service instead of stakeholders , But the aim is to introduce a set of adapters - Anti corruption layer to minimize BC The impact of the changes we depend on .

You can see , In this list , Two BC The level of cooperation and coupling is gradually decreasing ( See the picture 2). It has been used The language of the release , We from BC Establish a common standard with which to interact , We don't have the language , It's about owning the business they're in ( Maybe even industry standards ). With Open host service , We're still doing well ; BC Provide it as any other BC The function of the runtime service called , But with the development of services ( Probably ) Will remain backward compatible .

chart 2: Spectrum of bounded context relations

However , When we go When following the pattern , We just call and be called together ;  One BC Obviously succumbing to the other . If we have to buy with megabucks General ledger system integration of , That could be where we are . If we use The anti-corruption layer ,  So we usually integrate with legacy systems , But the extra layers separate us as much as possible . Of course , It takes money to implement , But it reduces the risk of dependence . The anti-corruption layer is also much cheaper than reimplementing legacy systems , At most, it distracts us from the core domain , At worst, it will end in failure .

DDD It is suggested that we make a BC chart To identify our BC And what we depend on or depend on BC, To determine the nature of these dependencies . chart 3 It shows me the past 5 Context mapping of systems that have been studied for about 20 years .

chart 3: Context mapping example

All of this is about bounded context graphs and BC Our discussion is sometimes called Strategic DDD, And for good reason . After all , When you think about it , To figure out what BC The relationship between them is very strategic : Which upstream systems will my system rely on , Is it easy for me to integrate with them , Do I use them , Do I believe in them ? So is the downstream : Which systems will use my services , How to expose my function as a service , Whether they will be good for me ? Misunderstood this , Your application can easily fail .

Layers and hexagons

Now let's turn inside and think about our own BC( System ) The architecture of . Basically ,DDD Only focus on domain layer , In fact, it doesn't have a lot to say about other layers : Like the presentation layer , Application layer or infrastructure layer ( Or the persistence layer ). But it does expect them to exist . This is a Layered architecture Pattern ( chart 4).

chart 4: Layered architecture

Of course , We've been building multi tier systems for years , But that doesn't mean we have to be good at it . exactly , Some of the mainstream technologies in the past - for example EJB 2, Yes , I'm talking about it ! - Has a positive impact on the idea that domain models can exist as meaningful layers . All business logic seems to penetrate the application layer or ( Worse ) The presentation layer , Leave a set of anemic domain objects as the empty shell of the data holder (DTO or VO), This is not DDD It means .

therefore , Be absolutely clear , There should be no domain logic in the application layer . contrary , The application layer is responsible for transaction management and security . In some architectures , It may also be responsible for ensuring that from the infrastructure / Domain objects retrieved in the persistence layer are properly initialized before interacting with them ( Although I prefer the infrastructure layer to do this ).

If the presentation layer has separate storage space ( For example, mobile terminals ), The application layer also acts as an intermediary between the presentation layer and the domain layer . The presentation layer usually deals with domain objects or other objects ( Data transfer object or DTO) Serializable representation of , Usually every “ View ” One . If these are modified , Then the presentation layer sends back any changes to the application layer , And the application layer determines the modified domain objects , And load them from the persistence layer , Then forward the changes to these domain objects .

One drawback of a layered architecture is : Its dependency is linear from the presentation layer to the infrastructure layer . however , We may want to support different implementations in the presentation layer and infrastructure layer . If we want to test our application, it must be like this :

  • for example ,FitNesse Tools that allow us to verify the behavior of our system from the perspective of end users . But these tools don't usually go through the presentation layer , It's going straight to the next level , The application layer . So in a sense ,FitNesse It's another kind of observer .
  • Again , We may have multiple persistence implementations . Our production implementation may use RDBMS Or something like that , But for testing and prototyping , We might have a lightweight implementation ( Maybe even in memory ), So we can simulate persistence .

We may also want to distinguish “ Inside ” and “ external ” Interaction between layers , Inside, I mean two layers in our system ( or BC) The interaction within , And external interactions span BC.

therefore , Don't think of our application as a set of layers , Another way is to think of it as a hexagon , Pictured 5 Shown . Our end users use viewers and FitNesse The test uses an internal client API( Or port ), And from others BC Call to ( for example ,RESTful For open host interaction , Or from ESB The call to the adapter is used for published language interaction ) Hit the external client port . For the back end infrastructure layer , We can see the persistence port used to replace the object storage implementation , Besides , Objects in the domain layer can call other services through external service ports BC.

chart 5: Hexagonal structure

Introduction to Domain Driven Design II

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504135746274P.html

随机推荐