news 2026/6/9 23:26:23

VonaJS AOP编程:魔术方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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);

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/7 9:01:29

达梦数据库安装

好的,这是一篇关于达梦数据库(DM Database)安装的详细指南,包含目录、文字说明和图片位置示意,内容丰富,力求达到3000字的要求。达梦数据库安装与配置详细指南目录引言1.1 达梦数据库简介1.2 安装前准备的重…

作者头像 李华
网站建设 2026/6/9 12:19:00

非线性最优化问题求解器Ipopt介绍

文章目录一、关键输入信息1、优化问题的维度2、优化变量的边界3、优化问题的初始迭代点:4、优化问题的数据结构(Structure):5、优化问题函数的值:二、C Interface1、Ipopt::TNLP::get_nlp_info2、Ipopt::TNLP::get_bounds_info3、Ipopt::TNLP…

作者头像 李华
网站建设 2026/5/18 21:56:44

springboot人事系统(11545)

有需要的同学,源代码和配套文档领取,加文章最下方的名片哦 一、项目演示 项目演示视频 二、资料介绍 完整源代码(前后端源代码SQL脚本)配套文档(LWPPT开题报告)远程调试控屏包运行 三、技术介绍 Java…

作者头像 李华
网站建设 2026/6/1 10:30:57

智慧城市与智慧校园之安防暴力检测 校园打架斗殴检测 街边暴力躁动识别 危险物品识别 智能安防 安防领域智能化 数据集第10319期 (1)

暴力检测数据集 本文档为深度学习相关研究与应用开发,提供暴力检测数据集的核心信息说明。数据集核心信息表项目详情类别共 4 类,分别为非暴力(NonViolence)、暴力(Violence)、枪支(guns&#x…

作者头像 李华
网站建设 2026/6/5 14:11:21

别慌!AI淘汰的不是人,是不会用AI的人

别慌!AI淘汰的不是人,是不会用AI的人一、先说说扎心现实:AI 已经在抢工作了,但比你想的更 “挑活”前几天跟做文案的朋友吃饭,他吐槽说现在写活动方案都得跟 AI 抢饭吃 —— 领导让写 3 个电商促销文案,他熬…

作者头像 李华