DDD + JavaScript domain modeling example - Alex Lawrence

Jiedao jdon 2021-01-23 12:52:36
ddd javascript domain modeling example


This article uses a simple example to illustrate the domain modeling process . First step , Identify practical issues . Next , Find a solution . The next step is to create the initial domain model . after , The first embodiment is provided . then , Technical and logical challenges were discussed and resolved . Besides , It will also explain the differences between the domain model and its implementation . The post finally suggests that even for small projects , A problem centered and model driven approach should also be used .

 

Problem identification

Domain Driven Design emphasizes the problem to be solved and the knowledge domain involved . stay In my book , The domain model is defined as “ Focus on [..] Knowledge abstraction set for solving specific problems ”. It means , To create useful models , First, the problem needs to be identified . As a concrete example , Please consider the following questions when you are reading : I want to know how individual words appear in the text .

At first, the question seemed very useful , however , It doesn't really describe the problem , It's a hint of a particular solution . The related question is : I try to solve what problem by calculating the appearance of a single word ? Because I'm not a native English speaker , So I'm usually not sure about my vocabulary diversity . I want to measure... In some way . therefore , The real problem to be solved is to determine the lexical diversity of the text .

 

resolvent

Once the problem is discovered , Specific solutions can be identified . My idea is , I can determine the diversity of words by looking at the appearance of individual words . however , This approach implies that software alone is not a complete solution . contrary , It will only generate data that helps to derive lexical usage instructions . Determining the actual diversity will be done by me as a user .

 

Initial domain model

As mentioned earlier , A domain model is a set of knowledge abstractions . therefore , It doesn't have to have a specific form of expression or representation . what's more , Although models are usually expressed in some way , But each artifact is usually just a subset of information . For vocabulary examples , Knowledge abstraction can be conveyed through plain text . Please note that , This approach makes no statement about the complexity of the problem or domain model .

The purpose is to determine the degree of lexical diversity in the text .“ Text ” Represents a collection of words and punctuation marks .“ vocabulary ” It can be defined as a collection of individual words . The term “ diversity ” It includes the appearance of different words and their appearance . describe “ degree ” Represents the boundary and the discrete steps between the boundaries . for example , Lexical diversity is considered to be a subjective indicator that cannot be calculated by software .

To make a long story short , The domain model aspect can be summarized as follows :

  • Given the text , Vocabulary diversity should be determined
  • Text is a collection of words and punctuation
  • A word is a group of individual words
  • Lexical diversity is a subjective indicator determined by hand

 

First implementation

After defining the domain model , We can start implementing . For this example , Suppose the iterative software development process . result , There are low requirements for the integrity and correctness of the domain model . contrary , The above definition can be regarded as an initial draft for further development . Another way to understand it is , The following iterations are part of the experimental phase , Without building production software .

The following code is the first implementation to calculate the number of occurrences of a single word in text :

const countWordOccurrences = text => {
  wordOccurrences = {};
  text.split(' ').forEach(word => {
    if (wordOccurrences[word] == null) wordOccurrences[word] = 0;
    wordOccurrences[word]++;
  });
  return wordOccurrences;
};
const wordOccurrences = countWordOccurrences(`This is a basic example.
  Also, this is only one of many possible examples.`);
console.log(wordOccurrences);
/* output: {
  This: 1, is: 2, a: 1, basic: 1, 'example.\n': 1, '': 1, 'Also,': 1,
  this: 1, only: 1, one: 1, of: 1, many: 1, possible: 1, 'examples.': 1
} */

The sample usage and its output demonstrate the functionality of the initial solution .

 

Technical problems

There are some technical problems in the first implementation . These aspects are not due to defects in the model , It's about integrating implicit requirements right into the code : One problem is that punctuation is mistakenly seen as part of a word . The same is true for line breaks . Another problem is , Multiple spaces cause empty word entries to be created . These aspects are not suitable as explicit parts of the model , Because they should be seen as common sense .

The following code provides a redone implementation , Overcome the above problems :

const wordRegex = /[a-z0-9]{1}[a-z0-9-]*/gi;
const countWordOccurrences = text => {
  wordOccurrences = {};
  Array.from(text.matchAll(wordRegex), match => match[0]).forEach(word => {
    if (wordOccurrences[word] == null) wordOccurrences[word] = 0;
    wordOccurrences[word]++;
  });
  return wordOccurrences;
};
const wordOccurrences = countWordOccurrences(`This is a basic example.
  Also, this is only one of many possible examples.`);
console.log(wordOccurrences);

The second code solves the above technical problems by using regular expressions . This expression defines two rules . First , Every word must begin with an alphanumeric character . secondly , The first character can be followed by any combination of alphanumeric characters and dashes .

 

Model improvement

The implementation of the redesign is an improvement , But there are still problems . There are some problems that suggest that there are Bug defects :

  1. One Bug Yes, the implementation is case sensitive , This results in multiple entries of the same word with different case .
  2. The other is : The singular and plural forms of a word are considered different things .

You can update the domain model definition as follows :

  • Given the text , Vocabulary diversity should be determined
  • Text is a collection of words and punctuation
  • A vocabulary set is a single word
  • Lexical diversity is an indicator of language quality
  • Different case of a word is considered to be the same
  • The singular and plural of a word are considered to be the same

The last example provides an implementation that reflects the latest domain model :

const wordRegex = /[a-z0-9]{1}[a-z0-9-]*/gi;
const countWordOccurrences = (text, {asSingular}) => {
  wordOccurrences = {};
  text = text.toLowerCase();
  Array.from(text.matchAll(wordRegex), match => match[0]).forEach(word => {
    word = asSingular(word);
    if (wordOccurrences[word] == null) wordOccurrences[word] = 0;
    wordOccurrences[word]++;
  });
  return wordOccurrences;
};
const pluralize = require('pluralize');
const wordOccurrences = countWordOccurrences(`This is a basic example.
  Also, this is only one of many possible examples.`,
  {asSingular: pluralize.singular});
console.log(wordOccurrences);
/* output: {
  this: 2, is: 2, a: 1, basic: 1, example: 2,
  also: 1, only: 1, one: 1, of: 1, many: 1, possible: 1
} */

Reduce case sensitivity by making input text lowercase . For the combination of singular and plural forms , The implementation introduces dependency asSingular. This parameter must be assigned a word and return an operation in the singular form . for example , take pluralize load npm modular , And put its function singular() Pass in as a dependency . This approach ensures that the model behavior is expressed correctly , At the same time, it is not subject to specific dependence .

 

Models and code

There is a difference between the knowledge expressed by the domain model and the implementation . Think about it In my book The following excerpt from :“ The actual implementation may only reflect a subset of the underlying abstraction , And finally deal with irrelevant technical aspects .”  Examples of lexical diversity illustrate these two statements . One side , The implementation does not express the complete model , Because it only counts the number of occurrences of each word . secondly , It also deals with purely technical issues , For example, multiple spaces or line breaks .

 

DDD A small problem

Another aspect of this article is that so-called simple problems can bring them a lot of complexity . For large software projects with rich and complex domains , Domain Driven Design and its separate patterns are generally recommended . however , It's always helpful to start with the problem space and create useful conceptual models before implementing . Even for small functions like determining lexical diversity , Problem centered and model driven approaches are also valuable .

                   

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210123125041229d.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云