BearCat 特性
提供 java 的基本编程思路: 基于配置的编程.
这篇文字对官文进行的部分翻译, 还不错:
http://www.infoq.com/cn/articles/ioc-meet-nodejs/
Ioc(Invertion Of Control):
AOP(Aspect-Oriented Programming):
类似于 Sails 的 policies 和 response, 但这个基于类实现, 根据不同类指定不同 aspect.
Model
包含魔法方法的类, 把 JS 对象操作变成和 java 一样的 set/get->pack 了, 支持强类型判断.
Filter:
tModel.$after(‘filter_name’).$set(‘num’, 3);
tModel.$before(‘filter_name’).$set(‘num’, 4);定义合法性检测方法(类型检测,数字范围…)
配置文件分文件夹存放
值得借鉴的是关于配置文件的加载部分, 很不错…
服务器端的加载依赖 context.json
- name : {String} 工程名
- beans : {Array} 类依赖
- scan : {String|Array} 自加载目录
- imports : {Array} 需要加载子文件的路径
- dependencies : specify the beans from the dependency module to be managed in the container
- 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
- before filter method has key, value as arguments, after filter does not have these arguments
- 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
- ref: set the attribute of type refered by ModelId
- type: type of the attribute, can be Array, Number, String, Object, Function, Boolean
- prefix: set the attribute with id = prefix + attribute name, to fill up with resultSet data
- default: set the default value of the attribute
- primary: mark this attribute as a primary key in O/R mapping to database table
- 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
- id : the unique bean name in the current container, for container to lookup
- mid : the unique model name in the current container, using bearcat.getModel(‘mid’) to get this model
- cid : the unique constraint name in the current container, set this id in model attribute value to enable this constraint
- func : the constructor function for the bean
- order : the order of instantiation when it is a singleton bean
- init : init method which will be invoked after constructor function, init can be async
- destroy : destroy method which will be invoked when the container gracefully shutdown, beans need to be destroyed
- factoryBean : the name of the factory bean for the bean’s instantiation
- factoryMethod : the factory method of the factory bean
- scope : scope can be singleton or prototype, by default it is singleton
- async : specify whether the init method is async or not, by default it is false
- abstract : specify a bean to be abstract, do not need to be instantiated, by default it is false
- 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
- 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
- args : the arguments dependency injection, it is an array, all of its elements will be wrapped into a BeanWrapper, it has the following attributes
- name : the name of the dependency injection element
- type : when type is specified, it is a var dependency injection, you can pass argument into the getBean function
- value : the value to be injected
- ref : the name of the bean to be injected in the current container
- props : the properties dependency injection, it is the same as args
- factoryArgs : the factory arguments dependency injection, it is the same as args
- 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.
- aop : to specify the bean is an aspect that defines pointcut and advice, it is an array.
- pointcut : defines the pointcut expression
- advice : defines the advice method matches the pointcut
- runtime : set to true to specify that target method arguments will be passed to this advice
- table : the object relation mapping to database table name
- prefix : set the attribute with id = prefix + attribute name, to fill up with resultSet data
- 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