简体   繁体   English

使用Mongoose和Express.js进行MVC的OOP

[英]OOP with MVC using Mongoose & Express.js

I'm creating an Express.js app in which I want to use the MVC pattern and Mongoose for the document mapping to a MongoDB database. 我正在创建一个Express.js应用程序,我想在其中使用MVC模式和Mongoose将文档映射到MongoDB数据库。 I've created a folder for models and I want to derive everything from (Javascript's version of) abstract classes for better code organization. 我已经为模型创建了一个文件夹,我希望从(Javascript的版本)抽象类派生出更好的代码组织。

I'm confused about what the best way is to organize the abstract classes and set default values that each instance of the models should be. 我很困惑组织抽象类的最佳方法是什么,并设置每个模型实例应该是的默认值。 For example, one way is to use Mongoose Schemas for abstract classes, and then use Mongoose models for the models themselves: 例如,一种方法是将Mongoose Schema用于抽象类,然后将Mongoose模型用于模型本身:

Feline.js: Feline.js:

var mongoose = require('mongoose');

var Feline = mongoose.Schema({
  size: 'Number'
});

Feline.methods.getSize = function () {
  return this.size;
}

module.exports = Feline;

HouseCat.js: HouseCat.js:

var mongoose = require('mongoose')
, FelineSchema = require('./Feline.js');

var HouseCatModel = mongoose.model('HouseCat', FelineSchema)
, HouseCat = new HouseCatModel({
  size: 1 //Domesticated cats are small
});

module.exports = HouseCat;

There are a few problems with this design. 这种设计存在一些问题。 For one, I would think there must be a better way to set specific properties for the each model without instantiating a new model object each time the client wants to create a new instance of a model type. 首先,我认为必须有一种更好的方法来为每个模型设置特定属性,而无需在每次客户端想要创建模型类型的新实例时实例化新模型对象。 For another, using this scheme, Mongoose has to be required in every model file, and the code is custom-tailored to use mongoose, which means it will be difficult to switch to another ODM if we want to do that in the future. 另一方面,使用这种方案,必须在每个模型文件中都需要Mongoose,并且代码是定制的,以便使用mongoose,这意味着如果我们希望将来这样做,将难以切换到另一个ODM。

Is there any better way of coding this? 有没有更好的编码方式? And is there any design pattern which is easy enough to implement in Node that will allow for easy changing of the ODM? 是否有任何设计模式很容易在Node中实现,以便轻松更改ODM?

As mongoose is specific to mongodb, this will be a hard task to abstract its behaviour. 由于猫鼬特别针对mongodb,因此抽象其行为将是一项艰巨的任务。

The easiest way to do it is to set an interface for all ODMs and use an adapter pattern where mongoose is an "adaptee". 最简单的方法是为所有ODM设置一个接口,并使用适配器模式 ,其中mongoose是一个“适配器”。 Then, you can use a module providing some dependency injection to replace the used ODM. 然后,您可以使用提供一些依赖注入的模块来替换使用的ODM。

As it is a really long task, I cannot give you some code. 由于这是一项非常漫长的任务,我无法给你一些代码。 Moreover, it may be a pain to implement that kind of thing in javascript because it does not provide strong OOP natively. 而且,在javascript中实现那种东西可能会很痛苦,因为它本身并不提供强大的OOP。 However, I can suggest you to take a look at some frameworks which can help you to do that like Danf for instance which provides a strong OOP paradigm with interfaces, classes, inheritance and a powerful dependency injection. 但是,我建议你看看一些可以帮助你做这些的框架 ,例如Danf ,它提供了一个强大的OOP范例,包括接口,类,继承和强大的依赖注入。

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

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