JSDM框架最新版本介绍

13-03-29 brighthas
参看JSDM框架源代码

JSDM 可运行在浏览器和服务器上的CQRS框架,前身是cqrsnode,之后兼容浏览器,改名为JSDM,简约/服务型框架。

一個Domain是個黑盒模塊,外界通過domain.exec操作內部。 (DDD CQRS的框架模式)

domain.register(          // register註冊domain的部件
   "AggreClass",UserClass,
   "repository",user_repo,
   "commandHandle",handles)
   .seal();                      // seal封印成黑盒。
<p>

通過 domain.exec(command) 調用。

下面是英文的使用方法:

step 1. define Aggre

    // User.js

    module.exports = wrap;

    function wrap(repos,services,publish){

            function User(name){
                this._name = name;
            }

            User.prototype = {
                getName:function(){
                   return this._name;
                },
                changeName:function(name){
                    this._name = name;
                    publish("user."+this.id+".changeName",name);
                    publish("user.*.changeName",this.id,name);
                }
            }

            return User;
        }

        wrap.NAME = "user";
<p>

step 2. define user repository

// user_repo.js
    moduel.exports = wrap

        // define aggre repository
        function wrap(repository,User){

            // repository is Repository instance , must implement _create/_data2aggre/_aggre2data
            repository._create = function(data,callback){
                var user = new User(data.name);
                callback(undefined,user);
            }

            repository._data2aggre = function(data){
                var user = new User(data.name);
                user.id = data.id;
                return user;
            }

            repository._aggre2data = function(aggre){
                var data = {
                    name:aggre.getName(),
                    id:aggre.id
                }
                return data;
            }

        }
        wrap.NAME = "user";  // and user aggre wrap same.

<p>

step 3. define command handle

    // handle.js

        // define command handle 1
        function wrap1(repos,services){
            function handle(args,callback){
                var repo = repos.user;
                repo.get(args.id,function(err,user){
                    user.changeName(args.name);
                    callback();
                })
            }
            return handle;
        }
        wrap1.NAME = "change user name";

        // define command handle 2
        function wrap2(repos,services){
            function handle(args,callback){
                var repo = repos.user;
                repo.create({name:args.name},callback)
            }
            return handle;
        }
        wrap2.NAME = "create a user";        
        module.exports = [wrap1,wrap2];

<p>

step 4. register and run

   var domain = require("jsdm")();
   var UserClass = require("./User"),
       user_repo = require("./user_repo"),
       handles = require("./handle");

   domain.register("AggreClass",UserClass)
         .register("repository",user_repo)
         .register("commandHandle",handles[0],handles[1])
         .seal();

   domain.exec("create a user",{name:"bright.has"},function(){
    console.log("lol")
   })

<p>

domain.register

   domain.register("AggreClass",UserClass)
         .register("repository",user_repo)
         .register("commandHandle",handles[0],handles[1])
         .seal();

<p>

or

   domain.register("AggreClass",UserClass,"repository",user_repo,"commandHandle",handles)
         .seal();

<p>

[该贴被brighthas于2013-03-29 19:16修改过]

              

1
猜你喜欢