使用AutoMapper框架来实现Entities和Modle之间的转换

This commit is contained in:
2025-07-15 22:18:37 +08:00
parent 656bb924bc
commit 25f3ba6f83
17 changed files with 150 additions and 80 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using AutoMapper;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using PMSWPF.Data.Entities;
using PMSWPF.Enums;
@@ -11,13 +12,15 @@ namespace PMSWPF.Data.Repositories;
public class DeviceRepository
{
private readonly IMapper _mapper;
private readonly MenuRepository _menuRepository;
private readonly VarTableRepository _varTableRepository;
public DeviceRepository()
public DeviceRepository(IMapper mapper,MenuRepository menuRepository,VarTableRepository varTableRepository)
{
_menuRepository = new MenuRepository();
_varTableRepository = new VarTableRepository();
_mapper = mapper;
_menuRepository = menuRepository;
_varTableRepository = varTableRepository;
}
@@ -48,7 +51,8 @@ public class DeviceRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Updateable<DbDevice>(device.CopyTo<DbDevice>())
var result = await db.Updateable<DbDevice>(_mapper.Map<DbDevice>(device))
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"编辑设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -70,16 +74,11 @@ public class DeviceRepository
.Includes(d => d.VariableTables, dv => dv.Device)
.Includes(d => d.VariableTables, dvd => dvd.DataVariables, data => data.VariableTable)
.ToListAsync();
var devices = new List<Device>();
foreach (var dbDevice in dlist)
{
var device = dbDevice.CopyTo<Device>();
devices.Add(device);
}
stopwatch.Stop();
NlogHelper.Info($"加载设备列表总耗时:{stopwatch.ElapsedMilliseconds}ms");
var devices= _mapper.Map<List<Device>>(dlist);
return devices;
}
}
@@ -99,7 +98,7 @@ public class DeviceRepository
.FirstAsync(p => p.Id == id);
stopwatch.Stop();
NlogHelper.Info($"根据ID '{id}' 获取设备耗时:{stopwatch.ElapsedMilliseconds}ms");
return result.CopyTo<Device>();
return _mapper.Map<Device>(result);
}
}
@@ -219,7 +218,7 @@ public class DeviceRepository
{
;
// 添加设备
var addDevice = await db.Insertable<DbDevice>(device.CopyTo<DbDevice>())
var addDevice = await db.Insertable<DbDevice>(_mapper.Map<DbDevice>(device))
.ExecuteReturnEntityAsync();
// 4. 为新设备添加菜单
@@ -250,7 +249,7 @@ public class DeviceRepository
}
await _menuRepository.AddVarTableMenu(addDevice, addDeviceMenuId, db);
return addDevice.CopyTo<Device>();
return _mapper.Map<Device>(addDevice);
}

View File

@@ -6,6 +6,7 @@ using PMSWPF.Extensions;
using PMSWPF.Helper;
using PMSWPF.Models;
using SqlSugar;
using AutoMapper;
using PMSWPF.Helper;
@@ -13,9 +14,11 @@ namespace PMSWPF.Data.Repositories;
public class MenuRepository
{
private readonly IMapper _mapper;
public MenuRepository()
public MenuRepository(IMapper mapper)
{
_mapper = mapper;
}
public async Task<int> DeleteMenu(MenuBean menu)
@@ -53,7 +56,7 @@ public class MenuRepository
.ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
foreach (var dbMenu in dbMenuTree)
menuTree.Add(dbMenu.CopyTo<MenuBean>());
menuTree.Add(_mapper.Map<MenuBean>(dbMenu));
stopwatch.Stop();
NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms");
return menuTree;
@@ -83,7 +86,7 @@ public class MenuRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
var result = await db.Insertable<DbMenu>(_mapper.Map<DbMenu>(menu))
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -136,7 +139,7 @@ public class MenuRepository
Icon = SegoeFluentIcons.Devices4.Glyph,
};
menu.ParentId = deviceMainMenu.Id;
var addDeviceMenuId = await db.Insertable<DbMenu>(menu.CopyTo<DbMenu>())
var addDeviceMenuId = await db.Insertable<DbMenu>(_mapper.Map<DbMenu>(menu))
.ExecuteReturnIdentityAsync();
stopwatch.Stop();
NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -170,7 +173,7 @@ public class MenuRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Updateable<DbMenu>(menu.CopyTo<DbMenu>())
var result = await db.Updateable<DbMenu>(_mapper.Map<DbMenu>(menu))
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -187,7 +190,7 @@ public class MenuRepository
.FirstAsync(m => m.DataId == dataId && m.Type == menuType);
stopwatch.Stop();
NlogHelper.Info($"根据DataId '{dataId}' 和 MenuType '{menuType}' 获取菜单耗时:{stopwatch.ElapsedMilliseconds}ms");
return result?.CopyTo<MenuBean>();
return _mapper.Map<MenuBean>(result);
}
}
@@ -195,6 +198,6 @@ public class MenuRepository
{
using var db = DbContext.GetInstance();
var dbMenu= await db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);
return dbMenu.CopyTo<MenuBean>();
return _mapper.Map<MenuBean>(dbMenu);
}
}

View File

@@ -1,13 +1,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using PMSWPF.Data.Entities;
using PMSWPF.Models;
using PMSWPF.Extensions;
using SqlSugar;
using PMSWPF.Helper;
using AutoMapper;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using PMSWPF.Data.Entities;
using PMSWPF.Enums;
using PMSWPF.Helper;
using PMSWPF.Models;
namespace PMSWPF.Data.Repositories;
@@ -17,10 +14,12 @@ namespace PMSWPF.Data.Repositories;
public class MqttRepository
{
private readonly MenuRepository _menuRepository;
private readonly IMapper _mapper;
public MqttRepository()
public MqttRepository(MenuRepository menuRepository, IMapper mapper)
{
_menuRepository = new MenuRepository();
_menuRepository = menuRepository;
_mapper = mapper;
}
/// <summary>
@@ -39,7 +38,7 @@ public class MqttRepository
.SingleAsync();
stopwatch.Stop();
NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.CopyTo<Mqtt>();
return _mapper.Map<Mqtt>(result);
}
}
@@ -57,7 +56,7 @@ public class MqttRepository
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.Select(m => m.CopyTo<Mqtt>())
return result.Select(m => _mapper.Map<Mqtt>(m))
.ToList();
}
}
@@ -75,7 +74,7 @@ public class MqttRepository
await db.BeginTranAsync();
try
{
var result = await db.Insertable(mqtt.CopyTo<DbMqtt>())
var result = await db.Insertable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteReturnIdentityAsync();
var mqttMenu = await _menuRepository.GetMainMenuByName("Mqtt服务器");
// Add menu entry
@@ -115,7 +114,7 @@ public class MqttRepository
await db.BeginTranAsync();
try
{
var result = await db.Updateable(mqtt.CopyTo<DbMqtt>())
var result = await db.Updateable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteCommandAsync();
// Update menu entry
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu);

View File

@@ -6,6 +6,7 @@ using PMSWPF.Extensions;
using PMSWPF.Helper;
using PMSWPF.Models;
using SqlSugar;
using AutoMapper;
namespace PMSWPF.Data.Repositories;
@@ -14,6 +15,12 @@ namespace PMSWPF.Data.Repositories;
/// </summary>
public class VarDataRepository
{
private readonly IMapper _mapper;
public VarDataRepository(IMapper mapper)
{
_mapper = mapper;
}
/// <summary>
/// 根据ID获取VariableData
@@ -51,7 +58,7 @@ public class VarDataRepository
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取所有VariableData耗时{stopwatch.ElapsedMilliseconds}ms");
return result.Select(d => d.CopyTo<VariableData>())
return result.Select(d => _mapper.Map<VariableData>(d))
.ToList();
}
}
@@ -71,7 +78,7 @@ public class VarDataRepository
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms");
return result.Select(d=>d.CopyTo<VariableData>()).ToList();
return result.Select(d=>_mapper.Map<VariableData>(d)).ToList();
}
}
@@ -103,11 +110,11 @@ public class VarDataRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbVarData = await db.Insertable(variableData.CopyTo<DbVariableData>())
var dbVarData = await db.Insertable(_mapper.Map<DbVariableData>(variableData))
.ExecuteReturnEntityAsync();
stopwatch.Stop();
NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return dbVarData.CopyTo<VariableData>();
return _mapper.Map<VariableData>(dbVarData);
}
/// <summary>
@@ -140,7 +147,7 @@ public class VarDataRepository
stopwatch.Start();
Stopwatch stopwatch2 = new Stopwatch();
stopwatch2.Start();
var dbList = variableDatas.Select(vb => vb.CopyTo<DbVariableData>())
var dbList = variableDatas.Select(vb => _mapper.Map<DbVariableData>(vb))
.ToList();
stopwatch2.Stop();
NlogHelper.Info($"复制 VariableData'{variableDatas.Count()}'个, 耗时:{stopwatch2.ElapsedMilliseconds}ms");
@@ -165,7 +172,7 @@ public class VarDataRepository
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.UpdateNav(variableData.CopyTo<DbVariableData>())
var result = await _db.UpdateNav(_mapper.Map<DbVariableData>(variableData))
.Include(d => d.Mqtts)
.ExecuteCommandAsync();
stopwatch.Stop();
@@ -195,7 +202,7 @@ public class VarDataRepository
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbVarDatas = variableDatas.Select(vd => vd.CopyTo<DbVariableData>());
var dbVarDatas = variableDatas.Select(vd => _mapper.Map<DbVariableData>(vd));
var result = await db.Updateable<DbVariableData>(dbVarDatas.ToList())
.ExecuteCommandAsync();
@@ -268,7 +275,7 @@ public class VarDataRepository
stopwatch.Start();
using var _db = DbContext.GetInstance();
var dbList = variableDatas.Select(vd => vd.CopyTo<DbVariableData>())
var dbList = variableDatas.Select(vd => _mapper.Map<DbVariableData>(vd))
.ToList();
var result = await _db.Deleteable<DbVariableData>(dbList)
.ExecuteCommandAsync();

View File

@@ -6,11 +6,18 @@ using PMSWPF.Helper;
using PMSWPF.Models;
using SqlSugar;
using System.Diagnostics;
using AutoMapper;
namespace PMSWPF.Data.Repositories;
public class VarTableRepository
{
private readonly IMapper _mapper;
public VarTableRepository(IMapper mapper)
{
_mapper = mapper;
}
/// <summary>
/// 添加变量表
@@ -43,11 +50,11 @@ public class VarTableRepository
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var addVarTabel = await db.Insertable<DbVariableTable>(variableTable.CopyTo<DbVariableTable>())
var addVarTabel = await db.Insertable<DbVariableTable>(_mapper.Map<DbVariableTable>(variableTable))
.ExecuteReturnEntityAsync();
stopwatch.Stop();
NlogHelper.Info($"添加设备 '{addVarTabel.Name}' 的默认变量表耗时:{stopwatch.ElapsedMilliseconds}ms");
return addVarTabel.CopyTo<VariableTable>();
return _mapper.Map<VariableTable>(addVarTabel);
}
/// <summary>
@@ -77,7 +84,7 @@ public class VarTableRepository
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var result = await db.Updateable<DbVariableTable>(variableTable.CopyTo<DbVariableTable>())
var result = await db.Updateable<DbVariableTable>(_mapper.Map<DbVariableTable>(variableTable))
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"编辑变量表 '{variableTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -114,7 +121,7 @@ public class VarTableRepository
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// 转换对象
var res= await db.Deleteable<DbVariableTable>(varTable.CopyTo<DbVariableTable>())
var res= await db.Deleteable<DbVariableTable>(_mapper.Map<DbVariableTable>(varTable))
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"删除变量表 '{varTable.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -131,7 +138,7 @@ public class VarTableRepository
if (deviceVariableTables == null || deviceVariableTables.Count == 0)
return;
// 转换对象
var dbList = deviceVariableTables.Select(v => v.CopyTo<DbVariableTable>())
var dbList = deviceVariableTables.Select(v => _mapper.Map<DbVariableTable>(v))
.ToList();
await db.Deleteable<DbVariableTable>(dbList)
.ExecuteCommandAsync();