diff --git a/DMS.Infrastructure.UnitTests/Repository_Test/BaseRepositoryTests.cs b/DMS.Infrastructure.UnitTests/Repository_Test/BaseRepositoryTests.cs index a1b9c2b..bc84781 100644 --- a/DMS.Infrastructure.UnitTests/Repository_Test/BaseRepositoryTests.cs +++ b/DMS.Infrastructure.UnitTests/Repository_Test/BaseRepositoryTests.cs @@ -16,7 +16,7 @@ namespace DMS.Infrastructure.UnitTests.Repository_Test public BaseRepositoryTests() { // Load real connection settings - var connectionSettings = new Config.ConnectionSettings() + var connectionSettings = new Config.AppSettings() { Database = "DMS_test" }; diff --git a/DMS.Infrastructure/Configurations/ConnectionSettings.cs b/DMS.Infrastructure/Configurations/AppSettings.cs similarity index 64% rename from DMS.Infrastructure/Configurations/ConnectionSettings.cs rename to DMS.Infrastructure/Configurations/AppSettings.cs index dba00ad..69a5fd7 100644 --- a/DMS.Infrastructure/Configurations/ConnectionSettings.cs +++ b/DMS.Infrastructure/Configurations/AppSettings.cs @@ -1,10 +1,12 @@ -using System; -using System.IO; +using DMS.Core.Models; using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; namespace DMS.Config { - public class ConnectionSettings + public class DatabaseSettings { public string DbType { get; set; } = "MySql"; public string Server { get; set; } = "127.0.0.1"; @@ -12,22 +14,28 @@ namespace DMS.Config public string UserId { get; set; } = "root"; public string Password { get; set; } = "Pgw15221236646"; public string Database { get; set; } = "pmswpf"; + } + + public class AppSettings + { + public DatabaseSettings Database { get; set; } = new DatabaseSettings(); public string Theme { get; set; } = "跟随系统"; public bool EnableS7Service { get; set; } = true; public bool EnableMqttService { get; set; } = true; public bool EnableOpcUaService { get; set; } = true; public bool MinimizeToTrayOnClose { get; set; } = true; + public List Menus { get; set; } = new List(); - private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "connectionSettings.json"); + private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appSettings.json"); - public static ConnectionSettings Load() + public static AppSettings Load() { if (File.Exists(SettingsFilePath)) { string json = File.ReadAllText(SettingsFilePath); - return JsonConvert.DeserializeObject(json); + return JsonConvert.DeserializeObject(json); } - return new ConnectionSettings(); // Return default settings if file doesn't exist + return new AppSettings(); } public void Save() @@ -38,8 +46,7 @@ namespace DMS.Config public string ToConnectionString() { - // This example is for MySQL. You'll need to adjust for other database types. - return $"server={Server};port={Port};user={UserId};password={Password};database={Database};"; + return $"server={Database.Server};port={Database.Port};user={Database.UserId};password={Database.Password};database={Database.Database};"; } } } diff --git a/DMS.Infrastructure/Data/SqlSugarDbContext.cs b/DMS.Infrastructure/Data/SqlSugarDbContext.cs index 0de098a..947321b 100644 --- a/DMS.Infrastructure/Data/SqlSugarDbContext.cs +++ b/DMS.Infrastructure/Data/SqlSugarDbContext.cs @@ -6,14 +6,14 @@ using System.Threading.Tasks; namespace DMS.Infrastructure.Data; -public class SqlSugarDbContext : ITransaction +public class SqlSugarDbContext { private readonly SqlSugarClient _db; - public SqlSugarDbContext(ConnectionSettings settings) + public SqlSugarDbContext(AppSettings settings) { var connectionString = settings.ToConnectionString(); - var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.DbType); + var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.Database.DbType); _db = new SqlSugarClient(new ConnectionConfig { @@ -25,23 +25,6 @@ public class SqlSugarDbContext : ITransaction } - public async Task BeginTranAsync() - { - await _db.BeginTranAsync(); - } - - public async Task CommitTranAsync() - { - await _db.CommitTranAsync(); - } - - - - public async Task RollbackTranAsync() - { - await _db.RollbackTranAsync(); - } - public SqlSugarClient GetInstance() { return _db; diff --git a/DMS.Infrastructure/Interfaces/IDbContext.cs b/DMS.Infrastructure/Interfaces/IDbContext.cs deleted file mode 100644 index 3447bb9..0000000 --- a/DMS.Infrastructure/Interfaces/IDbContext.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Threading.Tasks; - -namespace DMS.Infrastructure.Interfaces -{ - public interface IDbContext - { - // Define common database operations here, e.g., - // Task GetByIdAsync(int id) where TEntity : class; - // Task AddAsync(TEntity entity) where TEntity : class; - // Task UpdateAsync(TEntity entity) where TEntity : class; - // Task DeleteAsync(TEntity entity) where TEntity : class; - // ITransaction GetClient(); // This should NOT be here if you want to hide SqlSugar - - // For now, we'll just keep it empty or add methods as needed. - // The primary goal is to abstract away ITransaction. - // The ITransaction already handles transactions. - } -} \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IDeviceRepository.cs b/DMS.Infrastructure/Interfaces/IDeviceRepository.cs index 76ea551..dea19c0 100644 --- a/DMS.Infrastructure/Interfaces/IDeviceRepository.cs +++ b/DMS.Infrastructure/Interfaces/IDeviceRepository.cs @@ -1,4 +1,5 @@ using DMS.Core.Models; +using SqlSugar; using System.Collections.Generic; using System.Threading.Tasks; @@ -11,5 +12,8 @@ namespace DMS.Infrastructure.Interfaces Task DeleteAsync(DbDevice model); Task> GetAllAsync(); Task GetByIdAsync(int id); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IMenuRepository.cs b/DMS.Infrastructure/Interfaces/IMenuRepository.cs index 93a8f36..7b1c07a 100644 --- a/DMS.Infrastructure/Interfaces/IMenuRepository.cs +++ b/DMS.Infrastructure/Interfaces/IMenuRepository.cs @@ -2,21 +2,18 @@ using DMS.Core.Models; using DMS.Core.Enums; using System.Collections.Generic; using System.Threading.Tasks; +using DMS.Infrastructure.Entities; namespace DMS.Infrastructure.Interfaces { public interface IMenuRepository { - Task DeleteAsync(MenuBean menu); - Task DeleteAsync(MenuBean menu, ITransaction db); - Task> GetMenuTreesAsync(); - Task AddAsync(MenuBean menu); - Task AddAsync(MenuBean menu, ITransaction db); - Task AddVarTableMenuAsync(Device dbDevice, int parentMenuId, ITransaction db); - Task AddAsync(Device device, ITransaction db); - Task UpdateAsync(MenuBean menu); - - Task GetMenuByDataIdAsync(int dataId, MenuType menuType); - Task GetMainMenuByNameAsync(string name); + Task DeleteAsync(DbMenu menu); + Task> GetMenuTreesAsync(); + Task AddAsync(DbMenu menu); + Task UpdateAsync(DbMenu menu); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IMqttRepository.cs b/DMS.Infrastructure/Interfaces/IMqttRepository.cs index 43f2c0e..5eaeca5 100644 --- a/DMS.Infrastructure/Interfaces/IMqttRepository.cs +++ b/DMS.Infrastructure/Interfaces/IMqttRepository.cs @@ -1,4 +1,5 @@ using DMS.Core.Models; +using DMS.Infrastructure.Entities; using System.Collections.Generic; using System.Threading.Tasks; @@ -6,10 +7,13 @@ namespace DMS.Infrastructure.Interfaces { public interface IMqttRepository { - Task GetByIdAsync(int id); - Task> GetAllAsync(); - Task AddAsync(Mqtt mqtt); - Task UpdateAsync(Mqtt mqtt); - Task DeleteAsync(Mqtt mqtt); + Task GetByIdAsync(int id); + Task> GetAllAsync(); + Task AddAsync(DbMqtt mqtt); + Task UpdateAsync(DbMqtt mqtt); + Task DeleteAsync(DbMqtt mqtt); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/ITransaction.cs b/DMS.Infrastructure/Interfaces/ITransaction.cs deleted file mode 100644 index d05a91c..0000000 --- a/DMS.Infrastructure/Interfaces/ITransaction.cs +++ /dev/null @@ -1,17 +0,0 @@ -using SqlSugar; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DMS.Infrastructure.Interfaces -{ - public interface ITransaction - { - Task BeginTranAsync(); - Task CommitTranAsync(); - SqlSugarClient GetInstance(); - Task RollbackTranAsync(); - } -} diff --git a/DMS.Infrastructure/Interfaces/IUserRepository.cs b/DMS.Infrastructure/Interfaces/IUserRepository.cs index e39dd16..361f5eb 100644 --- a/DMS.Infrastructure/Interfaces/IUserRepository.cs +++ b/DMS.Infrastructure/Interfaces/IUserRepository.cs @@ -11,5 +11,8 @@ namespace DMS.Infrastructure.Interfaces Task AddAsync(User user); Task UpdateAsync(User user); Task DeleteAsync(int id); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IVarDataRepository.cs b/DMS.Infrastructure/Interfaces/IVarDataRepository.cs index 585732e..356826b 100644 --- a/DMS.Infrastructure/Interfaces/IVarDataRepository.cs +++ b/DMS.Infrastructure/Interfaces/IVarDataRepository.cs @@ -7,23 +7,12 @@ namespace DMS.Infrastructure.Interfaces public interface IVarDataRepository { Task GetByIdAsync(int id); - Task GetByIdAsync(int id, ITransaction db); Task> GetAllAsync(); - Task> GetAllAsync(ITransaction db); - Task> GetByVariableTableIdAsync(int varTableId); - Task> GetByVariableTableIdAsync(int varTableId, ITransaction db); Task AddAsync(Variable variable); - Task AddAsync(Variable variable, ITransaction db); - Task AddAsync(IEnumerable variableDatas); - Task AddAsync(IEnumerable variableDatas, ITransaction db); Task UpdateAsync(Variable variable); - Task UpdateAsync(Variable variable, ITransaction db); - Task UpdateAsync(List variableDatas); - Task UpdateAsync(List variableDatas, ITransaction db); Task DeleteAsync(Variable variable); - Task DeleteAsync(Variable variable, ITransaction db); - Task DeleteAsync(IEnumerable variableDatas); - - Task AddMqttToVariablesAsync(IEnumerable variableMqttList); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IVarTableRepository.cs b/DMS.Infrastructure/Interfaces/IVarTableRepository.cs index c81cc65..24cc164 100644 --- a/DMS.Infrastructure/Interfaces/IVarTableRepository.cs +++ b/DMS.Infrastructure/Interfaces/IVarTableRepository.cs @@ -12,6 +12,9 @@ namespace DMS.Infrastructure.Interfaces Task DeleteAsync(DbVariableTable variableTable); Task> GetAllAsync(); Task GetByIdAsync(int id); + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/IVariableMqttAliasRepository.cs b/DMS.Infrastructure/Interfaces/IVariableMqttAliasRepository.cs index c972b23..7635144 100644 --- a/DMS.Infrastructure/Interfaces/IVariableMqttAliasRepository.cs +++ b/DMS.Infrastructure/Interfaces/IVariableMqttAliasRepository.cs @@ -6,13 +6,12 @@ namespace DMS.Infrastructure.Interfaces { public interface IVariableMqttAliasRepository { - Task GetAliasByVariableAndMqtt(int variableDataId, int mqttId); - Task GetAliasByVariableAndMqtt(int variableDataId, int mqttId, ITransaction db); - Task AddManyAsync(IEnumerable entities); - Task AddManyAsync(IEnumerable entities, ITransaction db); + Task GetByIdAsync(int variableDataId, int mqttId); Task UpdateAliasAsync(int variableDataId, int mqttId, string newAlias); - Task UpdateAliasAsync(int variableDataId, int mqttId, string newAlias, ITransaction db); Task DeleteAsync(int variableDataId, int mqttId); - + Task BeginTranAsync(); + Task CommitTranAsync(); + Task RollbackTranAsync(); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index 1036c4d..7b93145 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -15,26 +15,21 @@ namespace DMS.Infrastructure.Repositories; public abstract class BaseRepository where TEntity : class, new() { - private readonly ITransaction _transaction; + private readonly SqlSugarDbContext _dbContext; /// /// 获取当前事务的 SqlSugarClient 实例,用于数据库操作。 /// - protected SqlSugarClient Db => _transaction.GetInstance(); + protected SqlSugarClient Db => _dbContext.GetInstance(); - /// - /// 获取到当前的事务 - /// - /// - public ITransaction GetTransaction() => _transaction; /// /// 初始化 BaseRepository 的新实例。 /// - /// 事务管理对象,通过依赖注入提供。 - protected BaseRepository(ITransaction transaction) + /// 事务管理对象,通过依赖注入提供。 + protected BaseRepository(SqlSugarDbContext dbContext) { - this._transaction = transaction; + this._dbContext = dbContext; } /// @@ -95,6 +90,7 @@ public abstract class BaseRepository NlogHelper.Info($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entities; } + /// /// 异步根据主键 ID 获取单个实体。 @@ -125,4 +121,36 @@ public abstract class BaseRepository NlogHelper.Info($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); return entity; } + + /// + /// 异步判断是否存在满足条件的实体。 + /// + /// 查询条件的 Lambda 表达式。 + /// 如果存在则返回 true,否则返回 false。 + public virtual async Task ExistsAsync(Expression> expression) + { + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + var result = await Db.Queryable().AnyAsync(expression); + stopwatch.Stop(); + NlogHelper.Info($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); + return result; + } + + public async Task BeginTranAsync() + { + await Db.BeginTranAsync(); + } + + public async Task CommitTranAsync() + { + await Db.CommitTranAsync(); + } + + + + public async Task RollbackTranAsync() + { + await Db.RollbackTranAsync(); + } } diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index 9ca8179..258d2df 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -13,8 +13,8 @@ namespace DMS.Infrastructure.Repositories; public class DeviceRepository : BaseRepository,IDeviceRepository { - public DeviceRepository(ITransaction transaction) - : base(transaction) + public DeviceRepository(SqlSugarDbContext dbContext) + : base(dbContext) { } diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs index b09e6d6..66486fd 100644 --- a/DMS.Infrastructure/Repositories/MenuRepository.cs +++ b/DMS.Infrastructure/Repositories/MenuRepository.cs @@ -10,27 +10,19 @@ using DMS.Infrastructure.Interfaces; namespace DMS.Infrastructure.Repositories; -public class MenuRepository : BaseRepository +public class MenuRepository : BaseRepository,IMenuRepository { public MenuRepository(SqlSugarDbContext dbContext) : base(dbContext) { } - public override async Task DeleteAsync(DbMenu menu) - { - return await base.DeleteAsync(menu); - } - - - public async Task> GetMenuTreesAsync() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var dbMenuTree = await Db.Queryable() .ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0); - stopwatch.Stop(); NlogHelper.Info($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); return dbMenuTree; @@ -54,9 +46,4 @@ public class MenuRepository : BaseRepository return result; } - public async Task GetMainMenuByNameAsync(string name) - { - var dbMenu= await Db.Queryable().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu); - return dbMenu; - } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VarDataRepository.cs b/DMS.Infrastructure/Repositories/VarDataRepository.cs index f53e1e7..5c959a7 100644 --- a/DMS.Infrastructure/Repositories/VarDataRepository.cs +++ b/DMS.Infrastructure/Repositories/VarDataRepository.cs @@ -49,9 +49,9 @@ public class VarDataRepository : BaseRepository - // public VarDataRepository(IMapper mapper) + // public VarDataRepository(IMapper _mapper) // { - // _mapper = mapper; + // _mapper = _mapper; // } /* diff --git a/DMS.Infrastructure/Services/BaseService.cs b/DMS.Infrastructure/Services/BaseService.cs new file mode 100644 index 0000000..4f24274 --- /dev/null +++ b/DMS.Infrastructure/Services/BaseService.cs @@ -0,0 +1,64 @@ +using AutoMapper; +using DMS.Infrastructure.Repositories; +using System.Threading.Tasks; + +namespace DMS.Infrastructure.Services +{ + /// + /// 通用服务基类,封装了常见的增、删、改操作。 + /// + /// 业务逻辑模型类型。 + /// 数据库实体类型。 + /// 与实体对应的仓储类型。 + public abstract class BaseService + where TEntity : class, new() + where TRepository : BaseRepository + { + protected readonly IMapper _mapper; + protected readonly TRepository _repository; + + /// + /// 初始化 BaseService 的新实例。 + /// + /// AutoMapper 实例,用于对象映射。 + /// 仓储实例,用于数据访问。 + protected BaseService(IMapper mapper, TRepository repository) + { + _mapper = mapper; + _repository = repository; + } + + /// + /// 异步添加一个新的业务模型对象。 + /// + /// 要添加的业务模型对象。 + /// 返回添加后的数据库实体。 + public virtual async Task AddAsync(TModel model) + { + var entity = _mapper.Map(model); + return await _repository.AddAsync(entity); + } + + /// + /// 异步更新一个现有的业务模型对象。 + /// + /// 要更新的业务模型对象。 + /// 返回受影响的行数。 + public virtual async Task UpdateAsync(TModel model) + { + var entity = _mapper.Map(model); + return await _repository.UpdateAsync(entity); + } + + /// + /// 异步删除一个业务模型对象。 + /// + /// 要删除的业务模型对象。 + /// 返回受影响的行数。 + public virtual async Task DeleteAsync(TModel model) + { + var entity = _mapper.Map(model); + return await _repository.DeleteAsync(entity); + } + } +} diff --git a/DMS.Infrastructure/Services/DatabaseInitializerService.cs b/DMS.Infrastructure/Services/DatabaseInitializerService.cs index a8fffe9..6fa7f82 100644 --- a/DMS.Infrastructure/Services/DatabaseInitializerService.cs +++ b/DMS.Infrastructure/Services/DatabaseInitializerService.cs @@ -1,9 +1,11 @@ using DMS.Config; using DMS.Core.Enums; +using DMS.Core.Models; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using SqlSugar; using System; +using System.Linq; using System.Threading.Tasks; namespace DMS.Infrastructure.Services @@ -31,52 +33,24 @@ namespace DMS.Infrastructure.Services _db.CodeFirst.InitTables(); } - public async Task InitializeMenu() + public Task InitializeMenu() { - var homeMenu = new DbMenu() - { Name = "主页", Type = MenuType.MainMenu, Icon = "Home", ParentId = 0 }; // Icon needs to be adjusted if it's not a string - - var deviceMenu = new DbMenu() + var settings = AppSettings.Load(); + if (settings.Menus.Any()) { - Name = "设备", Type = MenuType.MainMenu, Icon = "Devices3", - ParentId = 0 - }; - var dataTransfromMenu = new DbMenu() - { - Name = "数据转换", Type = MenuType.MainMenu, - Icon = "ChromeSwitch", ParentId = 0 - }; - var mqttMenu = new DbMenu() - { - Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = "Cloud", - ParentId = 0 - }; - - var settingMenu = new DbMenu() - { - Name = "设置", Type = MenuType.MainMenu, Icon = "Settings", - ParentId = 0 - }; - var aboutMenu = new DbMenu() - { Name = "关于", Type = MenuType.MainMenu, Icon = "Info", ParentId = 0 }; - - await CheckMainMenuExist(homeMenu); - await CheckMainMenuExist(deviceMenu); - await CheckMainMenuExist(dataTransfromMenu); - await CheckMainMenuExist(mqttMenu); - await CheckMainMenuExist(settingMenu); - await CheckMainMenuExist(aboutMenu); - } - - private async Task CheckMainMenuExist(DbMenu menu) - { - var homeMenuExist = await _db.Queryable() - .FirstAsync(dm => dm.Name == menu.Name); - if (homeMenuExist == null) - { - await _db.Insertable(menu) - .ExecuteCommandAsync(); + return Task.CompletedTask; } + + settings.Menus.Add(new MenuBean() { Id=1, Name = "主页", Type = MenuType.MainMenu, Icon = "Home", ParentId = 0 }); + settings.Menus.Add(new MenuBean() { Id = 2, Name = "设备", Type = MenuType.MainMenu, Icon = "Devices3", ParentId = 0 }); + settings.Menus.Add(new MenuBean() { Id = 3, Name = "数据转换", Type = MenuType.MainMenu, Icon = "ChromeSwitch", ParentId = 0 }); + settings.Menus.Add(new MenuBean() { Id = 4, Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = "Cloud", ParentId = 0 }); + settings.Menus.Add(new MenuBean() { Id = 5, Name = "设置", Type = MenuType.MainMenu, Icon = "Settings", ParentId = 0 }); + settings.Menus.Add(new MenuBean() { Id = 6, Name = "关于", Type = MenuType.MainMenu, Icon = "Info", ParentId = 0 }); + + settings.Save(); + + return Task.CompletedTask; } } -} \ No newline at end of file +} diff --git a/DMS.Infrastructure/Services/DeviceService.cs b/DMS.Infrastructure/Services/DeviceService.cs index 8f8755d..dc1c4b5 100644 --- a/DMS.Infrastructure/Services/DeviceService.cs +++ b/DMS.Infrastructure/Services/DeviceService.cs @@ -15,86 +15,68 @@ using System.Collections.Concurrent; namespace DMS.Infrastructure.Services { - public class DeviceService : IDeviceService + public class DeviceService :BaseService { - private readonly DeviceRepository _deviceRepository; + private readonly IDeviceRepository _deviceRepository; private readonly IMenuRepository _menuRepository; private readonly IVarTableRepository _varTableRepository; private readonly IMapper _mapper; - private ConcurrentDictionary _devicesDic; - public DeviceService(DeviceRepository deviceRepository, IMenuRepository menuRepository, IVarTableRepository varTableRepository, IMapper mapper, SqlSugarDbContext dbContext) + public DeviceService(IMapper mapper, DeviceRepository repository) : base(mapper, repository) { - _deviceRepository = deviceRepository; - _menuRepository = menuRepository; - _varTableRepository = varTableRepository; + _deviceRepository = repository; _mapper = mapper; - _devicesDic = new ConcurrentDictionary(); } public async Task> GetAllAsync() { var dbDevices = await _deviceRepository.GetAllAsync(); - var deviceDic = _mapper.Map>(dbDevices).ToDictionary(d => d.Id); - _devicesDic = new ConcurrentDictionary(deviceDic); - - return deviceDic.Values.ToList(); + return _mapper.Map>(dbDevices); } public async Task AddAsync(Device device) { Device resDevice = null; - try + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + var dbList = await GetAllAsync(); + //查询设备的名字是否存在 + if (dbList.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl)) { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - - //查询设备的名字是否存在 - if (_devicesDic.Values.Any(d => d.Name == device.Name || (d.Ip == device.Ip && d.Prot == device.Prot) || d.OpcUaEndpointUrl == device.OpcUaEndpointUrl)) - { - NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。"); - return resDevice; - } - await _deviceRepository.GetTransaction().BeginTranAsync(); - // 2. 将设备添加到数据库 - var addDevice = await _deviceRepository.AddAsync(_mapper.Map(device)); - - //判断判断是否添加默认变量表 - if (device.IsAddDefVarTable) - { - DbVariableTable dbVariableTable = new DbVariableTable(); - dbVariableTable.Name = "默认变量表"; - dbVariableTable.Description = "默认变量表"; - dbVariableTable.DeviceId = addDevice.Id; - dbVariableTable.ProtocolType = addDevice.ProtocolType; - var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable); - if (addDevice.VariableTables==null) - { - addDevice.VariableTables= new List(); - } - - addDevice.VariableTables.Add(dbAddVarTable); - } - - // 4. 为新设备添加菜单 - //var addDeviceMenuId = await _menuRepository.AddAsync(addDevice); - resDevice = _mapper.Map(addDevice); - - await _deviceRepository.GetTransaction().CommitTranAsync(); - - stopwatch.Stop(); - NlogHelper.Info($"添加设备 '{device.Name}' 及相关菜单耗时:{stopwatch.ElapsedMilliseconds}ms"); + NlogHelper.Warn("设备的名称,Ip:端口,OpcUrl,不可以重复。"); return resDevice; } - catch (Exception e) - { - await _deviceRepository.GetTransaction().RollbackTranAsync(); - throw; - - } + // 2. 将设备添加到数据库 + var addDevice = await _deviceRepository.AddAsync(_mapper.Map(device)); + + //判断判断是否添加默认变量表 + //if (device.IsAddDefVarTable) + //{ + // DbVariableTable dbVariableTable = new DbVariableTable(); + // dbVariableTable.Name = "默认变量表"; + // dbVariableTable.Description = "默认变量表"; + // dbVariableTable.DeviceId = addDevice.Id; + // dbVariableTable.ProtocolType = addDevice.ProtocolType; + // var dbAddVarTable= await _varTableRepository.AddAsync(dbVariableTable); + // if (addDevice.VariableTables==null) + // { + // addDevice.VariableTables= new List(); + // } + + // addDevice.VariableTables.Add(dbAddVarTable); + //} + + // 4. 为新设备添加菜单 + //var addDeviceMenuId = await _menuRepository.AddAsync(addDevice); + resDevice = _mapper.Map(addDevice); + + + stopwatch.Stop(); + NlogHelper.Info($"添加设备 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); + return resDevice; } } diff --git a/DMS.Infrastructure/Services/MenuService.cs b/DMS.Infrastructure/Services/MenuService.cs index 2e8dc1a..37cf9c9 100644 --- a/DMS.Infrastructure/Services/MenuService.cs +++ b/DMS.Infrastructure/Services/MenuService.cs @@ -1,102 +1,14 @@ using AutoMapper; -using DMS.Core.Enums; -using DMS.Core.Helper; using DMS.Core.Models; -using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; -using SqlSugar; -using System.Diagnostics; -using System.Collections.Generic; -using System; -using System.Threading.Tasks; +using DMS.Infrastructure.Repositories; namespace DMS.Infrastructure.Services { - public class MenuService + public class MenuService : BaseService { - private readonly IMapper _mapper; - private readonly SqlSugarDbContext _dbContext; - private SqlSugarClient Db => _dbContext.GetInstance(); - - public MenuService(IMapper mapper, SqlSugarDbContext dbContext) + public MenuService(IMapper mapper, MenuRepository repository) : base(mapper, repository) { - _mapper = mapper; - _dbContext = dbContext; - } - - public async Task DeleteAsync(MenuBean menu, SqlSugarClient db) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var childList = await db.Queryable() - .ToChildListAsync(it => it.ParentId, menu.Id); - var result = await db.Deleteable(childList) - .ExecuteCommandAsync(); - stopwatch.Stop(); - NlogHelper.Info($"删除菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; - } - - public async Task AddAsync(MenuBean menu, SqlSugarClient db) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var result = await db.Insertable(_mapper.Map(menu)) - .ExecuteCommandAsync(); - stopwatch.Stop(); - NlogHelper.Info($"添加菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; - } - - public async Task AddVarTableMenuAsync(DbDevice dbDevice, int parentMenuId, SqlSugarClient db) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var addVarTable = new MenuBean() - { - Name = "添加变量表", - Type = MenuType.AddVariableTableMenu, - ParentId = parentMenuId, - DataId = dbDevice.Id - }; - var addTableRes = await db.Insertable(addVarTable) - .ExecuteCommandAsync(); - stopwatch.Stop(); - return addTableRes; - } - - public async Task AddAsync(DbDevice device, SqlSugarClient db) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var deviceMainMenu = await db.Queryable() - .FirstAsync(m => m.Name == "设备"); - if (deviceMainMenu == null) - throw new InvalidOperationException("没有找到设备菜单!!"); - - MenuBean menu = new MenuBean() - { - Name = device.Name, - Type = MenuType.DeviceMenu, - DataId = device.Id, - }; - menu.ParentId = deviceMainMenu.Id; - var addDeviceMenuId = await db.Insertable(_mapper.Map(menu)) - .ExecuteReturnIdentityAsync(); - stopwatch.Stop(); - NlogHelper.Info($"添加设备菜单 '{device.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return addDeviceMenuId; - } - - public async Task UpdateAsync(MenuBean menu, SqlSugarClient db) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - var result = await db.Updateable(_mapper.Map(menu)) - .ExecuteCommandAsync(); - stopwatch.Stop(); - NlogHelper.Info($"编辑菜单 '{menu.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; } } } diff --git a/DMS.Infrastructure/Services/MqttService.cs b/DMS.Infrastructure/Services/MqttService.cs index 08b810b..2b98579 100644 --- a/DMS.Infrastructure/Services/MqttService.cs +++ b/DMS.Infrastructure/Services/MqttService.cs @@ -4,6 +4,7 @@ using DMS.Core.Helper; using DMS.Core.Models; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; +using DMS.Infrastructure.Interfaces; using DMS.Infrastructure.Repositories; using SqlSugar; using System; @@ -13,112 +14,10 @@ using System.Threading.Tasks; namespace DMS.Infrastructure.Services { - public class MqttService + public class MqttService:BaseService { - private readonly MqttRepository _mqttRepository; - private readonly MenuRepository _menuRepository; - private readonly IMapper _mapper; - private readonly SqlSugarDbContext _dbContext; - private SqlSugarClient Db => _dbContext.GetInstance(); - - public MqttService(MqttRepository mqttRepository, MenuRepository menuRepository, IMapper mapper, SqlSugarDbContext dbContext) + public MqttService(IMapper mapper, MqttRepository repository) : base(mapper, repository) { - _mqttRepository = mqttRepository; - _menuRepository = menuRepository; - _mapper = mapper; - _dbContext = dbContext; - } - - public async Task AddAsync(Mqtt mqtt) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - await Db.BeginTranAsync(); - try - { - var result = await Db.Insertable(_mapper.Map(mqtt)) - .ExecuteReturnIdentityAsync(); - var mqttMenu = await _menuRepository.GetMainMenuByNameAsync("Mqtt服务器"); - // AddAsync menu entry - var menu = new MenuBean() - { - Name = mqtt.Name, - Type = MenuType.MqttMenu, - DataId = result, - ParentId = mqttMenu.Id, - }; - await _menuRepository.AddAsync(_mapper.Map(menu)); - await Db.CommitTranAsync(); - stopwatch.Stop(); - NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; - } - catch (Exception ex) - { - await Db.RollbackTranAsync(); - NlogHelper.Error($"添加MQTT配置 {mqtt.Name} 失败", ex); - throw; - } - } - - public async Task UpdateAsync(Mqtt mqtt) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - await Db.BeginTranAsync(); - try - { - var result = await Db.Updateable(_mapper.Map(mqtt)) - .ExecuteCommandAsync(); - // Update menu entry - var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu); - if (menu != null) - { - menu.Name = mqtt.Name; - await _menuRepository.UpdateAsync(_mapper.Map(menu)); - } - - await Db.CommitTranAsync(); - stopwatch.Stop(); - NlogHelper.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; - } - catch (Exception ex) - { - await Db.RollbackTranAsync(); - NlogHelper.Error($"更新MQTT配置 {mqtt.Name} 失败", ex); - throw; - } - } - - public async Task DeleteAsync(Mqtt mqtt) - { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - await Db.BeginTranAsync(); - try - { - var result = await Db.Deleteable() - .In(mqtt.Id) - .ExecuteCommandAsync(); - // DeleteAsync menu entry - var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu); - if (menu != null) - { - await _menuRepository.DeleteAsync(_mapper.Map(menu)); - } - - await Db.CommitTranAsync(); - stopwatch.Stop(); - NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result; - } - catch (Exception ex) - { - await Db.RollbackTranAsync(); - NlogHelper.Error($"删除MQTT配置 {mqtt.Name} 失败", ex); - throw; - } } } } diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index f2bc58e..4a80e28 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -22,6 +22,8 @@ using DMS.WPF.Services.Processors; using DMS.WPF.ViewModels.DMS.WPF.ViewModels; using SqlSugar; using LogLevel = Microsoft.Extensions.Logging.LogLevel; +using DMS.Infrastructure.Services; +using DMS.Infrastructure.Interfaces; namespace DMS; @@ -31,6 +33,8 @@ namespace DMS; public partial class App : System.Windows.Application { public IServiceProvider Services { get; } + public AppSettings Settings { get; private set; } + public App() { @@ -59,11 +63,10 @@ public partial class App : System.Windows.Application try { - var databaseInitializer = Host.Services.GetRequiredService(); + var databaseInitializer = Host.Services.GetRequiredService(); databaseInitializer.InitializeDataBase(); - await databaseInitializer.InitializeMenu() - .Await((e) => { NotificationHelper.ShowError($"初始化主菜单失败:{e.Message}", e); }, - () => { MessageHelper.SendLoadMessage(LoadTypes.Menu); }); + await databaseInitializer.InitializeMenu(); + Settings = AppSettings.Load(); Host.Services.GetRequiredService(); // 初始化数据处理链 @@ -83,7 +86,7 @@ public partial class App : System.Windows.Application MainWindow.Show(); // 根据配置启动服务 - // var connectionSettings = DMS.Config.ConnectionSettings.Load(); + // var connectionSettings = DMS.Config.AppSettings.Load(); // if (connectionSettings.EnableMqttService) // { // Host.Services.GetRequiredService().StartService(); @@ -105,18 +108,8 @@ public partial class App : System.Windows.Application private void ConfigureServices(IServiceCollection services) { - // Register ConnectionSettings - services.AddSingleton(ConnectionSettings.Load()); + services.AddTransient(); - // Register SqlSugarDbContext (concrete type, used by DatabaseInitializerService) - // SqlSugarDbContext now internally creates SqlSugarClient using ConnectionSettings - services.AddScoped(); - - // Register ITransaction (abstract interface for transaction management) - //services.AddScoped(); - - //// Register IDatabaseService (abstract interface for database initialization) - //services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/DMS.WPF/Helper/ThemeHelper.cs b/DMS.WPF/Helper/ThemeHelper.cs index 30a3878..e1fde4c 100644 --- a/DMS.WPF/Helper/ThemeHelper.cs +++ b/DMS.WPF/Helper/ThemeHelper.cs @@ -2,59 +2,78 @@ using System; using System.Linq; using System.Windows; -using DMS.Infrastructure.Configurations; -using iNKORE.UI.WPF.Modern; -using Microsoft.Win32; +using iNKORE.UI.WPF.Modern; // 导入 iNKORE UI 库,用于现代WPF控件和主题 +using Microsoft.Win32; // 导入用于访问Windows注册表的类 namespace DMS.WPF.Helper; +/// +/// 主题管理帮助类,用于应用和初始化应用程序的主题。 +/// public static class ThemeHelper { + /// + /// 应用指定的主题。 + /// + /// 要应用的主题名称("跟随系统", "浅色", "深色")。 public static void ApplyTheme(string themeName) { - ApplicationTheme theme; + ApplicationTheme theme; // 定义一个变量来存储最终的主题 + // 判断是否设置为"跟随系统" if (themeName == "跟随系统") { + // 如果是,则根据当前系统主题设置应用主题 theme = IsSystemInDarkTheme() ? ApplicationTheme.Dark : ApplicationTheme.Light; } else { + // 否则,根据传入的主题名称进行匹配 theme = themeName switch { - "浅色" => ApplicationTheme.Light, - "深色" => ApplicationTheme.Dark, - _ => IsSystemInDarkTheme() ? ApplicationTheme.Dark : ApplicationTheme.Light + "浅色" => ApplicationTheme.Light, // 设置为浅色主题 + "深色" => ApplicationTheme.Dark, // 设置为深色主题 + _ => IsSystemInDarkTheme() ? ApplicationTheme.Dark : ApplicationTheme.Light // 默认情况,跟随系统 }; } - // Apply theme for iNKORE controls + // 为 iNKORE 控件应用主题 ThemeManager.Current.ApplicationTheme = theme; - // Apply theme for HandyControl + // 为 HandyControl 控件应用主题 UpdateHandyControlTheme(theme); } + /// + /// 初始化主题,在应用启动时调用。 + /// public static void InitializeTheme() { - var settings = ConnectionSettings.Load(); - ApplyTheme(settings.Theme); + // 从应用设置中读取保存的主题并应用 + ApplyTheme(App.Current.Settings.Theme); - // Listen for system theme changes + // 监听系统主题变化事件 SystemEvents.UserPreferenceChanged += (s, e) => { - if (e.Category == UserPreferenceCategory.General && ConnectionSettings.Load().Theme == "跟随系统") + // 当用户偏好设置中的"常规"类别发生变化,并且应用主题设置为"跟随系统"时 + if (e.Category == UserPreferenceCategory.General && App.Current.Settings.Theme == "跟随系统") { - Application.Current.Dispatcher.Invoke(() => { ApplyTheme("跟随系统"); }); + // 在UI线程上调用ApplyTheme来更新主题,以响应系统主题的变化 + App.Current.Dispatcher.Invoke(() => { ApplyTheme("跟随系统"); }); } }; } + /// + /// 更新 HandyControl 库的主题。 + /// + /// 要应用的主题 (浅色或深色)。 private static void UpdateHandyControlTheme(ApplicationTheme theme) { - var dictionaries = Application.Current.Resources.MergedDictionaries; + // 获取当前应用的资源字典集合 + var dictionaries = App.Current.Resources.MergedDictionaries; - // Find and remove the existing HandyControl skin dictionary + // 查找并移除现有的 HandyControl 皮肤资源字典 var existingSkin = dictionaries.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains("HandyControl;component/Themes/Skin")); if (existingSkin != null) @@ -62,15 +81,15 @@ public static class ThemeHelper dictionaries.Remove(existingSkin); } - // Determine the new skin URI + // 根据主题确定新的皮肤资源URI string skinUri = theme == ApplicationTheme.Dark - ? "pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml" - : "pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"; + ? "pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml" // 深色主题皮肤 + : "pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"; // 浅色主题皮肤 - // AddAsync the new skin dictionary + // 添加新的皮肤资源字典 dictionaries.Add(new ResourceDictionary { Source = new Uri(skinUri, UriKind.Absolute) }); - // To force refresh of dynamic resources, remove and re-add the main theme dictionary + // 为了强制刷新动态资源,先移除再重新添加主主题字典 var existingTheme = dictionaries.FirstOrDefault(d => d.Source != null && d.Source.OriginalString.Contains("HandyControl;component/Themes/Theme.xaml")); if (existingTheme != null) { @@ -79,18 +98,25 @@ public static class ThemeHelper } } + /// + /// 检查当前Windows系统是否处于深色模式。 + /// + /// 如果系统是深色模式,则返回 true;否则返回 false。 private static bool IsSystemInDarkTheme() { try { + // 定义注册表项路径和值名称 const string keyName = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"; const string valueName = "AppsUseLightTheme"; + // 读取注册表值,1表示浅色模式,0表示深色模式 var value = Registry.GetValue(keyName, valueName, 1); + // 如果值为0,则系统为深色模式 return value is 0; } catch { - // Default to light theme if registry access fails + // 如果访问注册表失败,则默认返回浅色主题 return false; } } diff --git a/DMS.WPF/ViewModels/SettingViewModel.cs b/DMS.WPF/ViewModels/SettingViewModel.cs index a6a9c16..12b87c0 100644 --- a/DMS.WPF/ViewModels/SettingViewModel.cs +++ b/DMS.WPF/ViewModels/SettingViewModel.cs @@ -5,35 +5,38 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DMS.Config; -using DMS.Data; -using DMS.Helper; using DMS.Services; +using DMS.WPF.Helper; +using DMS.Infrastructure.Interfaces; +using DMS.Helper; namespace DMS.WPF.ViewModels; public partial class SettingViewModel : ViewModelBase { - private ConnectionSettings _connectionSettings; + private readonly IDbContext transaction; + private AppSettings _settings; - public SettingViewModel() + public SettingViewModel(IDbContext transaction) { - _connectionSettings = ConnectionSettings.Load(); + _settings = AppSettings.Load(); AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList(); Themes = new List { "浅色", "深色", "跟随系统" }; + this.transaction = transaction; } public List Themes { get; } public string SelectedTheme { - get => _connectionSettings.Theme; + get => _settings.Theme; set { - if (_connectionSettings.Theme != value) + if (_settings.Theme != value) { - _connectionSettings.Theme = value; + _settings.Theme = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); ThemeHelper.ApplyTheme(value); } } @@ -43,98 +46,98 @@ public partial class SettingViewModel : ViewModelBase public string SelectedDbType { - get => _connectionSettings.DbType; + get => _settings.Database.DbType; set { - if (_connectionSettings.DbType != value) + if (_settings.Database.DbType != value) { - _connectionSettings.DbType = value; + _settings.Database.DbType = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public string Server { - get => _connectionSettings.Server; + get => _settings.Database.Server; set { - if (_connectionSettings.Server != value) + if (_settings.Database.Server != value) { - _connectionSettings.Server = value; + _settings.Database.Server = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public int Port { - get => _connectionSettings.Port; + get => _settings.Database.Port; set { - if (_connectionSettings.Port != value) + if (_settings.Database.Port != value) { - _connectionSettings.Port = value; + _settings.Database.Port = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public string UserId { - get => _connectionSettings.UserId; + get => _settings.Database.UserId; set { - if (_connectionSettings.UserId != value) + if (_settings.Database.UserId != value) { - _connectionSettings.UserId = value; + _settings.Database.UserId = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public string Password { - get => _connectionSettings.Password; + get => _settings.Database.Password; set { - if (_connectionSettings.Password != value) + if (_settings.Database.Password != value) { - _connectionSettings.Password = value; + _settings.Database.Password = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public string Database { - get => _connectionSettings.Database; + get => _settings.Database.Database; set { - if (_connectionSettings.Database != value) + if (_settings.Database.Database != value) { - _connectionSettings.Database = value; + _settings.Database.Database = value; OnPropertyChanged(); - _connectionSettings.Save(); + _settings.Save(); } } } public bool MinimizeToTrayOnClose { - get => _connectionSettings.MinimizeToTrayOnClose; + get => _settings.MinimizeToTrayOnClose; set { - if (_connectionSettings.MinimizeToTrayOnClose != value) + if (_settings.MinimizeToTrayOnClose != value) { - _connectionSettings.MinimizeToTrayOnClose = value; + _settings.MinimizeToTrayOnClose = value; OnPropertyChanged(nameof(MinimizeToTrayOnClose)); - _connectionSettings.Save(); + _settings.Save(); } } } @@ -144,7 +147,7 @@ public partial class SettingViewModel : ViewModelBase { try { - using (var db = DbContext.GetInstance()) + using (var db = transaction.GetInstance()) { await db.Ado.OpenAsync(); NotificationHelper.ShowSuccess("连接成功!"); diff --git a/DMS.WPF/Views/MainView.xaml.cs b/DMS.WPF/Views/MainView.xaml.cs index ecdae43..5397367 100644 --- a/DMS.WPF/Views/MainView.xaml.cs +++ b/DMS.WPF/Views/MainView.xaml.cs @@ -33,7 +33,7 @@ public partial class MainView : Window private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { - var settings = Config.ConnectionSettings.Load(); + var settings = Config.AppSettings.Load(); if (settings.MinimizeToTrayOnClose) { // Hide the window instead of closing it