添加了将数据库对象列表转换为Model对象的列表
This commit is contained in:
@@ -2,6 +2,7 @@ using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Excptions;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
@@ -33,9 +34,18 @@ public class DevicesRepositories:BaseRepositories
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<DbDevice>> GetAll()
|
||||
public async Task<List<Device>> GetAll()
|
||||
{
|
||||
return await _db.Queryable<DbDevice>().Includes(d=>d.VariableTables).ToListAsync();
|
||||
var dlist= await _db.Queryable<DbDevice>().Includes(d=>d.VariableTables).ToListAsync();
|
||||
List<Device> devices=new List<Device>();
|
||||
foreach (DbDevice dbDevice in dlist)
|
||||
{
|
||||
Device device = dbDevice.NewTo<Device>();
|
||||
device.VariableTables=CovertHelper.ConvertList<DbVariableTable,VariableTable>(dbDevice.VariableTables);
|
||||
devices.Add(device);
|
||||
}
|
||||
|
||||
return devices;
|
||||
}
|
||||
public async Task<DbDevice> GetById(int id)
|
||||
{
|
||||
|
||||
33
Helper/CovertHelper.cs
Normal file
33
Helper/CovertHelper.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace PMSWPF.Helper;
|
||||
|
||||
public class CovertHelper
|
||||
{
|
||||
public static List<TTarget> ConvertList<TSource, TTarget>(List<TSource> sourceList)
|
||||
{
|
||||
List<TTarget> targetList = new List<TTarget>();
|
||||
Type sourceType = typeof(TSource);
|
||||
Type targetType = typeof(TTarget);
|
||||
|
||||
// 获取源类型和目标类型的公共属性
|
||||
PropertyInfo[] sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
PropertyInfo[] targetProperties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
foreach (TSource sourceObject in sourceList)
|
||||
{
|
||||
TTarget targetObject = Activator.CreateInstance<TTarget>();
|
||||
foreach (PropertyInfo targetProperty in targetProperties)
|
||||
{
|
||||
PropertyInfo sourceProperty = sourceProperties.FirstOrDefault(p => p.Name == targetProperty.Name && p.PropertyType == targetProperty.PropertyType);
|
||||
if (sourceProperty!= null)
|
||||
{
|
||||
object value = sourceProperty.GetValue(sourceObject);
|
||||
targetProperty.SetValue(targetObject, value);
|
||||
}
|
||||
}
|
||||
targetList.Add(targetObject);
|
||||
}
|
||||
return targetList;
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
private readonly DevicesRepositories _devicesRepositories;
|
||||
private readonly ILogger<DevicesViewModel> _logger;
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Device> _devices = new();
|
||||
private ObservableCollection<Device> _devices ;
|
||||
|
||||
public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,ILogger<DevicesViewModel> logger
|
||||
)
|
||||
@@ -40,25 +40,8 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
public async Task OnLoadedAsync()
|
||||
{
|
||||
var ds = await _devicesRepositories.GetAll();
|
||||
_devices.Clear();
|
||||
foreach (var dbDevice in ds)
|
||||
{
|
||||
Device device = new Device();
|
||||
dbDevice.CopyTo(device);
|
||||
foreach (var dbVariableTable in dbDevice.VariableTables)
|
||||
{
|
||||
|
||||
if (device.VariableTables == null)
|
||||
{
|
||||
device.VariableTables=new List<VariableTable>();
|
||||
}
|
||||
|
||||
var table = dbVariableTable.NewTo<VariableTable>();
|
||||
device.VariableTables.Add(table);
|
||||
}
|
||||
|
||||
_devices.Add(device);
|
||||
}
|
||||
Devices=new ObservableCollection<Device>(ds);
|
||||
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
Title="设备管理系统"
|
||||
Width="800"
|
||||
Height="600"
|
||||
Width="1080"
|
||||
Height="800"
|
||||
|
||||
ui:WindowHelper.UseModernWindowStyle="True"
|
||||
ui:WindowHelper.SystemBackdropType="Mica"
|
||||
d:DataContext="{d:DesignInstance vm:MainViewModel}"
|
||||
|
||||
Reference in New Issue
Block a user