这里采用node.js的环境编程,采用CQRS方式编程。
首先建立“刷新”Command
function ProductRefreshCommand(productId){ this.productId = productId; }
|
然后建立 Command handle. 是和Command一一对应的。
function ProductRefreshCommandHandle(command){ this.command = command; }
var handle = ProductRefreshCommandHandle.prototype;
handle.handle = function(){ global.repos.productRepo.findById(this.command.productId,function(product){ product.refresh(); }); }
|
之后,我们建立Product.
function Product(id){ this._num = 0; this._id = id; }
var product = Product.prototype;
product.__defineGetter__('num',function(){ return this._num; }); product.__defineGetter__('id',function(){ return this._id; });
product.refresh = function(){ ++this._num; global.eventbus.publish(new global.events.ProductRefreshEvent(this)) }
|
然后,我们建立刷新事件
function ProductRefreshEvent(product){ this.product = product; }
|
最后,我们建立事件处理器
function ProductRefreshEventHandle(event){ event.product.save(); }
|
先写这些,有些没有主题,希望各位一起探讨。