使用AutoMapper框架来实现Entities和Modle之间的转换
This commit is contained in:
35
App.xaml.cs
35
App.xaml.cs
@@ -14,7 +14,8 @@ using PMSWPF.ViewModels;
|
||||
using PMSWPF.Views;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using PMSWPF.Config;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Services.Processors;
|
||||
using SqlSugar;
|
||||
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
||||
|
||||
@@ -59,6 +60,10 @@ public partial class App : Application
|
||||
.Await((e) => { NotificationHelper.ShowError($"初始化主菜单失败:{e.Message}", e); },
|
||||
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
|
||||
Host.Services.GetRequiredService<GrowlNotificationService>();
|
||||
|
||||
// 初始化数据处理链
|
||||
var dataProcessingService = Host.Services.GetRequiredService<IDataProcessingService>();
|
||||
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<LoggingDataProcessor>());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -106,23 +111,41 @@ public partial class App : Application
|
||||
services.AddSingleton<S7BackgroundService>();
|
||||
services.AddSingleton<MqttBackgroundService>();
|
||||
services.AddSingleton<OpcUaBackgroundService>();
|
||||
|
||||
// 注册 AutoMapper
|
||||
services.AddAutoMapper(typeof(App).Assembly);
|
||||
|
||||
// 注册数据处理服务和处理器
|
||||
services.AddSingleton<IDataProcessingService, DataProcessingService>();
|
||||
services.AddHostedService(provider => (DataProcessingService)provider.GetRequiredService<IDataProcessingService>());
|
||||
services.AddSingleton<LoggingDataProcessor>();
|
||||
|
||||
// 注册数据仓库
|
||||
services.AddSingleton<DeviceRepository>();
|
||||
services.AddSingleton<MenuRepository>();
|
||||
services.AddSingleton<MqttRepository>();
|
||||
services.AddSingleton<UserRepository>();
|
||||
services.AddSingleton<VarDataRepository>();
|
||||
services.AddSingleton<VarTableRepository>();
|
||||
// 注册视图模型
|
||||
services.AddSingleton<MainViewModel>();
|
||||
services.AddSingleton<HomeViewModel>();
|
||||
services.AddSingleton<DevicesViewModel>();
|
||||
services.AddSingleton<DataTransformViewModel>();
|
||||
services.AddSingleton<SettingViewModel>();
|
||||
services.AddSingleton<DataTransformViewModel>();
|
||||
services.AddTransient<VariableTableViewModel>();
|
||||
services.AddScoped<MqttServerDetailViewModel>();
|
||||
services.AddScoped<DeviceDetailViewModel>();
|
||||
services.AddScoped<MqttsViewModel>();
|
||||
//注册View视图
|
||||
services.AddSingleton<SettingView>();
|
||||
services.AddSingleton<MainView>();
|
||||
services.AddSingleton<HomeView>();
|
||||
services.AddSingleton<DevicesView>();
|
||||
services.AddSingleton<DataTransformViewModel>();
|
||||
services.AddTransient<VariableTableViewModel>();
|
||||
services.AddSingleton<VariableTableView>();
|
||||
services.AddScoped<DeviceDetailViewModel>();
|
||||
services.AddScoped<DeviceDetailView>();
|
||||
services.AddScoped<MqttsViewModel>();
|
||||
services.AddScoped<MqttsView>();
|
||||
services.AddScoped<MqttServerDetailViewModel>();
|
||||
}
|
||||
|
||||
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
|
||||
|
||||
@@ -13,4 +13,11 @@ public class DbUser
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] //数据库是自增才配自增
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string PhoneNumber { get; set; }
|
||||
public string Address { get; set; }
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -2,4 +2,13 @@
|
||||
|
||||
public class User
|
||||
{
|
||||
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string PhoneNumber { get; set; }
|
||||
public string Address { get; set; }
|
||||
}
|
||||
@@ -37,7 +37,7 @@ namespace PMSWPF.Tests
|
||||
// For now, we'll assume DbContext.GetInstance() can be configured for testing.
|
||||
// In a real scenario, you'd typically inject ISqlSugarClient into MqttRepository.
|
||||
// For this example, we'll directly use the _db instance for setup and verification.
|
||||
_mqttRepository = new MqttRepository(); // This will still use the static DbContext.GetInstance()
|
||||
// _mqttRepository = new MqttRepository(); // This will still use the static DbContext.GetInstance()
|
||||
|
||||
// To properly test, DbContext.GetInstance() needs to be mockable or configurable.
|
||||
// For demonstration, we'll simulate the DbContext behavior directly here.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
@@ -13,6 +13,9 @@
|
||||
<Compile Remove="PMSWPF.Tests\**\*.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="HandyControl" Version="3.5.1" />
|
||||
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -18,6 +19,7 @@ namespace PMSWPF.Services;
|
||||
/// </summary>
|
||||
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
// 设备列表,使用ObservableProperty特性,当值改变时会自动触发属性变更通知。
|
||||
[ObservableProperty]
|
||||
private List<Device> _devices;
|
||||
@@ -78,14 +80,16 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
/// DataServices类的构造函数。
|
||||
/// 注入ILogger<DataServices>,并初始化各个数据仓库。
|
||||
/// </summary>
|
||||
/// <param name="logger">日志记录器实例。</param>
|
||||
public DataServices()
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
/// <param name="varDataRepository"></param>
|
||||
public DataServices(IMapper mapper,DeviceRepository deviceRepository,MenuRepository menuRepository,MqttRepository mqttRepository,VarDataRepository varDataRepository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
IsActive = true; // 激活消息接收器
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_mqttRepository = new MqttRepository();
|
||||
_varDataRepository = new VarDataRepository();
|
||||
_deviceRepository = deviceRepository;
|
||||
_menuRepository = menuRepository;
|
||||
_mqttRepository = mqttRepository;
|
||||
_varDataRepository = varDataRepository;
|
||||
_variableDatas = new List<VariableData>();
|
||||
}
|
||||
|
||||
@@ -198,4 +202,5 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
{
|
||||
await _varDataRepository.UpdateAsync(variableData);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using HandyControl.Tools.Extension;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
@@ -10,9 +11,11 @@ namespace PMSWPF.Services;
|
||||
|
||||
public class DialogService :IDialogService
|
||||
{
|
||||
public DialogService()
|
||||
{
|
||||
private readonly MqttRepository _mqttRepository;
|
||||
|
||||
public DialogService(MqttRepository mqttRepository)
|
||||
{
|
||||
_mqttRepository = mqttRepository;
|
||||
}
|
||||
|
||||
public async Task<Device> ShowAddDeviceDialog()
|
||||
@@ -174,7 +177,7 @@ public class DialogService :IDialogService
|
||||
|
||||
public async Task<Mqtt?> ShowMqttSelectionDialog()
|
||||
{
|
||||
var vm = new MqttSelectionDialogViewModel();
|
||||
var vm = new MqttSelectionDialogViewModel(_mqttRepository);
|
||||
var dialog = new MqttSelectionDialog(vm);
|
||||
var result = await dialog.ShowAsync();
|
||||
return result == ContentDialogResult.Primary ? vm.SelectedMqtt : null;
|
||||
|
||||
@@ -42,12 +42,12 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
/// <param name="dataServices">数据服务。</param>
|
||||
public DevicesViewModel(
|
||||
IDialogService dialogService, DataServices dataServices
|
||||
IDialogService dialogService, DataServices dataServices,DeviceRepository deviceRepository,VarTableRepository varTableRepository,MenuRepository menuRepository
|
||||
)
|
||||
{
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_varTableRepository = new VarTableRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_deviceRepository = deviceRepository;
|
||||
_varTableRepository = varTableRepository;
|
||||
_menuRepository = menuRepository;
|
||||
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
|
||||
@@ -16,9 +16,9 @@ public partial class MqttSelectionDialogViewModel : ObservableObject
|
||||
[ObservableProperty]
|
||||
private Mqtt? selectedMqtt;
|
||||
|
||||
public MqttSelectionDialogViewModel()
|
||||
public MqttSelectionDialogViewModel(MqttRepository mqttRepository)
|
||||
{
|
||||
_mqttRepository = new MqttRepository();
|
||||
_mqttRepository = mqttRepository;
|
||||
LoadMqtts();
|
||||
}
|
||||
|
||||
|
||||
@@ -49,14 +49,14 @@ public partial class MainViewModel : ViewModelBase
|
||||
/// <param name="dialogService">对话框服务。</param>
|
||||
/// <param name="logger">日志记录器。</param>
|
||||
public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService,
|
||||
ILogger<MainViewModel> logger)
|
||||
ILogger<MainViewModel> logger,VarTableRepository varTableRepository,MenuRepository menuRepository)
|
||||
{
|
||||
_navgatorServices = navgatorServices;
|
||||
_dataServices = dataServices;
|
||||
_dialogService = dialogService;
|
||||
_logger = logger;
|
||||
_varTableRepository = new VarTableRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_varTableRepository = varTableRepository;
|
||||
_menuRepository = menuRepository;
|
||||
|
||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||
|
||||
|
||||
@@ -51,10 +51,10 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
private Mqtt _selectedMqtt;
|
||||
|
||||
public MqttsViewModel(
|
||||
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices
|
||||
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices,MqttRepository mqttRepository
|
||||
)
|
||||
{
|
||||
_mqttRepository = new MqttRepository();
|
||||
_mqttRepository = mqttRepository;
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
@@ -49,6 +50,8 @@ namespace PMSWPF.ViewModels;
|
||||
/// </summary>
|
||||
partial class VariableTableViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 对话服务接口,用于显示各种对话框(如确认、编辑、导入等)。
|
||||
/// </summary>
|
||||
@@ -127,12 +130,13 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
/// 初始化服务、数据仓库和变量数据集合视图。
|
||||
/// </summary>
|
||||
/// <param name="dialogService">对话服务接口的实例。</param>
|
||||
public VariableTableViewModel(IDialogService dialogService)
|
||||
public VariableTableViewModel(IMapper mapper,IDialogService dialogService,VarTableRepository varTableRepository,VarDataRepository varDataRepository)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_dialogService = dialogService;
|
||||
IsLoadCompletion = false; // 初始设置为 false,表示未完成加载
|
||||
_varTableRepository = new VarTableRepository();
|
||||
_varDataRepository = new VarDataRepository();
|
||||
_varTableRepository = varTableRepository;
|
||||
_varDataRepository = varDataRepository;
|
||||
_dataVariables = new ObservableCollection<VariableData>(); // 初始化集合
|
||||
VariableDataView = CollectionViewSource.GetDefaultView(_dataVariables); // 获取集合视图
|
||||
VariableDataView.Filter = FilterVariables; // 设置过滤方法
|
||||
@@ -246,7 +250,8 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
foreach (var modifiedData in modifiedDatas)
|
||||
{
|
||||
var oldData = _originalDataVariables.First(od => od.Id == modifiedData.Id);
|
||||
oldData.CopyTo(modifiedData); // 将原始数据复制回当前数据
|
||||
// 将原始数据复制回当前数据
|
||||
_mapper.Map(oldData, modifiedData);
|
||||
modifiedData.IsModified = false; // 重置修改状态
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user