将所有的异步方法名称都添加Async

This commit is contained in:
2025-07-16 19:36:18 +08:00
parent 76f16d50be
commit f24769e94c
5 changed files with 47 additions and 57 deletions

View File

@@ -29,13 +29,13 @@ public class DeviceRepository
/// </summary> /// </summary>
/// <param name="device">要编辑的设备对象。</param> /// <param name="device">要编辑的设备对象。</param>
/// <returns>受影响的行数。</returns> /// <returns>受影响的行数。</returns>
public async Task<int> Edit(Device device) public async Task<int> UpdateAsync(Device device)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
var result = await Edit(device, db); var result = await UpdateAsync(device, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result; return result;
@@ -47,7 +47,7 @@ public class DeviceRepository
/// </summary> /// </summary>
/// <param name="device">要编辑的设备对象。</param> /// <param name="device">要编辑的设备对象。</param>
/// <returns>受影响的行数。</returns> /// <returns>受影响的行数。</returns>
public async Task<int> Edit(Device device, SqlSugarClient db) public async Task<int> UpdateAsync(Device device, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -64,7 +64,7 @@ public class DeviceRepository
/// 获取设备列表 /// 获取设备列表
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task<List<Device>> GetAll() public async Task<List<Device>> GetAllAsync()
{ {
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
@@ -88,7 +88,7 @@ public class DeviceRepository
/// </summary> /// </summary>
/// <param name="id">设备ID。</param> /// <param name="id">设备ID。</param>
/// <returns>对应的DbDevice对象。</returns> /// <returns>对应的DbDevice对象。</returns>
public async Task<Device> GetById(int id) public async Task<Device> GetByIdAsync(int id)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -108,7 +108,7 @@ public class DeviceRepository
/// </summary> /// </summary>
/// <param name="id">要删除的设备ID。</param> /// <param name="id">要删除的设备ID。</param>
/// <returns>受影响的行数。</returns> /// <returns>受影响的行数。</returns>
public async Task<int> Delete(Device device, List<MenuBean> menus) public async Task<int> DeleteAsync(Device device, List<MenuBean> menus)
{ {
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
@@ -117,7 +117,7 @@ public class DeviceRepository
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
var res = await Delete(device, menus, db); var res = await DeleteAsync(device, menus, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -143,7 +143,7 @@ public class DeviceRepository
/// <param name="db"></param> /// <param name="db"></param>
/// <param name="id">要删除的设备ID。</param> /// <param name="id">要删除的设备ID。</param>
/// <returns>受影响的行数。</returns> /// <returns>受影响的行数。</returns>
public async Task<int> Delete(Device device, List<MenuBean> menus, SqlSugarClient db) public async Task<int> DeleteAsync(Device device, List<MenuBean> menus, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -156,7 +156,7 @@ public class DeviceRepository
var menu = DataServicesHelper.FindMenusForDevice(device, menus); var menu = DataServicesHelper.FindMenusForDevice(device, menus);
if (menu == null) if (menu == null)
throw new NullReferenceException($"没有找到设备:{device.Name},的菜单对象。"); throw new NullReferenceException($"没有找到设备:{device.Name},的菜单对象。");
await _menuRepository.DeleteMenu(menu, db); await _menuRepository.DeleteAsync(menu, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"删除设备:{device.Name},耗时:{stopwatch.ElapsedMilliseconds}ms");
return result; return result;
@@ -167,7 +167,7 @@ public class DeviceRepository
/// 添加设备 /// 添加设备
/// </summary> /// </summary>
/// <param name="device"></param> /// <param name="device"></param>
public async Task Add(Device device) public async Task AddAsync(Device device)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -185,7 +185,7 @@ public class DeviceRepository
throw new InvalidOperationException("设备名称已经存在。"); throw new InvalidOperationException("设备名称已经存在。");
// 2. 将设备添加到数据库 // 2. 将设备添加到数据库
var addDevice = await Add(device, db); var addDevice = await AddAsync(device, db);
await db.CommitTranAsync(); await db.CommitTranAsync();
@@ -214,7 +214,7 @@ public class DeviceRepository
/// <param name="db"></param> /// <param name="db"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="InvalidOperationException"></exception> /// <exception cref="InvalidOperationException"></exception>
private async Task<Device> Add(Device device, SqlSugarClient db) private async Task<Device> AddAsync(Device device, SqlSugarClient db)
{ {
; ;
// 添加设备 // 添加设备
@@ -222,7 +222,7 @@ public class DeviceRepository
.ExecuteReturnEntityAsync(); .ExecuteReturnEntityAsync();
// 4. 为新设备添加菜单 // 4. 为新设备添加菜单
var addDeviceMenuId = await _menuRepository.Add(addDevice, db); var addDeviceMenuId = await _menuRepository.AddAsync(addDevice, db);
if (device.IsAddDefVarTable) if (device.IsAddDefVarTable)
{ {
@@ -235,7 +235,7 @@ public class DeviceRepository
varTable.Description = "默认变量表"; varTable.Description = "默认变量表";
varTable.ProtocolType = device.ProtocolType; varTable.ProtocolType = device.ProtocolType;
device.VariableTables.Add(varTable); device.VariableTables.Add(varTable);
var addVarTable = await _varTableRepository.Add(varTable, db); var addVarTable = await _varTableRepository.AddAsync(varTable, db);
// 添加添加变量表的菜单 // 添加添加变量表的菜单
var varTableMenu = new MenuBean() var varTableMenu = new MenuBean()
{ {
@@ -245,10 +245,10 @@ public class DeviceRepository
ParentId = addDeviceMenuId, ParentId = addDeviceMenuId,
DataId = addVarTable.Id DataId = addVarTable.Id
}; };
await _menuRepository.Add(varTableMenu, db); await _menuRepository.AddAsync(varTableMenu, db);
} }
await _menuRepository.AddVarTableMenu(addDevice, addDeviceMenuId, db); await _menuRepository.AddVarTableMenuAsync(addDevice, addDeviceMenuId, db);
return _mapper.Map<Device>(addDevice); return _mapper.Map<Device>(addDevice);
} }

View File

@@ -21,17 +21,13 @@ public class MenuRepository
_mapper = mapper; _mapper = mapper;
} }
public async Task<int> DeleteMenu(MenuBean menu) public async Task<int> DeleteAsync(MenuBean menu)
{ {
Stopwatch stopwatch = new Stopwatch(); using var db = DbContext.GetInstance();
stopwatch.Start(); return await DeleteAsync(menu, db);
using (var db = DbContext.GetInstance())
{
return await DeleteMenu(menu, db);
}
} }
public async Task<int> DeleteMenu(MenuBean menu, SqlSugarClient db) public async Task<int> DeleteAsync(MenuBean menu, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
@@ -45,7 +41,7 @@ public class MenuRepository
return result; return result;
} }
public async Task<List<MenuBean>> GetMenuTrees() public async Task<List<MenuBean>> GetMenuTreesAsync()
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -64,12 +60,12 @@ public class MenuRepository
} }
public async Task<int> Add(MenuBean menu) public async Task<int> AddAsync(MenuBean menu)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
using var db = DbContext.GetInstance(); using var db = DbContext.GetInstance();
var result = await Add(menu, db); var result = await AddAsync(menu, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result; return result;
@@ -82,7 +78,7 @@ public class MenuRepository
/// <param name="menu"></param> /// <param name="menu"></param>
/// <param name="db"></param> /// <param name="db"></param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Add(MenuBean menu, SqlSugarClient db) public async Task<int> AddAsync(MenuBean menu, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -94,7 +90,7 @@ public class MenuRepository
} }
public async Task<int> AddVarTableMenu(DbDevice dbDevice, int parentMenuId, SqlSugarClient db) public async Task<int> AddVarTableMenuAsync(DbDevice dbDevice, int parentMenuId, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -121,7 +117,7 @@ public class MenuRepository
/// <param name="db"></param> /// <param name="db"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="InvalidOperationException"></exception> /// <exception cref="InvalidOperationException"></exception>
public async Task<int> Add(DbDevice device, SqlSugarClient db) public async Task<int> AddAsync(DbDevice device, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -151,13 +147,13 @@ public class MenuRepository
/// </summary> /// </summary>
/// <param name="menu"></param> /// <param name="menu"></param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Edit(MenuBean menu) public async Task<int> UpdateAsync(MenuBean menu)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
var result = await Edit(menu, db); var result = await UpdateAsync(menu, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result; return result;
@@ -169,7 +165,7 @@ public class MenuRepository
/// </summary> /// </summary>
/// <param name="menu"></param> /// <param name="menu"></param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Edit(MenuBean menu, SqlSugarClient db) public async Task<int> UpdateAsync(MenuBean menu, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -180,7 +176,7 @@ public class MenuRepository
return result; return result;
} }
public async Task<MenuBean?> GetMenuByDataId(int dataId, MenuType menuType) public async Task<MenuBean?> GetMenuByDataIdAsync(int dataId, MenuType menuType)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -194,7 +190,7 @@ public class MenuRepository
} }
} }
public async Task<MenuBean> GetMainMenuByName(string name) public async Task<MenuBean> GetMainMenuByNameAsync(string name)
{ {
using var db = DbContext.GetInstance(); using var db = DbContext.GetInstance();
var dbMenu= await db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu); var dbMenu= await db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);

View File

@@ -27,7 +27,7 @@ public class MqttRepository
/// </summary> /// </summary>
/// <param name="id">主键ID</param> /// <param name="id">主键ID</param>
/// <returns></returns> /// <returns></returns>
public async Task<Mqtt> GetById(int id) public async Task<Mqtt> GetByIdAsync(int id)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -46,7 +46,7 @@ public class MqttRepository
/// 获取所有Mqtt配置 /// 获取所有Mqtt配置
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task<List<Mqtt>> GetAll() public async Task<List<Mqtt>> GetAllAsync()
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -66,7 +66,7 @@ public class MqttRepository
/// </summary> /// </summary>
/// <param name="mqtt">Mqtt实体</param> /// <param name="mqtt">Mqtt实体</param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Add(Mqtt mqtt) public async Task<int> AddAsync(Mqtt mqtt)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -76,8 +76,8 @@ public class MqttRepository
{ {
var result = await db.Insertable(_mapper.Map<DbMqtt>(mqtt)) var result = await db.Insertable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteReturnIdentityAsync(); .ExecuteReturnIdentityAsync();
var mqttMenu = await _menuRepository.GetMainMenuByName("Mqtt服务器"); var mqttMenu = await _menuRepository.GetMainMenuByNameAsync("Mqtt服务器");
// Add menu entry // AddAsync menu entry
var menu = new MenuBean() var menu = new MenuBean()
{ {
Name = mqtt.Name, Name = mqtt.Name,
@@ -86,7 +86,7 @@ public class MqttRepository
DataId = result, DataId = result,
ParentId = mqttMenu.Id, ParentId = mqttMenu.Id,
}; };
await _menuRepository.Add(menu, db); await _menuRepository.AddAsync(menu, db);
await db.CommitTranAsync(); await db.CommitTranAsync();
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -105,7 +105,7 @@ public class MqttRepository
/// </summary> /// </summary>
/// <param name="mqtt">Mqtt实体</param> /// <param name="mqtt">Mqtt实体</param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Edit(Mqtt mqtt) public async Task<int> UpdateAsync(Mqtt mqtt)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -117,11 +117,11 @@ public class MqttRepository
var result = await db.Updateable(_mapper.Map<DbMqtt>(mqtt)) var result = await db.Updateable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteCommandAsync(); .ExecuteCommandAsync();
// Update menu entry // Update menu entry
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu); var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu != null) if (menu != null)
{ {
menu.Name = mqtt.Name; menu.Name = mqtt.Name;
await _menuRepository.Edit(menu, db); await _menuRepository.UpdateAsync(menu, db);
} }
await db.CommitTranAsync(); await db.CommitTranAsync();
@@ -143,7 +143,7 @@ public class MqttRepository
/// </summary> /// </summary>
/// <param name="mqtt">Mqtt实体</param> /// <param name="mqtt">Mqtt实体</param>
/// <returns></returns> /// <returns></returns>
public async Task<int> Delete(Mqtt mqtt) public async Task<int> DeleteAsync(Mqtt mqtt)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -156,10 +156,10 @@ public class MqttRepository
.In(mqtt.Id) .In(mqtt.Id)
.ExecuteCommandAsync(); .ExecuteCommandAsync();
// DeleteAsync menu entry // DeleteAsync menu entry
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu); var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu!=null ) if (menu!=null )
{ {
await _menuRepository.DeleteMenu(menu, db); await _menuRepository.DeleteAsync(menu, db);
} }
await db.CommitTranAsync(); await db.CommitTranAsync();

View File

@@ -67,7 +67,7 @@ public class VarDataRepository
/// 获取所有VariableData /// 获取所有VariableData
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task<List<VariableData>> GetByVariableTableId(int varTableId) public async Task<List<VariableData>> GetByVariableTableIdAsync(int varTableId)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();

View File

@@ -24,13 +24,13 @@ public class VarTableRepository
/// </summary> /// </summary>
/// <param name="varTable"></param> /// <param name="varTable"></param>
/// <returns>变量表的ID</returns> /// <returns>变量表的ID</returns>
public async Task<VariableTable> Add(VariableTable varTable) public async Task<VariableTable> AddAsync(VariableTable varTable)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
using (var db = DbContext.GetInstance()) using (var db = DbContext.GetInstance())
{ {
var addVarTable = await Add(varTable, db); var addVarTable = await AddAsync(varTable, db);
stopwatch.Stop(); stopwatch.Stop();
NlogHelper.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); NlogHelper.Info($"添加变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -45,7 +45,7 @@ public class VarTableRepository
/// <param name="db"></param> /// <param name="db"></param>
/// <param name="dbDevice"></param> /// <param name="dbDevice"></param>
/// <returns></returns> /// <returns></returns>
public async Task<VariableTable> Add(VariableTable variableTable, SqlSugarClient db) public async Task<VariableTable> AddAsync(VariableTable variableTable, SqlSugarClient db)
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
@@ -142,12 +142,6 @@ public class VarTableRepository
.ExecuteCommandAsync(); .ExecuteCommandAsync();
} }
public async Task<VariableTable> AddAsync(VariableTable varTable)
{
using var db = DbContext.GetInstance();
return await Add(varTable);
}
} }