找回密码
 立即注册
首页 业界区 业界 VonaJS AOP编程:魔术方法

VonaJS AOP编程:魔术方法

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

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

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

  • 参见: 模块Scope
比如,模块 demo-student 中有一个 model student,用于 crud 操作。可以这样使用 model:
  1. import { ModelStudent } from '../model/student.ts';
  2. async findMany(params) {
  3.   const model = this.bean._getBean(ModelStudent);
  4.   return await model.selectAndCount(params);
  5. }
复制代码
使用魔术方法:
  1. async findMany(params) {
  2.   return await this.scope.model.student.selectAndCount(params);
  3. }
复制代码

  • this.scope.model.xxx: 通过魔术方法动态获取当前模块中的 model 实例
举例:CRUD(魔术方法)

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

  • 参见: CRUD(魔术方法)
比如,通过字段id查询学生信息,代码如下:
  1. async findOne(id) {
  2.   return await this.scope.model.student.get({ id });
  3. }
复制代码
使用魔术方法:
  1. async findOne(id) {
  2.   return await this.scope.model.student.getById(id);
  3. }
复制代码

  • 系统自动从 method name getById中解析出参数id,然后调用实际的 CRUD 方法,这里就是: get({ id })
创建Class

可以在任何 Class 中实现魔术方法。下面,以 Service 为例,在模块 demo-student 中创建一个 Service color,代码如下:

  • 如何创建 Service,参见: Service
  1. import { BeanBase } from 'vona';
  2. import { Service } from 'vona-module-a-bean';
  3. @Service()
  4. export class ServiceColor extends BeanBase {}
复制代码
__get__

然后,通过__get__实现颜色值的获取
1. 添加代码骨架

在 VSCode 编辑器中,输入代码片段aopmagicget,自动生成代码骨架:
  1. @Service()
  2. export class ServiceColor extends BeanBase {
  3. + protected __get__(prop: string) {}
  4. }
复制代码
2. 实现自定义逻辑
  1. @Service()
  2. export class ServiceColor extends BeanBase {
  3. + private _colors = {
  4. +   red: '#FF0000',
  5. +   green: '#00FF00',
  6. +   blue: '#0000FF',
  7. + };
  8.   protected __get__(prop: string) {
  9. +   return this._colors[prop];
  10.   }
  11. }
复制代码
3. 添加类型合并

通过接口类型合并的机制为颜色提供类型定义
  1. export interface ServiceColor {
  2.   red: string;
  3.   green: string;
  4.   blue: string;
  5. }
复制代码
4. 使用魔术方法
  1. async test() {
  2.   console.log(this.scope.service.color.red);
  3.   console.log(this.scope.service.color.green);
  4.   console.log(this.scope.service.color.blue);
  5. }
复制代码
__set__

然后,通过__set__实现颜色值的设置
1. 添加代码骨架

在 VSCode 编辑器中,输入代码片段aopmagicset,自动生成代码骨架:
  1. @Service()
  2. export class ServiceColor extends BeanBase {
  3. + protected __set__(prop: string, value: any): boolean {
  4. +   return false;
  5. + }
  6. }
复制代码
2. 实现自定义逻辑
  1. @Service()
  2. export class ServiceColor extends BeanBase {
  3.   private _colors = {
  4.     red: '#FF0000',
  5.     green: '#00FF00',
  6.     blue: '#0000FF',
  7. +   black: '',
  8.   };
  9.   protected __set__(prop: string, value: any): boolean {
  10. +   if (this._colors[prop] === undefined) return false;
  11. +   this._colors[prop] = value;
  12. +   return true;
  13.   }
  14. }
复制代码

  • 如果为prop设置了值,返回true,否则返回false
3. 添加类型合并

通过接口类型合并的机制为颜色提供类型定义
  1. export interface ServiceColor {
  2.   red: string;
  3.   green: string;
  4.   blue: string;
  5. + black: string;
  6. }
复制代码
4. 使用魔术方法
  1. async test() {
  2.   this.scope.service.color.black = '#000000';
  3.   console.log(this.scope.service.color.black);
  4. }
复制代码
资源


  • Github:https://github.com/vonajs/vona
  • 文档:https://vona.js.org/

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册