Node.js应用程序中的领域驱动devise

TL; DR; 我正在寻找DDD node.js应用程序的小例子。


嗨,

我要创build节点应用程序。 我不知道,我无法find任何应​​用程序与业务逻辑分离的例子。

好的,有一些例子: https : //github.com/adrai/node-cqrs-domain – 但是这是整个CQRS与事件采购实施。

我的想法是这样做的:

//domain/book.js function Book(title, author) { this._title = title; this._author = author; } // domain methods ... //infrastructure/persistance/repository/book-repository.js function BookRepository() {} BookRepository.prototype.save(book) { var bookModel = mappers.mapToOrm(book); return bookModel.save(); } // [...] get, getAll, getNextId //infrastructure/persistance/orm/book.js //using http://bookshelfjs.org/ var Book = bookshelf.Model.extend({ tableName: 'books' }); //infrastructure/mappers/book-mapper.js function mapToOrm(book) { //mapping [...] return new persistance.Book(); } function mapToDomain(domain) { //mapping [...] return new domain.Book(); } 

但另一方面,我从来没有见过类似的解决scheme(域模型,orm模型,存储库和映射器)。 我以正确的方式思考? 也许没有理由在node.js应用程序中分离域中的业务逻辑。 如果是这样,为什么? 如果没有,你可以给我一个DDD实施的例子,或改善我的代码?

[2017年1月13日]

我已经在TypeScript中创build了示例应用程序。 现在没有存储库,没有太多的服务。 问题和拉请求是受欢迎的。 https://github.com/dawiddominiak/ddd-typescript-bin-packing-problem-solution

我对Node.js世界很陌生。

但是我相信,如果你使用带有Node的TypeScript来完成你的工作你可以强制使用大部分的DDD原则。

在node.js中,问题“同时具有优势”,并没有像C#或Java这样的OOP语言中的限制。 而这种JavaScript自由的“混乱” 使得非常难以创build强健复杂的DomainModel和Business逻辑

我现在正在做同样的事情,而且我来自Ruby世界。 所以,让我做两件事情:

  1. 把你指向我见过的领域驱动devise的最好的Ruby实现,Hanami: http ://hanamirb.org/guides/models/overview/你可以用它作为参考。

  2. 讨论我正在寻找的东西(现在,就像我input的那样)尝试在Node中find类似物。

我发现这个网页: https : //github.com/sindresorhus/awesome-nodejs

其中目录大量的高品质/高度受欢迎的节点包。

首先,我们需要一些将为我们的领域模型进行validation和模式构build的东西。 只要看看数据validation部分的第一个条目,Joi似乎是一个不错的select:

https://github.com/hapijs/joi

对于域对象的持久性,我只是build立一个存根对象,从Hanami的接口借用:

 var repo = { find: function(entity_name, id) { // - Fetch an entity from the collection by its ID }, create: function(entity_name, data) { // – Create a record for the given data and return an entity }, update: function(entity_name, id, data) { // – Update the record corresponding to the id and return the updated entity }, delete: function(entity_name, id) { // – Delete the record corresponding to the given entity }, all: function(entity_name) { // - Fetch all the entities from the collection }, query: function(entity_name, query_object) { }, first: function(entity_name) { // - Fetch the first entity from the collection }, last: function(entity_name) { // - Fetch the last entity from the collection }, clear: function(entity_name) { // - Delete all the records from the collection } } module.exports = repo 

无论你select使用BookshelfSequelize还是LoopBack框架,都可以编写一个适合上述接口的对象,然后完成与这些框架集成的肮脏工作。

如果我尝试不同的ORM,我会为上面的每个创build一个不同的回购对象。 请注意,正如我写的那样,回购是一个知道不同实体的单例,以及如何坚持它们。 在许多情况下,这无疑将以每个实体为基础委托给不同的存储库对象。 但是,这可能并不总是如此。 一个简单的内存回购,可以只为每个实体有一个对象数组。

这留下了服务/交互 – 实际上起作用的function/类。 这些都很简单 – 他们是那些采取域对象,执行一些业务逻辑,并在CRUD的情况下,调用存储库。 一个可能是语法错误的例子:

 const repository = require('./myFileRepository') function createBook(bookEntity) { if(bookEntity.valid?) { repository.create('book', bookEntity) return true } else { return { error: 'book not valid' } } } module.exports = createBook 

对于服务function,我今天才了解了Node机器,他们看起来像一个非常聪明的想法: http : //node-machine.org

他们似乎是单子+文档的JS尝试。 所以我正在考虑像这样写。

无论如何,鉴于你的post已经有一年了,你可能已经开始了。 希望这可以帮助你/社区!

许多人会认为,JavaScript不适合将复杂问题build模成领域模型,然后编码。 如果这个领域是在商业,工业和商业领域,而不是在计算机或数据科学领域,情况尤其如此。

我不是说在JavaScript中不能创build一个领域模型。 就像可以用C创build一个一样,但这是否意味着应该?

你给的例子在领域驱动的devise中使用了一些术语,但是忽略了应用它的全部目的和精神。