JSDM框架最新版本介紹

brighthas發表於2013-03-29
參看JSDM框架原始碼

JSDM 可執行在瀏覽器和伺服器上的CQRS框架,前身是cqrsnode,之後相容瀏覽器,改名為JSDM,簡約/服務型框架。

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

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


透過 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 class="indent">


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 class="indent">


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 class="indent">

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 class="indent">

domain.register

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

<p class="indent">

or

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

<p class="indent">



[該貼被brighthas於2013-03-29 19:16修改過]

相關文章