automapper框架优化

This commit is contained in:
hsc
2026-06-10 15:04:11 +08:00
parent 5452857299
commit 2e07c0c446
43 changed files with 612 additions and 293 deletions

View File

@@ -8,6 +8,10 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="16.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CAN驱动\CAN驱动.csproj" />
<ProjectReference Include="..\Command\Command.csproj" />

View File

@@ -20,6 +20,9 @@ using UIShare;
using System;
using DeviceCommand.Device;
using DeviceCommand.Base;
using AutoMapper;
using Microsoft.Extensions.Logging.Abstractions;
using ADP.Profiles;
namespace ADP
{
@@ -68,6 +71,22 @@ namespace ADP
RegionManager.SetRegionManager(Application.Current.MainWindow, re);
login.Show();
}
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
base.RegisterRequiredTypes(containerRegistry);
//注册全局变量
containerRegistry.RegisterScoped<SystemConfig>();
containerRegistry.RegisterScoped<StepRunning>();
containerRegistry.RegisterScoped<ScopedContext>();
containerRegistry.RegisterScoped<DeviceManager>();
containerRegistry.RegisterSingleton<GlobalInfo>();
//注册AutoMapper
var config = new MapperConfiguration(
cfg => cfg.AddProfile<AutoMapperProfile>(),
NullLoggerFactory.Instance
);
containerRegistry.RegisterSingleton<IMapper>(() => config.CreateMapper());
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册弹窗
@@ -75,12 +94,8 @@ namespace ADP
// 注册通知管理器
INotificationManager NotificationManager = new NotificationManager();
containerRegistry.RegisterInstance<INotificationManager>(NotificationManager);
//注册全局变量
containerRegistry.RegisterScoped<SystemConfig>();
containerRegistry.RegisterScoped<StepRunning>();
containerRegistry.RegisterScoped<ScopedContext>();
containerRegistry.RegisterScoped<DeviceManager>();
containerRegistry.RegisterSingleton<GlobalInfo>();
// 注册仓储
containerRegistry.RegisterScoped(typeof(SqlSugarRepository<>));
}
//指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中)
protected override IModuleCatalog CreateModuleCatalog()

View File

@@ -0,0 +1,64 @@
using AutoMapper;
using Model.Models;
using System;
using UIShare.UIViewModel;
namespace ADP.Profiles
{
/// <summary>
/// UIShare.UIViewModel ↔ Model.Models 双向映射配置。
/// </summary>
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
// ===== Parameter =====
// ParameterModel.Type(System.Type) ↔ Parameter.TypeName(string)
// ParameterModel.Category(enum) ↔ Parameter.Category(string)
CreateMap<ParameterVM, Parameter>()
.ForMember(dest => dest.TypeName,
opt => opt.MapFrom(src => src.Type != null ? src.Type.AssemblyQualifiedName : null))
.ForMember(dest => dest.Category,
opt => opt.MapFrom(src => src.Category.ToString()));
CreateMap<Parameter, ParameterVM>()
.ForMember(dest => dest.Type,
opt => opt.MapFrom(src => string.IsNullOrEmpty(src.TypeName)
? typeof(string)
: (Type.GetType(src.TypeName) ?? typeof(string))))
.ForMember(dest => dest.Category,
opt => opt.MapFrom(src => ParseCategory(src.Category)));
// ===== Step =====
CreateMap<StepVM, Step>().ReverseMap();
// ===== Program =====
CreateMap<ProgramVM, Program>().ReverseMap();
// ===== Method =====
CreateMap<MethodVM, Method>().ReverseMap();
// ===== DeviceInfo =====
CreateMap<DeviceInfoVM, DeviceInfo>().ReverseMap();
// ===== CanMessageShow =====
CreateMap<CanMessageShowVM, CanMessageShow>().ReverseMap();
// ===== InstructionNode =====
CreateMap<InstructionNodeVM, InstructionNode>().ReverseMap();
// ===== SubProgramItem =====
CreateMap<SubProgramItemVM,SubProgramItem>().ReverseMap();
}
private static ParameterVM.ParameterCategory ParseCategory(string? category)
{
if (string.IsNullOrEmpty(category))
return ParameterVM.ParameterCategory.Temp;
return Enum.TryParse<ParameterVM.ParameterCategory>(category, true, out var result)
? result
: ParameterVM.ParameterCategory.Temp;
}
}
}

View File

@@ -530,8 +530,8 @@ namespace ADP.ViewModels
// 读取 JSON 文件
string json = File.ReadAllText(filePath);
// 反序列化为 ProgramModel
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramModel>(json);
// 反序列化为 ProgramVM
var program = Newtonsoft.Json.JsonConvert.DeserializeObject<ProgramVM>(json);
if (program == null)
{
@@ -621,12 +621,12 @@ namespace ADP.ViewModels
LoggerHelper.SuccessWithNotify($"工位 [{runningScope}] 程序保存成功!");
}
// 💡 辅助方法:建议将你的通用序列化落盘代码调整为接收 ProgramModel 参数,提高复用性
private void SaveProgramToFile(string filePath, ProgramModel programModel)
// 💡 辅助方法:建议将你的通用序列化落盘代码调整为接收 ProgramVM 参数,提高复用性
private void SaveProgramToFile(string filePath, ProgramVM ProgramVM)
{
try
{
string json = Newtonsoft.Json.JsonConvert.SerializeObject(programModel, Newtonsoft.Json.Formatting.Indented);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(ProgramVM, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, json);
}
catch (Exception ex)