From b3edd27cb80fd64966c78558bd3ee763749c1aeb Mon Sep 17 00:00:00 2001 From: Chunyi Date: Tue, 30 Sep 2025 14:29:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Service=20=E5=B1=A4?= =?UTF-8?q?=E4=BB=A5=E8=99=95=E7=90=86=E6=A5=AD=E5=8B=99=E9=82=8F=E8=BC=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `Program.cs` 中註冊 `IRoleService` 和 `IDepartmentService` 的相依性注入。 新增 `DepartmentService` 和 `RoleService` 類別,實作相應的介面,並提供取得所有處室及身份資料的功能,使用 AutoMapper 進行物件映射。 定義 `IDepartmentService` 和 `IRoleService` 介面,包含 `GetAllAsync` 方法以取得資料 DTO 集合。 --- XisongSpaceBooking_BackEnd/Program.cs | 5 +++ .../Services/DepartmentService.cs | 36 +++++++++++++++++++ .../Services/IDepartmentService.cs | 16 +++++++++ .../Services/IRoleService.cs | 16 +++++++++ .../Services/RoleService.cs | 36 +++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 XisongSpaceBooking_BackEnd/Services/DepartmentService.cs create mode 100644 XisongSpaceBooking_BackEnd/Services/IDepartmentService.cs create mode 100644 XisongSpaceBooking_BackEnd/Services/IRoleService.cs create mode 100644 XisongSpaceBooking_BackEnd/Services/RoleService.cs diff --git a/XisongSpaceBooking_BackEnd/Program.cs b/XisongSpaceBooking_BackEnd/Program.cs index b2d3eca..f252fec 100644 --- a/XisongSpaceBooking_BackEnd/Program.cs +++ b/XisongSpaceBooking_BackEnd/Program.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using XisongSpaceBooking_BackEnd.Configurations; using XisongSpaceBooking_BackEnd.Repositories; +using XisongSpaceBooking_BackEnd.Services; var builder = WebApplication.CreateBuilder(args); @@ -17,6 +18,10 @@ builder.Services.AddAutoMapper(cfg => builder.Services.AddScoped(); builder.Services.AddScoped(); +// U Service A +builder.Services.AddScoped(); +builder.Services.AddScoped(); + // tm Entity Framework Psu builder.Services.AddDbContextPool(options => { diff --git a/XisongSpaceBooking_BackEnd/Services/DepartmentService.cs b/XisongSpaceBooking_BackEnd/Services/DepartmentService.cs new file mode 100644 index 0000000..0babda4 --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Services/DepartmentService.cs @@ -0,0 +1,36 @@ +using AutoMapper; +using XisongSpaceBooking_BackEnd.Models.DTOs; +using XisongSpaceBooking_BackEnd.Repositories; + +namespace XisongSpaceBooking_BackEnd.Services +{ + /// + /// 處室業務邏輯實作 + /// + public class DepartmentService : IDepartmentService + { + private readonly IDepartmentRepository _departmentRepository; + private readonly IMapper _mapper; + + /// + /// 建構函式 + /// + /// 處室資料存取 + /// 物件映射器 + public DepartmentService(IDepartmentRepository departmentRepository, IMapper mapper) + { + _departmentRepository = departmentRepository ?? throw new ArgumentNullException(nameof(departmentRepository)); + _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); + } + + /// + /// 取得所有處室資料 + /// + /// 所有處室 DTO 的集合 + public async Task> GetAllAsync() + { + var departments = await _departmentRepository.GetAllAsync(); + return _mapper.Map>(departments); + } + } +} diff --git a/XisongSpaceBooking_BackEnd/Services/IDepartmentService.cs b/XisongSpaceBooking_BackEnd/Services/IDepartmentService.cs new file mode 100644 index 0000000..023ee9a --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Services/IDepartmentService.cs @@ -0,0 +1,16 @@ +using XisongSpaceBooking_BackEnd.Models.DTOs; + +namespace XisongSpaceBooking_BackEnd.Services +{ + /// + /// 處室業務邏輯介面 + /// + public interface IDepartmentService + { + /// + /// 取得所有處室資料 + /// + /// 所有處室 DTO 的集合 + Task> GetAllAsync(); + } +} \ No newline at end of file diff --git a/XisongSpaceBooking_BackEnd/Services/IRoleService.cs b/XisongSpaceBooking_BackEnd/Services/IRoleService.cs new file mode 100644 index 0000000..fa07eee --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Services/IRoleService.cs @@ -0,0 +1,16 @@ +using XisongSpaceBooking_BackEnd.Models.DTOs; + +namespace XisongSpaceBooking_BackEnd.Services +{ + /// + /// 身份業務邏輯介面 + /// + public interface IRoleService + { + /// + /// 取得所有身份資料 + /// + /// 所有身份 DTO 的集合 + Task> GetAllAsync(); + } +} \ No newline at end of file diff --git a/XisongSpaceBooking_BackEnd/Services/RoleService.cs b/XisongSpaceBooking_BackEnd/Services/RoleService.cs new file mode 100644 index 0000000..cb2a626 --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Services/RoleService.cs @@ -0,0 +1,36 @@ +using AutoMapper; +using XisongSpaceBooking_BackEnd.Models.DTOs; +using XisongSpaceBooking_BackEnd.Repositories; + +namespace XisongSpaceBooking_BackEnd.Services +{ + /// + /// 身份業務邏輯實作 + /// + public class RoleService : IRoleService + { + private readonly IRoleRepository _roleRepository; + private readonly IMapper _mapper; + + /// + /// 建構函式 + /// + /// 身份資料存取 + /// 物件映射器 + public RoleService(IRoleRepository roleRepository, IMapper mapper) + { + _roleRepository = roleRepository ?? throw new ArgumentNullException(nameof(roleRepository)); + _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); + } + + /// + /// 取得所有身份資料 + /// + /// 所有身份 DTO 的集合 + public async Task> GetAllAsync() + { + var roles = await _roleRepository.GetAllAsync(); + return _mapper.Map>(roles); + } + } +}