简体   繁体   English

使用Composition的通用JavaScript模型

[英]Generic JavaScript Models using Composition

I am currently working on a Node.js application with a couple of JavaScript model like this: 我目前正在使用一些像这样的JavaScript模型的Node.js应用程序:

function Role(data) {
  this.data = data;
  ...
}

Role.prototype.save = function(...) {...}

Role.findById = function(...) {...}
Role.findAll = function(...) {...}

All of them are using the same (similar) logic for most of the functions, but some need a different implementation for saving and so on. 对于大多数函数,它们都使用相同(相似)的逻辑,但有些需要不同的实现来保存等等。 So my idea was to refactor them by using some kind of composition. 所以我的想法是通过使用某种组合来重构它们。 My current solution is a combination of using inheritance for the prototype functions and an Adapter for the static functions. 我目前的解决方案是使用原型函数的继承和静态函数的适配器的组合。 It looks like this. 看起来像这样。

var _adapter = new DatabaseAdapter(SCHEMA, TABLE, Role);

function Role(data) {
  Model.call(this, _adapter, Role._attributes)
  this.data = data;
  ...
}

Role._attributes = {
  name: ''
}

Role.prototype.save = function(...) {...}

Role.findById = function(...) {
  _adapter.findById(...);
}

Role.findAll = function(...)
{
  _adapter.findAll(...)
}

But, I am not really happy with my current solution, because the developers need to know a lot of implementation details to create a new Model. 但是,我对目前的解决方案并不满意,因为开发人员需要了解大量实现细节才能创建新模型。 So, I hope someone could show me a better approach to solve my problem. 所以,我希望有人能告诉我一个更好的方法来解决我的问题。

Thanks, Hendrik 谢谢,亨德里克

Edit After some research I came up with the following solution: 编辑经过一些研究后,我想出了以下解决方案:

Role model: 榜样:

 Role.schema = 'db-schema-name';
 Role.table = 'db-table-name';
 Role.attributes = { /* attributes of the model */ }

 Role.prototype.save = genericSaveFunc;

 Role.findById = genericFindByIdFunc;
 ...

generic save: 通用保存:

 function genericSaveFunc(...) {
   if (this.id) {
     // handle update
     // attributes in 'this' will be updated 
   } else {
     // handle create
     // attributes in 'this' will be updated 
   }
 }

static generic function findById: 静态泛型函数findById:

 function genericFindByIdFunc(...) {
   /* use this.schema && this.table to create correct SELECT statement */
 }

The model creation could be wrapped into a factory function. 模型创建可以包装到工厂功能中。 The good part of this solution is the simple creation of new models with different kind of functionality (eg only add save and findById to a model). 该解决方案的优点是可以简单地创建具有不同功能的新模型(例如,只将savefindById添加到模型中)。 But I don't know if it is a good idea to rely on the calling context of the generic functions? 但我不知道依赖泛型函数的调用上下文是否是一个好主意?

Since your using a prototype-based language. 由于您使用的是基于原型的语言。 There is no need to try to do class-inheritance. 没有必要尝试进行类继承。 Take a look at stampit it will make it very easy for you to do composition the JavaScript way :) 看看stampit这将使它很容易为你做组成的JavaScript的方式:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM