refactor: 移除MenuBeanDto,直接使用MenuBean模型
- 删除了 DMS.Application/DTOs/MenuBeanDto.cs 文件 - 在DTOs中将MenuBeanDto类型替换为MenuBean类型 - 更新了IMenuAppService和IMenuManagementService接口中相关方法的参数和返回值类型 - 修改了MenuAppService、MenuManagementService等服务类的实现 - 在DMS.Core/Models/MenuBean.cs中添加了Children属性以支持菜单树结构 - 更新了WPF层相关的菜单处理逻辑 - 修改了映射配置和视图模型中的菜单对象创建方式 - 这一更改简化了数据模型,消除了DTO与模型之间的重复定义,直接在各层之间使用MenuBean实体。
This commit is contained in:
@@ -24,12 +24,12 @@ public class AppDataStorageService : IAppDataStorageService
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有菜单数据
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<int, MenuBeanDto> Menus { get; } = new();
|
||||
public ConcurrentDictionary<int, MenuBean> Menus { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有菜单数据
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<int, MenuBeanDto> MenuTrees { get; } = new();
|
||||
public ConcurrentDictionary<int, MenuBean> MenuTrees { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 安全字典,用于存储所有MQTT服务器数据
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using AutoMapper;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Interfaces;
|
||||
using System.Collections.Concurrent;
|
||||
@@ -178,11 +177,10 @@ public class DataLoaderService : IDataLoaderService
|
||||
_appDataStorageService.Menus.Clear();
|
||||
_appDataStorageService.MenuTrees.Clear();
|
||||
var menus = await _repositoryManager.Menus.GetAllAsync();
|
||||
var menuDtos = _mapper.Map<List<MenuBeanDto>>(menus);
|
||||
// 将菜单添加到安全字典
|
||||
foreach (var menuDto in menuDtos)
|
||||
foreach (var menuBean in menus)
|
||||
{
|
||||
_appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto);
|
||||
_appDataStorageService.Menus.TryAdd(menuBean.Id, menuBean);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
@@ -28,38 +27,37 @@ public class MenuAppService : IMenuAppService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取菜单数据传输对象。
|
||||
/// 异步根据ID获取菜单。
|
||||
/// </summary>
|
||||
/// <param name="id">菜单ID。</param>
|
||||
/// <returns>菜单数据传输对象。</returns>
|
||||
public async Task<MenuBeanDto> GetMenuByIdAsync(int id)
|
||||
/// <returns>菜单对象。</returns>
|
||||
public async Task<MenuBean> GetMenuByIdAsync(int id)
|
||||
{
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
||||
return _mapper.Map<MenuBeanDto>(menu);
|
||||
return _mapper.Map<MenuBean>(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有菜单数据传输对象列表。
|
||||
/// 异步获取所有菜单列表。
|
||||
/// </summary>
|
||||
/// <returns>菜单数据传输对象列表。</returns>
|
||||
public async Task<List<MenuBeanDto>> GetAllMenusAsync()
|
||||
/// <returns>菜单列表。</returns>
|
||||
public async Task<List<MenuBean>> GetAllMenusAsync()
|
||||
{
|
||||
var menus = await _repoManager.Menus.GetAllAsync();
|
||||
return _mapper.Map<List<MenuBeanDto>>(menus);
|
||||
return _mapper.Map<List<MenuBean>>(menus);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="menuDto">要创建的菜单数据传输对象。</param>
|
||||
/// <param name="menu">要创建的菜单。</param>
|
||||
/// <returns>新创建菜单的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
|
||||
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
|
||||
public async Task<int> CreateMenuAsync(MenuBean menu)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
var menu = _mapper.Map<MenuBean>(menuDto);
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
return menu.Id;
|
||||
@@ -74,21 +72,21 @@ public class MenuAppService : IMenuAppService
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="menuDto">要更新的菜单数据传输对象。</param>
|
||||
/// <param name="menu">要更新的菜单。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
|
||||
public async Task<int> UpdateMenuAsync(MenuBeanDto menuDto)
|
||||
public async Task<int> UpdateMenuAsync(MenuBean menu)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
|
||||
if (menu == null)
|
||||
var dbmenu = await _repoManager.Menus.GetByIdAsync(menu.Id);
|
||||
if (dbmenu == null)
|
||||
{
|
||||
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
|
||||
throw new ApplicationException($"Menu with ID {menu.Id} not found.");
|
||||
}
|
||||
_mapper.Map(menuDto, menu);
|
||||
int res = await _repoManager.Menus.UpdateAsync(menu);
|
||||
_mapper.Map(menu, dbmenu);
|
||||
int res = await _repoManager.Menus.UpdateAsync(dbmenu);
|
||||
await _repoManager.CommitAsync();
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Services.Management;
|
||||
|
||||
@@ -28,17 +28,17 @@ public class MenuManagementService : IMenuManagementService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有菜单DTO列表。
|
||||
/// 异步获取所有菜单列表。
|
||||
/// </summary>
|
||||
public async Task<List<MenuBeanDto>> GetAllMenusAsync()
|
||||
public async Task<List<MenuBean>> GetAllMenusAsync()
|
||||
{
|
||||
return await _menuService.GetAllMenusAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取菜单DTO。
|
||||
/// 异步根据ID获取菜单。
|
||||
/// </summary>
|
||||
public async Task<MenuBeanDto> GetMenuByIdAsync(int id)
|
||||
public async Task<MenuBean> GetMenuByIdAsync(int id)
|
||||
{
|
||||
return await _menuService.GetMenuByIdAsync(id);
|
||||
}
|
||||
@@ -46,24 +46,24 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// <summary>
|
||||
/// 异步创建一个新菜单。
|
||||
/// </summary>
|
||||
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
|
||||
public async Task<int> CreateMenuAsync(MenuBean menu)
|
||||
{
|
||||
var result = await _menuService.CreateMenuAsync(menuDto);
|
||||
var result = await _menuService.CreateMenuAsync(menu);
|
||||
|
||||
// 创建成功后,将菜单添加到内存中
|
||||
if (result > 0)
|
||||
{
|
||||
menuDto.Id = result; // 假设返回的ID是新创建的
|
||||
if (_appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto))
|
||||
menu.Id = result; // 假设返回的ID是新创建的
|
||||
if (_appDataStorageService.Menus.TryAdd(menu.Id, menu))
|
||||
{
|
||||
MenuBeanDto parentMenu = null;
|
||||
if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parent))
|
||||
MenuBean parentMenu = null;
|
||||
if (menu.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menu.ParentId.Value, out var parent))
|
||||
{
|
||||
parentMenu = parent;
|
||||
parent.Children.Add(menuDto);
|
||||
parent.Children.Add(menu);
|
||||
}
|
||||
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Added, menuDto));
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Added, menu));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,17 +73,17 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的菜单。
|
||||
/// </summary>
|
||||
public async Task<int> UpdateMenuAsync(MenuBeanDto menuDto)
|
||||
public async Task<int> UpdateMenuAsync(MenuBean menu)
|
||||
{
|
||||
var result = await _menuService.UpdateMenuAsync(menuDto);
|
||||
var result = await _menuService.UpdateMenuAsync(menu);
|
||||
|
||||
// 更新成功后,更新内存中的菜单
|
||||
if (result > 0 && menuDto != null)
|
||||
if (result > 0 && menu != null)
|
||||
{
|
||||
_appDataStorageService.Menus.AddOrUpdate(menuDto.Id, menuDto, (key, oldValue) => menuDto);
|
||||
_appDataStorageService.Menus.AddOrUpdate(menu.Id, menu, (key, oldValue) => menu);
|
||||
|
||||
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menuDto));
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menu));
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -100,15 +100,15 @@ public class MenuManagementService : IMenuManagementService
|
||||
// 删除成功后,从内存中移除菜单
|
||||
if (result && menu != null)
|
||||
{
|
||||
if (_appDataStorageService.Menus.TryRemove(id, out var menuDto))
|
||||
if (_appDataStorageService.Menus.TryRemove(id, out var menuData))
|
||||
{
|
||||
// 从父菜单中移除子菜单
|
||||
if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parentMenu))
|
||||
if (menuData.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu))
|
||||
{
|
||||
parentMenu.Children.Remove(menuDto);
|
||||
parentMenu.Children.Remove(menuData);
|
||||
}
|
||||
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Deleted, menuDto));
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Deleted, menuData));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// <summary>
|
||||
/// 获取根菜单列表
|
||||
/// </summary>
|
||||
public List<MenuBeanDto> GetRootMenus()
|
||||
public List<MenuBean> GetRootMenus()
|
||||
{
|
||||
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == 0)
|
||||
.ToList();
|
||||
@@ -129,7 +129,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// </summary>
|
||||
/// <param name="parentId">父级菜单ID</param>
|
||||
/// <returns>子菜单列表</returns>
|
||||
public List<MenuBeanDto> GetChildMenus(int parentId)
|
||||
public List<MenuBean> GetChildMenus(int parentId)
|
||||
{
|
||||
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == parentId)
|
||||
.ToList();
|
||||
|
||||
Reference in New Issue
Block a user