BearcatJS 写出Java一样的JS

BearCat 特性

提供 java 的基本编程思路: 基于配置的编程.

这篇文字对官文进行的部分翻译, 还不错:

http://www.infoq.com/cn/articles/ioc-meet-nodejs/

Ioc(Invertion Of Control):

AOP(Aspect-Oriented Programming):

类似于 Sails 的 policies 和 response, 但这个基于类实现, 根据不同类指定不同 aspect.

Model

  1. 包含魔法方法的类, 把 JS 对象操作变成和 java 一样的 set/get->pack 了, 支持强类型判断.

  2. Filter:

    tModel.$after(‘filter_name’).$set(‘num’, 3);
    tModel.$before(‘filter_name’).$set(‘num’, 4);

  3. 定义合法性检测方法(类型检测,数字范围…)

配置文件分文件夹存放

值得借鉴的是关于配置文件的加载部分, 很不错…

服务器端的加载依赖 context.json

  1. name : {String} 工程名
  2. beans : {Array} 类依赖
  3. scan : {String|Array} 自加载目录
  4. imports : {Array} 需要加载子文件的路径
  5. dependencies : specify the beans from the dependency module to be managed in the container
  6. namespace : 当前 beans 的命名空间, 在 getBeans 时需要加此前缀

浏览器资源整合

使用 bearcat generate 生成 bearcat-generate.js 用于浏览器端, 加载负载的代码(当代码结构变更时需要重新生成)

关键文档:

Installation

npm install bearcat
var bearcat = require('bearcat');

Start

var bearcat = require('bearcat');
var contextPath = require.resolve('./context.json');
global.bearcat = bearcat; // make bearcat global, for `bearcat.module()`
bearcat.createApp([contextPath]);
bearcat.start(function() {
  var car = bearcat.getBean('car'); // get car
  car.run(); // call the method
});

DI (IOC)

AOP

Model & Constrain:

Define
var SimpleModel = function() {
    this.$mid = "simpleModel";
    this.num1 = 0;
    this.num2 = "$type:Number";
    this.num3 = "$type:Number;default:20";
}
SimpleModel.prototype.beforeFunc = function(key, value) {
if (typeof value !== 'number') {
    return new Error('num must be number');
}
if (value > 10) {
    return new Error('num must be small than 10');
}
}
FilterModel.prototype.afterFunc = function() {
this.num1 = 10;
}
module.exports = SimpleModel;
Usage
var simpleModel = bearcat.getModel('simpleModel');
simpleModel.$set('num1', 10);
simpleModel.$pack({'num1': 5});

var r = simpleModel.$set('num2', 'aaa');
if (r) {console.log(r.stack);}

var num3 = simpleModel.$get('num3');

simpleModel.$before('beforeFunc').$set('num', 'aaa');
simpleModel.$after('afterFunc').$set('num', 3);
Note
  1. before filter method has key, value as arguments, after filter does not have these arguments
  2. filter return can be Error object, or not return, not return means pass, Error object means not pass with the error object
model magic attribute value
  1. ref: set the attribute of type refered by ModelId
  2. type: type of the attribute, can be Array, Number, String, Object, Function, Boolean
  3. prefix: set the attribute with id = prefix + attribute name, to fill up with resultSet data
  4. default: set the default value of the attribute
  5. primary: mark this attribute as a primary key in O/R mapping to database table
  6. balance: mark this attribute as a balance key in distributed sharding databases
Model constraint -> 类型检测,约束

https://github.com/bearcatjs/bearcat/tree/master/examples/model

Context.JSON

config style
var Car = function() {
    this.$id = "car"; // id car
}
Car.prototype.run = function() {
    console.log('run car...');
    return 'car';
}
module.exports = Car;

OR

var Car = function() {}
Car.prototype.run = function() {
    console.log('run car...');
    return 'car';
}
// func is the constructor function
module.exports = {
    id: "car",
    func: Car
};
Bean attribute
  1. id : the unique bean name in the current container, for container to lookup
  2. mid : the unique model name in the current container, using bearcat.getModel(‘mid’) to get this model
  3. cid : the unique constraint name in the current container, set this id in model attribute value to enable this constraint
  4. func : the constructor function for the bean
  5. order : the order of instantiation when it is a singleton bean
  6. init : init method which will be invoked after constructor function, init can be async
  7. destroy : destroy method which will be invoked when the container gracefully shutdown, beans need to be destroyed
  8. factoryBean : the name of the factory bean for the bean’s instantiation
  9. factoryMethod : the factory method of the factory bean
  10. scope : scope can be singleton or prototype, by default it is singleton
  11. async : specify whether the init method is async or not, by default it is false
  12. abstract : specify a bean to be abstract, do not need to be instantiated, by default it is false
  13. parent : specify the inheritance relationship between beans, the child bean will inherit the method in parent bean’s prototype that child bean does not have, the value is the parent bean id
  14. lazy : specify whether current bean do not need to be preInstantiated, it will be instantiated when requested by the container, by default it is false
  15. args : the arguments dependency injection, it is an array, all of its elements will be wrapped into a BeanWrapper, it has the following attributes
  16. name : the name of the dependency injection element
  17. type : when type is specified, it is a var dependency injection, you can pass argument into the getBean function
  18. value : the value to be injected
  19. ref : the name of the bean to be injected in the current container
  20. props : the properties dependency injection, it is the same as args
  21. factoryArgs : the factory arguments dependency injection, it is the same as args
  22. proxy : specify whether the bean need to be proxied or not, by default it is true. Proxy is needed when the bean can be intercepted by an AOP advice, however when the bean is infrastructural, it is unnecessary to be proxied.
  23. aop : to specify the bean is an aspect that defines pointcut and advice, it is an array.
  24. pointcut : defines the pointcut expression
  25. advice : defines the advice method matches the pointcut
  26. runtime : set to true to specify that target method arguments will be passed to this advice
  27. table : the object relation mapping to database table name
  28. prefix : set the attribute with id = prefix + attribute name, to fill up with resultSet data
  29. constraint : set the constraint with primitive constraints, like this.$constraint = “$myNotNull”;
Special attribute

$V : $V prefixed attribute will be replaced by the specific environment configuration, like this.$Vnum = “${car.vnum}”;

however, $V is not required when using consistent configuration in bearcat, you can do the same thing like

this.xnum = “${car.xnum}”;

$N : $N prefixed attribute specifies the namespace, like this.$Ncar = “app:car”;

app is the namespace, car is the bean id

Donate - Support to make this site better.
捐助 - 支持我让我做得更好.