VonaJS AOP编程:魔术方法

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

魔术方法

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

举例:Module Scope

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

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

typescript 复制代码
import { ModelStudent } from '../model/student.ts';

async findMany(params) {
  const model = this.bean._getBean(ModelStudent);
  return await model.selectAndCount(params);
}

使用魔术方法:

typescript 复制代码
async findMany(params) {
  return await this.scope.model.student.selectAndCount(params);
}
  • this.scope.model.xxx: 通过魔术方法动态获取当前模块中的 model 实例

举例:CRUD(魔术方法)

Vona ORM 采用魔术方法的机制进一步简化操作数据的代码

比如,通过字段id查询学生信息,代码如下:

typescript 复制代码
async findOne(id) {
  return await this.scope.model.student.get({ id });
}

使用魔术方法:

typescript 复制代码
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
typescript 复制代码
import { BeanBase } from 'vona';
import { Service } from 'vona-module-a-bean';

@Service()
export class ServiceColor extends BeanBase {}

__get__

然后,通过__get__实现颜色值的获取

1. 添加代码骨架

在 VSCode 编辑器中,输入代码片段aopmagicget,自动生成代码骨架:

diff 复制代码
@Service()
export class ServiceColor extends BeanBase {
+ protected __get__(prop: string) {}
}

2. 实现自定义逻辑

diff 复制代码
@Service()
export class ServiceColor extends BeanBase {
+ private _colors = {
+   red: '#FF0000',
+   green: '#00FF00',
+   blue: '#0000FF',
+ };

  protected __get__(prop: string) {
+   return this._colors[prop];
  }
}

3. 添加类型合并

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

typescript 复制代码
export interface ServiceColor {
  red: string;
  green: string;
  blue: string;
}

4. 使用魔术方法

typescript 复制代码
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,自动生成代码骨架:

diff 复制代码
@Service()
export class ServiceColor extends BeanBase {
+ protected __set__(prop: string, value: any): boolean {
+   return false;
+ }
}

2. 实现自定义逻辑

diff 复制代码
@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. 添加类型合并

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

diff 复制代码
export interface ServiceColor {
  red: string;
  green: string;
  blue: string;
+ black: string;
}

4. 使用魔术方法

typescript 复制代码
async test() {
  this.scope.service.color.black = '#000000';
  console.log(this.scope.service.color.black);
}

资源

相关推荐
damoluomu18 分钟前
静态站点生成器怎么选:Node.js 生态里常用的五个(番外一)
javascript·css·typescript·node.js·html
ZoeLandia1 小时前
TypeScript 04:类型断言与类型守卫
开发语言·前端·typescript
濮水大叔1 小时前
Cabloy/Vona 如何避开 NestJS 的循环依赖问题:懒属性注入机制源码解析
typescript·node.js·nestjs
雨雨雨雨雨别下啦1 天前
【OpenHarmony北向开发】ArkTS基础教学
typescript·arkts·openharmonys
ZoeLandia1 天前
TypeScript 01:类型注解与类型推断
前端·javascript·typescript
ZoeLandia1 天前
TypeScript 02:interface、type 与类型收窄
前端·typescript
行者-全栈开发1 天前
华为云码道 CodeArts 实测:让 AI 用纯 TypeScript 写一个确定性回放的塔防小游戏「熵减特勤局」
typescript·小游戏·canvas·ecs·ai 编程·华为云码道·codearts 代码智能体
mbshqqb2 天前
TypeScript基础教程之同步生成器
前端·javascript·typescript
濮水大叔3 天前
Cabloy 双引擎架构详解:Vona 与 Zova 是如何协作的
typescript·node.js·全栈
前端大斗师3 天前
「大于 1000」把 1000 元那单也算进去了:我在 Vue3 订单页对了 10 句话
前端·人工智能·typescript·大模型·原力计划