Files
DMS/DMS.Application/Services/Database/MenuAppService.cs
David P.G 816827e5e9 refactor: 移除MenuBeanDto,直接使用MenuBean模型
- 删除了 DMS.Application/DTOs/MenuBeanDto.cs 文件
- 在DTOs中将MenuBeanDto类型替换为MenuBean类型
- 更新了IMenuAppService和IMenuManagementService接口中相关方法的参数和返回值类型
- 修改了MenuAppService、MenuManagementService等服务类的实现
- 在DMS.Core/Models/MenuBean.cs中添加了Children属性以支持菜单树结构
- 更新了WPF层相关的菜单处理逻辑
- 修改了映射配置和视图模型中的菜单对象创建方式
- 这一更改简化了数据模型,消除了DTO与模型之间的重复定义,直接在各层之间使用MenuBean实体。
2025-10-13 20:20:09 +08:00

126 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces;
namespace DMS.Application.Services.Database;
/// <summary>
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
/// 实现 <see cref="IMenuAppService"/> 接口。
/// </summary>
public class MenuAppService : IMenuAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
/// <summary>
/// 构造函数通过依赖注入获取仓储管理器和AutoMapper实例。
/// </summary>
/// <param name="repoManager">仓储管理器实例。</param>
/// <param name="mapper">AutoMapper 实例。</param>
public MenuAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
/// <summary>
/// 异步根据ID获取菜单。
/// </summary>
/// <param name="id">菜单ID。</param>
/// <returns>菜单对象。</returns>
public async Task<MenuBean> GetMenuByIdAsync(int id)
{
var menu = await _repoManager.Menus.GetByIdAsync(id);
return _mapper.Map<MenuBean>(menu);
}
/// <summary>
/// 异步获取所有菜单列表。
/// </summary>
/// <returns>菜单列表。</returns>
public async Task<List<MenuBean>> GetAllMenusAsync()
{
var menus = await _repoManager.Menus.GetAllAsync();
return _mapper.Map<List<MenuBean>>(menus);
}
/// <summary>
/// 异步创建一个新菜单(事务性操作)。
/// </summary>
/// <param name="menu">要创建的菜单。</param>
/// <returns>新创建菜单的ID。</returns>
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
public async Task<int> CreateMenuAsync(MenuBean menu)
{
try
{
await _repoManager.BeginTranAsync();
await _repoManager.Menus.AddAsync(menu);
await _repoManager.CommitAsync();
return menu.Id;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("创建菜单时发生错误,操作已回滚。", ex);
}
}
/// <summary>
/// 异步更新一个已存在的菜单(事务性操作)。
/// </summary>
/// <param name="menu">要更新的菜单。</param>
/// <returns>受影响的行数。</returns>
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
public async Task<int> UpdateMenuAsync(MenuBean menu)
{
try
{
await _repoManager.BeginTranAsync();
var dbmenu = await _repoManager.Menus.GetByIdAsync(menu.Id);
if (dbmenu == null)
{
throw new ApplicationException($"Menu with ID {menu.Id} not found.");
}
_mapper.Map(menu, dbmenu);
int res = await _repoManager.Menus.UpdateAsync(dbmenu);
await _repoManager.CommitAsync();
return res;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("更新菜单时发生错误,操作已回滚。", ex);
}
}
/// <summary>
/// 异步删除一个菜单(事务性操作)。
/// </summary>
/// <param name="id">要删除菜单的ID。</param>
/// <returns>如果删除成功则为 true否则为 false。</returns>
/// <exception cref="InvalidOperationException">如果删除菜单失败。</exception>
/// <exception cref="ApplicationException">如果删除菜单时发生其他错误。</exception>
public async Task<bool> DeleteMenuAsync(int id)
{
try
{
await _repoManager.BeginTranAsync();
var delRes = await _repoManager.Menus.DeleteByIdAsync(id);
if (delRes == 0)
{
throw new InvalidOperationException($"删除菜单失败菜单ID:{id}请检查菜单Id是否存在");
}
await _repoManager.CommitAsync();
return true;
}
catch (Exception ex)
{
await _repoManager.RollbackAsync();
throw new ApplicationException("删除菜单时发生错误,操作已回滚。", ex);
}
}
}