RbacMapsterRegister.cs 1.01 KB
using Mapster;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Menu;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared;
using Yi.Framework.Rbac.Domain.Shared.Dtos;

namespace Yi.Framework.Rbac.Application.ObjectMapping;

/// <summary>
/// <c>Menu.ParentId</c> 实体为字符串,对外 DTO 仍为 <see cref="Guid"/>。
/// </summary>
public class RbacMapsterRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<MenuAggregateRoot, MenuGetOutputDto>()
            .Map(d => d.ParentId, s => MenuParentIdConverter.ToGuid(s.ParentId));

        config.NewConfig<MenuAggregateRoot, MenuGetListOutputDto>()
            .Map(d => d.ParentId, s => MenuParentIdConverter.ToGuid(s.ParentId));

        // 登录组装用户信息时使用;库中 ParentId 可能为 "0",不能交给 Mapster 默认 string→Guid
        config.NewConfig<MenuAggregateRoot, MenuDto>()
            .Map(d => d.ParentId, s => MenuParentIdConverter.ToGuid(s.ParentId));
    }
}