动态马普
Dynamic Mapper 是一个依赖性自由库,为对象映射到 JavaScript 提供支持 用于浏览器和节点.js环境。 灵感来自 自动马普。 。 。 .
为了获得最佳经验,请使用Dynamic Mapper和 类型脚本。 。 。 .
安装
npm i @dynamic-mapper/mapper --save
文档
完整的现有文件 这里。 。 。 .
动机
在以下列形式消耗数据时,采用UI应用程序: Response DTO 需要转换为 Domain 接口
后被转化为UI视图(即角反应形式值). 修改后视图需要转换为 Domain 财务报告和审定财务报表
此更新的域对象应当以 Request DTO相当乏味地写那些地图
用于每个单个域对象的方式.

使用量
import { MappingPair, MapperConfiguration } from '@dynamic-mapper/mapper';
interface CustomerDto {
firstName: string;
lastName: string;
}
interface Customer {
firstName: string;
lastName: string;
fullName: string;
}
const CustomerDtoToCustomer = new MappingPair<CustomerDto, Customer>();
const configuration = new MapperConfiguration(cfg => {
cfg.createAutoMap(CustomerDtoToCustomer, {
fullName: opt => opt.mapFrom(src => `${src.firstName} ${src.lastName}`)
});
});
const mapper = configuration.createMapper();
const customerDto: CustomerDto = {
firstName: 'John',
lastName: 'Doe'
};
const customer = mapper.map(CustomerDtoToCustomer, customerDto);
// {
// firstName: 'John',
// lastName: 'Doe',
// fullName: 'John Doe'
// }