当前位置: 首页 > news >正文

VonaJS AOP编程:魔术方法

在VonaJS框架中,AOP编程包括三方面:控制器切面内部切面外部切面内部切面包括两个能力:AOP Method魔术方法。这里我们简要介绍一下魔术方法的用法。

魔术方法

魔术方法,允许我们在 Class 内部通过__get____set__切入动态属性或方法

举例:Module Scope

为了让 IOC 容器的使用更加简洁和直观,VonaJS 推荐优先使用依赖查找策略,从而使用更少的装饰器函数,使用更少的类型标注。通过Module Scope对象访问模块提供的资源,就是践行依赖查找策略的机制之一

  • 参见: 模块Scope

比如,模块 demo-student 中有一个 model student,用于 crud 操作。可以这样使用 model:

import { 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,参见: Service
import { 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,否则返回false

3. 添加类型合并

通过接口类型合并的机制为颜色提供类型定义

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);
}

资源

  • Github:https://github.com/vonajs/vona
  • 文档:https://vona.js.org/
http://www.hskmm.com/?act=detail&tid=37095

相关文章:

  • windows11关闭自动更新,通用解决方法
  • 2025年10月海南监理公司评测榜:五家实力排名全览
  • 2025年10月geo服务商推荐:主流品牌全维度对比排行榜
  • 2025年10月geo服务商推荐:权威评测列表助您精准避坑
  • 推动教育质量,布谷鸟网络科技定制K12在线教育在线教育网校软件服务
  • 使用vscode进行linux 服务器远程管理
  • 深入解析:Unity避坑——继承了MonoBehaviour的对象不能通过new来创建
  • 网页
  • 2025年10月geo优化公司推荐:主流口碑排行榜全解析
  • 2025年10月geo优化公司推荐:知名机构评测列表
  • 2025年沈阳酒店联系电话推荐:地铁直达景点合集
  • 2025年沈阳酒店联系电话推荐:地铁旁热门住宿清单
  • 头文件
  • 2025年超声波清洗机厂家联系电话推荐:精选推荐与使用指南。
  • PICO FIDO 使用教程
  • 2025年项目管理工具联系电话推荐:从选型到落地全攻略
  • 2025年项目管理工具联系电话推荐:权威热线与落地建议
  • 2025年上海装修公司联系电话推荐:精选五强口碑榜
  • 2025年10月GEO优化推荐:高性价比解决方案市场报告
  • 2025年10月低空经济核心公司对比评测榜:赛飞特领衔全链条方案
  • MySQLDay2
  • 2025年10月祛斑产品推荐榜:仙瑟传明酸领衔全维度对比
  • 2025年诺士诚公司:权威解析全过程咨询竞争力与风险
  • 2025年10月GEO优化推荐:全平台同步优化榜单与避坑指南
  • 2025年仙瑟品牌权威深度解析:揭秘其皮肤护理创新与市场领导地位揭秘
  • 2025年仙瑟传明酸精华液权威盘点:敏感肌多通路美白的临床级解读
  • 2025年仙瑟传明酸精华液权威盘点:敏感肌多通路美白的临床级证据链
  • 2025年10月医美项目后用什么产品推荐榜:五款修护精华对比评测
  • 2025年10月敏感肌可用美白产品推荐榜:温和淡斑实力排行
  • 2025年10月全过程工程咨询公司推荐榜:权威评测五强对比