当前位置: 首页 > 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);

http://www.cnnetsun.cn/news/174788.html

相关文章:

  • 9 个降AI率工具,MBA 必备避坑指南
  • Windows系统文件inetmib1.dll丢失损坏 下载修复方法
  • Boost电路的右半平面零点
  • 【全球AI伦理治理】
  • 毕业季必看!7款免费AI写论文神器实测,一站式搞定选题、大纲到降重
  • LLMs之Survey之Agent:《Measuring Agents in Production》翻译与解读
  • 零代码上手Google Gemini 3:5种实用方法大揭秘
  • “你用的那个AI,到底把你坑了还是救了?”——解锁宏智树论文的协作新范式
  • 好写作AI:别等学校采购了!你的论文“救命神器”自己就能用上
  • Windows系统文件GdiPlus.dll丢失或损坏 下载修复方法
  • 研究生必备8款AI写论文神器:5分钟生成25000字问卷类论文,自动生成高信度数据
  • 【BuildFlow 筑流】unitrix_macros库 Cargo.toml 配置详解及依赖库用法
  • 《开发者出海必看:如何优雅地搞定海外服务支付?(保姆级干货)》
  • Thinkphp和Laravel企业防爆安全设备信息系统
  • Thinkphp和Laravel全家桶鲜花售卖商城系统vue
  • 记录我适配iOS26遇到的一些问题
  • 通过命令模拟pod创建
  • 同步机无感 STM32 低成本 MD500E 永磁同步控制方案大揭秘
  • 小宝玩具 【通达信、源码 、主图、附图】
  • 使用 Github Pages 和 Hexo
  • 审稿 一区期刊注意事项: journal offers the option to connec;please note, reviewers are not expected 是什么意思
  • 线性代数:多维世界的变形工具箱
  • 力扣题目142. 环形链表 II​的解法分享,附图解
  • MATLAB电力系统继电保护之自动重合闸
  • 10 个AI写作工具,助你轻松搞定继续教育论文!
  • 【开题答辩全过程】以 基于Vue的茶道知识科普网站的设计与实现为例,包含答辩的问题和答案
  • 主动配电网两阶段鲁棒恢复:Matlab 代码探索之旅
  • ICG-20660L加速度+陀螺仪六轴IMU传感器原理图设计,已量产(加速度传感器)
  • 百度AI架构师亲授:Agentic智能体在医疗领域的落地(附诊断案例)
  • 软件工程期末高频易错点深度剖析:避开这些坑,你就赢了!