gdmb
v1.3.5
Published
通用领域模型基座,提供领域模型设计开发通用基座能力。依赖 `tsyringe` 提供依赖注入能力。
Readme
项目介绍
通用领域模型基座,提供领域模型设计开发通用基座能力。依赖 tsyringe 提供依赖注入能力。
安装
npm i gdmb --save
依赖
以下依赖需要在项目中引入
- tsyringe:轻量级依赖注入容器
- reflect-metadata: 反射元数据
修改 tsconfig.json 配置,开放元数据与反射支持
"emitDecoratorMetadata": true,
"experimentalDecorators": true用例
注册领域、领域服务、领域实体
/** DomainOne.ts */
import { GDMB, Domain, DomainService, DomainEntity } from 'gdmb';
/** 值对象接口 */
export interface IVOOne {
text: string
}
/** 实例化gdmb实例对象,全局单例 */
const gdmb = new GDMB();
/** 领域一 */
class DomainOne extends Domain {
constructor() {
super();
// 注册服务域
this.registerService(ServiceOne);
// 注册实体域
this.registerEntity(EntityOne);
// 注册值对象
this.registerValueObject<IVOOne>({
token: 'IVOOne',
factoryType: 'containerCache',
resolveFactory() {
return { text: 'this is value object' };
}
});
// 更多使用方法参考类型定义
}
}
/** 服务一 */
class ServiceOne extends DomainService {}
/** 实体一 */
class EntityOne extends DomainEntity {}
/** 注册领域 */
gdmb.registerDomain(DomainOne);获取领域相关实例
import { GDMB, Domain } from 'gdmb';
/** 实例化gdmb实例对象,全局单例 */
const gdmb = new GDMB();
// 获取领域对象
const domainOne = gdmb.domains(DomainOne.name);
/** 或者 */
const domainOne = gdmb.domains(DomainOne);
// 获取服务对象
const serviceOne = domainOne.services(ServiceOne.name);
/** 或者 */
const serviceOne = domainOne.services(ServiceOne);
// 获取实体对象
const entityOne = domainOne.entities(EntityOne.name);
/** 或者 */
const entityOne = domainOne.entities(EntityOne);
// 获取值对象
const voOne = domainOne.vos('IVOOne');