程序员 2014-10-17 一天的奇妙之旅
发布于 9 年前 作者 leogiese 3264 次浏览 最后一次编辑是 8 年前 来自 分享

原文 http://forjs.org/book/xyfULqrrXg/section/xJriPWHEUe

这里还是推荐一下我的书:《精通Node.js开发》 《Angular.js视频详解》

早晨起来,我思考了一些重要问题。

关于domain框架,我觉得应该有saga概念,但没必要真的做这么Saga类。

再者,根对象可以监听领域事件。把AggregateRoot改名为Actor,这样更形象。写法我想采用es6的语法开发。

关于jshow项目的开发,我决定采用polymer开发前端ui,并且把polymer应用到forjs.org,在下一版本。

一杯香咖啡!开始计划一天的工作。

  • domain框架大改动
  • jshow polymer 布局
  • 继续blog课程录制,angular也需要录制一下。

domain framework 大动工

首先把 AggregatorRoot 转换成 es6 的 Actor


var Event = require("./Event"),
    uid = require("shortid");

class Actor {

    constructor() {
        this.__data = {};
        this.set("id", uid());
        this.set("alive", true);
        this.uncommittedEvents = [];
    }

    set(k, v) {
        if (arguments.length === 1) {
            for (var n in k) {
                this.set(n, k[n]);
            }
        } else {
            this.__data[k] = v;
        }

    }

    get(k) {
        return this.__data[k];
    }

    get json() {
        return JSON.parse(JSON.stringify(this.__data));
    }

    loadEvents(events) {
        if (this.__isLoadEvents) return;
        events.forEach(function (event) {
            this.when(event);
        }.bind(this))
        this.__isLoadEvents = true;
    }

    loadSnap(snap) {
        if (this.__isLoadSnap) return;
        this.__data = snap;
        this.__isLoadSnap = true;

    }

    when(event) {
        if (event.name === "remove") {
            this.set("alive", false);
        }
        if (event.name === "completed") {
            this.set("completed", true);
        } else {
            this._when(event);
        }

    }

    _apply(name, data, sagaId, sagaType) {
        if (this.get("alive")) {

            var event = new Event({
                sagaId: sagaId || null,
                sagaType: sagaType || null,
                aggregateId: this.get("id"),
                name: name,
                aggregateType: this.constructor.className,
                type: "aggregate",
                data: data
            });
            this.when(event);
            this.uncommittedEvents.push(event);
            this._publish();

        }
    }

    remove() {
        this.apply("remove");

    }

    _publish() {
        console.log("please implement _publish method.");
    }
}

module.exports = Actor;

下一步看看有何不妥的地方,继承方式抛弃静态方法 extend,因为太不优雅了。

接着重写Event.js

var uid = require("shortid");

class Event{

    /**
     * @param data {name,targetId,targetType,sourceId,sourceType}
     */
    constructor(data){
        this._id = uid();
        this._time = Date.now();
        this._data = data;
    }

    get data(){
        return JSON.parse(JSON.stringify(this._data));
    }

}

上午就工作到这里,下午继续 … …

2 回复

已更新

回到顶部