‹ 首页

abp-object-mapping

@burakdmir · 收录于 昨天 · 上游提交 2 周前

ABP Framework v10.x (10.4/10.5) object mapping: IObjectMapper, Mapperly (default since v10.4, MapperBase), AutoMapper profiles, entity↔DTO conversion, AutoMap attributes. Use when you need object mapping or DTO mapping in ABP.

适合你,如果你在使用 ABP 框架时需要实体与 DTO 的映射转换

/ 通过 npx 安装 校验哈希
npx oh-my-skill add burakdmir/abp-skills/abp-object-mapping
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- burakdmir/abp-skills/abp-object-mapping
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify burakdmir/abp-skills/abp-object-mapping
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
13GitHub stars
~877上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 ce71259 · 表述以原文为准
它做什么

装上此技能后,Claude 能够帮助你在 ABP Framework v10.x 中处理对象映射,包括实体与 DTO 之间的转换。它会提供使用 IObjectMapper 抽象、Mapperly(v10.4 起默认)或 AutoMapper 的代码示例及配置方法。

什么时候触发

当用户提到“ABP object mapping”、“ABP entity to DTO mapping”或“ABP DTO mapping”等关键词时触发。

装好后可以这样说
Claude 会提供 Profile 类和模块配置代码。
技能原文 SKILL.md作者撰写 · MIT · ce71259

ABP Framework — Object Mapping

ABP Framework v10.x (10.4/10.5) object mapping guide. Entity↔DTO conversion via the IObjectMapper abstraction. Since v10.4, Mapperly (compile-time, source-generated) is the default provider; AutoMapper is also supported. Stick with whichever one the solution already uses.

Trigger
  • "ABP object mapping"
  • "ABP entity to DTO mapping"
  • "ABP DTO mapping"
  • "ABP Mapperly"
  • "ABP AutoMapper"
  • "ABP IObjectMapper"
The IObjectMapper Abstraction

ABP base classes (ApplicationService, etc.) come with an ObjectMapper property out of the box:

public class BookAppService : ApplicationService
{
    public async Task<BookDto> GetAsync(Guid id)
    {
        var book = await _bookRepository.GetAsync(id);
        return ObjectMapper.Map<Book, BookDto>(book);            // single object
    }

    public async Task<List<BookDto>> GetAllAsync()
    {
        var books = await _bookRepository.GetListAsync();
        return ObjectMapper.Map<List<Book>, List<BookDto>>(books); // list
    }

    // Map onto an existing object (source, destination)
    public void Update(Book book, UpdateBookDto input)
        => ObjectMapper.Map(input, book);
}

In places without a base class, inject IObjectMapper.

Mapperly (Default Since v10.4)

A compile-time source generator — no reflection, fast. ABP integration uses MapperBase<TSource, TDestination>:

[Mapper]
public partial class BookToBookDtoMapper : MapperBase<Book, BookDto>
{
    public override partial BookDto Map(Book source);
    public override partial void Map(Book source, BookDto destination);
}

[Mapper]
public partial class CreateUpdateBookDtoToBookMapper : MapperBase<CreateUpdateBookDto, Book>
{
    public override partial Book Map(CreateUpdateBookDto source);
    public override partial void Map(CreateUpdateBookDto source, Book destination);
}

Module configuration:

[DependsOn(typeof(AbpMapperlyModule))]
public class MyApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddMapperlyObjectMapper<MyApplicationModule>();
    }
}

Since the mappers derive from MapperBase, an ObjectMapper.Map<Book, BookDto>(...) call uses them automatically.

Custom Field Mapping (Mapperly)
[Mapper]
public partial class BookMapper : MapperBase<Book, BookDto>
{
    [MapProperty(nameof(Book.Name), nameof(BookDto.Title))]   // different name
    public override partial BookDto Map(Book source);
    public override partial void Map(Book source, BookDto destination);
}
AutoMapper (Alternative)

If the solution uses AutoMapper, define a profile:

public class MyApplicationAutoMapperProfile : Profile
{
    public MyApplicationAutoMapperProfile()
    {
        CreateMap<Book, BookDto>();
        CreateMap<CreateUpdateBookDto, Book>();
    }
}

Module configuration:

[DependsOn(typeof(AbpAutoMapperModule))]
public class MyApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAutoMapperObjectMapper<MyApplicationModule>();
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<MyApplicationModule>(validate: true);
        });
    }
}
Attribute-Based Mapping (AutoMapper)

On the DTO:

[AutoMapFrom(typeof(Book))]      // Book → BookDto
public class BookDto : EntityDto<Guid>
{
    public string Name { get; set; }
}

[AutoMapTo(typeof(Book))]        // CreateBookDto → Book
public class CreateBookDto { public string Name { get; set; } }
Best Practices
  1. Use the ObjectMapper abstraction — don't depend directly on the provider (Mapperly/AutoMapper)
  2. Stick with the solution's provider — for new projects, prefer Mapperly (default since v10.4)
  3. Keep mappings in the Application layer — entity↔DTO conversion is an application responsibility
  4. Use validate: true with AutoMapper — catch unmapped fields early
  5. Map entity to DTO, do the reverse carefully — on create/update, only map allowed fields
Related
  • [DDD](../abp-ddd/SKILL.md) — DTO design, application service
  • [API](../abp-api/SKILL.md) — how DTOs are reflected in proxies
  • [Development Flow](../abp-development-flow/SKILL.md) — where the mapper fits in the end-to-end flow
  • ABP Docs: https://abp.io/docs/latest/framework/infrastructure/object-to-object-mapping
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。