2026/5/19 1:23:41
网站建设
项目流程
网站建站侵权怎么办,wordpress 主题 带筛选,谷歌seo营销,网站什么时候备案在VonaJS框架中#xff0c;AOP编程包括三方面#xff1a;控制器切面、内部切面和外部切面。内部切面包括两个能力#xff1a;AOP Method和魔术方法。这里我们简要介绍一下魔术方法的用法。魔术方法魔术方法#xff0c;允许我们在 Class 内部通过__get__和__set__切入动态属…在VonaJS框架中AOP编程包括三方面控制器切面、内部切面和外部切面。内部切面包括两个能力AOP Method和魔术方法。这里我们简要介绍一下魔术方法的用法。魔术方法魔术方法允许我们在 Class 内部通过__get__和__set__切入动态属性或方法举例Module Scope为了让 IOC 容器的使用更加简洁和直观VonaJS 推荐优先使用依赖查找策略从而使用更少的装饰器函数使用更少的类型标注。通过Module Scope对象访问模块提供的资源就是践行依赖查找策略的机制之一参见: 模块Scope比如模块 demo-student 中有一个 model student用于 crud 操作。可以这样使用 modelimport { ModelStudent } from ../model/student.ts;async findMany(params) {const model this.bean._getBean(ModelStudent);return await model.selectAndCount(params);}使用魔术方法async findMany(params) {return await this.scope.model.student.selectAndCount(params);}this.scope.model.xxx: 通过魔术方法动态获取当前模块中的 model 实例举例CRUD(魔术方法)Vona ORM 采用魔术方法的机制进一步简化操作数据的代码参见: CRUD(魔术方法)比如通过字段id查询学生信息代码如下async findOne(id) {return await this.scope.model.student.get({ id });}使用魔术方法async findOne(id) {return await this.scope.model.student.getById(id);}系统自动从 method name getById中解析出参数id然后调用实际的 CRUD 方法这里就是: get({ id })创建Class可以在任何 Class 中实现魔术方法。下面以 Service 为例在模块 demo-student 中创建一个 Service color代码如下如何创建 Service参见: Serviceimport { BeanBase } from vona;import { Service } from vona-module-a-bean;Service()export class ServiceColor extends BeanBase {}__get__然后通过__get__实现颜色值的获取1. 添加代码骨架在 VSCode 编辑器中输入代码片段aopmagicget自动生成代码骨架:Service()export class ServiceColor extends BeanBase { protected __get__(prop: string) {}}2. 实现自定义逻辑Service()export class ServiceColor extends BeanBase { private _colors { red: #FF0000, green: #00FF00, blue: #0000FF, };protected __get__(prop: string) { return this._colors[prop];}}3. 添加类型合并通过接口类型合并的机制为颜色提供类型定义export interface ServiceColor {red: string;green: string;blue: string;}4. 使用魔术方法async test() {console.log(this.scope.service.color.red);console.log(this.scope.service.color.green);console.log(this.scope.service.color.blue);}__set__然后通过__set__实现颜色值的设置1. 添加代码骨架在 VSCode 编辑器中输入代码片段aopmagicset自动生成代码骨架:Service()export class ServiceColor extends BeanBase { protected __set__(prop: string, value: any): boolean { return false; }}2. 实现自定义逻辑Service()export class ServiceColor extends BeanBase {private _colors {red: #FF0000,green: #00FF00,blue: #0000FF, black: ,};protected __set__(prop: string, value: any): boolean { if (this._colors[prop] undefined) return false; this._colors[prop] value; return true;}}如果为prop设置了值返回true否则返回false3. 添加类型合并通过接口类型合并的机制为颜色提供类型定义export interface ServiceColor {red: string;green: string;blue: string; black: string;}4. 使用魔术方法async test() {this.scope.service.color.black #000000;console.log(this.scope.service.color.black);