news 2026/3/8 15:35:42

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/3/6 16:59:44

【01-02】

文章目录题目要求项目结构1.Action2.ColorableStep1:写接口和父类Step2:写实现类Step3:写测试类题目要求 项目结构 1.Action 2.Colorable Step1:写接口和父类 package Colorable; /*** 定义一个接口Colorable,包含一个方法void setColor(String aolor)*/ public …

作者头像 李华
网站建设 2026/3/6 16:59:43

初学者如何通过工作负载分析掌握项目进度与资源分配

你是否也经历过这样的项目困境:团队忙得焦头烂额,却总有人无事可做;任务堆积如山,却说不清到底卡在了哪里?明明每个人都看似在工作,项目进度却一再拖延——这背后,很可能不是努力不够&#xff0…

作者头像 李华
网站建设 2026/2/28 13:03:35

CSS2 表格布局入门:前端新人也能轻松驾驭的排版利器

CSS2 表格布局入门:前端新人也能轻松驾驭的排版利器CSS2 表格布局入门:前端新人也能轻松驾驭的排版利器引言:给老伙计一个重新自我介绍的机会table-layout 到底是个啥?先给 W3C 规范配个人话翻译机auto 与 fixed:一场龟…

作者头像 李华
网站建设 2026/3/3 14:37:26

(100分)- 部门人力分配(Java JS Python C)

(100分)- 部门人力分配(Java & JS & Python & C)题目描述部门在进行需求开发时需要进行人力安排。当前部门需要完成 N 个需求,需求用 requirements 表述,requirements[i] 表示第 i 个需求的工作量大小,单位…

作者头像 李华
网站建设 2026/2/28 10:46:24

(100分)- 测试用例执行计划(Java JS Python C)

(100分)- 测试用例执行计划(Java & JS & Python & C) 题目描述 某个产品当前迭代周期内有 N 个特性(F1,F2,......FN)需要进行覆盖测试,每个特性都被评估了对应的优先级,特性使用其 ID 作为下…

作者头像 李华