新增 AutoMapper 映射配置及 DTO 類別

在 `AutoMappingProfile.cs` 中新增了 AutoMapper 的映射配置,定義了 `DepartmentEntity` 到 `DepartmentDto` 以及 `RoleEntity` 到 `RoleDto` 的映射關係。
在 `DepartmentDto.cs` 和 `RoleDto.cs` 中分別新增了 `DepartmentDto` 和 `RoleDto` 類別,並提供了相應的 XML 註解。
最後,在 `Program.cs` 中新增了 AutoMapper 的服務註冊,將 `AutoMappingProfile` 類別的映射配置加入到服務容器中。
This commit is contained in:
Chen, Chun-Yi 2025-09-30 14:07:48 +08:00
parent d804e92363
commit 797e6b5f3c
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,21 @@
using AutoMapper;
using Models.Entities;
using XisongSpaceBooking_BackEnd.Models.DTOs;
namespace XisongSpaceBooking_BackEnd.Configurations
{
/// <summary>
/// AutoMapper 映射配置檔案
/// </summary>
public class AutoMappingProfile : Profile
{
public AutoMappingProfile()
{
// Department Entity 到 DTO 的映射
CreateMap<DepartmentEntity, DepartmentDto>();
// Role Entity 到 DTO 的映射
CreateMap<RoleEntity, RoleDto>();
}
}
}

View File

@ -0,0 +1,18 @@
namespace XisongSpaceBooking_BackEnd.Models.DTOs
{
/// <summary>
/// 處室資料傳輸物件
/// </summary>
public class DepartmentDto
{
/// <summary>
/// 處室 ID主鍵
/// </summary>
public int DepartmentId { get; set; }
/// <summary>
/// 處室名稱
/// </summary>
public string DepartmentName { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,18 @@
namespace XisongSpaceBooking_BackEnd.Models.DTOs
{
/// <summary>
/// 身份資料傳輸物件
/// </summary>
public class RoleDto
{
/// <summary>
/// 身份 ID主鍵
/// </summary>
public int RoleId { get; set; }
/// <summary>
/// 身份名稱
/// </summary>
public string RoleName { get; set; } = string.Empty;
}
}

View File

@ -7,6 +7,11 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// µù¥U AutoMapper ªA°È
builder.Services.AddAutoMapper(cfg =>
{
}, typeof(AutoMappingProfile).Assembly);
// °t¸m Entity Framework »P³s½u¦À
builder.Services.AddDbContextPool<SpaceBookingDbContext>(options =>
{