我有一个像下面这样的类层次结构,我通过 NHibernate 从 DB 加载数据。我使用自动映射。
public class Unit
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Value
{
public virtual int Id { get; set; }
public virtual double Quantity { get; private set; }
}
public class DirectQuantity : Value
{
}
public class DependentQuantity : Value
{
private double _getValue()
{
throw new NotImplementedException();
}
public override double Quantity
{
get
{
return _getValue();
}
}
}
public class MainCategory
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Notes { get; set; }
}
public class ItemTemplate
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual Unit Unit { get; set; }
public virtual MainCategory Category { get; set; }
public virtual List<ItemTemplate> Children { get; set; }
}
public class Item
{
public virtual int Id { get; set; }
public virtual string Note { get; set; }
public virtual Value Quantity { get; set; }
public virtual ItemTemplate Template { get; set; }
public virtual List<Item> Children { get; set; }
}
public class BSR
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual List<MainCategory> Categories { get; set; }
}
我这样写我的 DTO:
public class UnitDto : INotifyPropertyChanged
{
public int Id { get; set; }
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ValueDto : INotifyPropertyChanged
{
public int Id { get; set; }
public double Quantity
{
get { return _Quantity; }
set
{
if (_Quantity != value)
{
_Quantity = value;
OnPropertyChanged(QuantityPropertyName);
}
}
}
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
//throw new NotImplementedException();
}
private double _Quantity;
public const string QuantityPropertyName = "Quantity";
public event PropertyChangedEventHandler PropertyChanged;
}
public class DirectQuantityDto : ValueDto
{
}
public class DependentQuantityDto : ValueDto
{
}
public class MainCategoryDto : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
OnPropertyChanged(NamePropertyName);
}
}
}
private string _Name;
public const string NamePropertyName = "Name";
public string Notes
{
get { return _Notes; }
set
{
if (_Notes != value)
{
_Notes = value;
OnPropertyChanged(NotesPropertyName);
}
}
}
private string _Notes;
public const string NotesPropertyName = "Notes";
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
//throw new NotImplementedException();
}
}
public class ItemTemplateDto :INotifyPropertyChanged
{
public int Id { get; set; }
public string Description
{
get { return _Description; }
set
{
if (_Description != value)
{
_Description = value;
OnPropertyChanged(DescriptionPropertyName);
}
}
}
private string _Description;
public const string DescriptionPropertyName = "Description";
public int? UnitID
{
get { return _UnitID; }
set
{
if (_UnitID != value)
{
_UnitID = value;
OnPropertyChanged(UnitIDPropertyName);
}
}
}
private int? _UnitID;
public const string UnitIDPropertyName = "UnitID";
public int? MainCategoryId
{
get { return _MainCategoryId; }
set
{
if (_MainCategoryId != value)
{
_MainCategoryId = value;
OnPropertyChanged(MainCategoryIdPropertyName);
}
}
}
private int? _MainCategoryId;
public const string MainCategoryIdPropertyName = "MainCategoryId";
public ObservableCollection<int> ChildItemIds { get; set; }
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
//throw new NotImplementedException();
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ItemDto : INotifyPropertyChanged
{
public virtual int Id { get; set; }
public string Note
{
get { return _Note; }
set
{
if (_Note != value)
{
_Note = value;
OnPropertyChanged(NotePropertyName);
}
}
}
private string _Note;
public const string NotePropertyName = "Note";
public int? QuantityId
{
get { return _QuantityId; }
set
{
if (_QuantityId != value)
{
_QuantityId = value;
OnPropertyChanged(QuantityIdPropertyName);
}
}
}
private int? _QuantityId;
public const string QuantityIdPropertyName = "QuantityId";
public int? ItemTemplateId
{
get { return _ItemTemplateId; }
set
{
if (_ItemTemplateId != value)
{
_ItemTemplateId = value;
OnPropertyChanged(ItemTemplateIdPropertyName);
}
}
}
private int? _ItemTemplateId;
public const string ItemTemplateIdPropertyName = "ItemTemplateId";
public ObservableCollection<int> ChildItemIds { get; set; }
//public virtual string Note { get; set; }
//public virtual ValueDto Quantity { get; set; }
//public virtual ItemTemplateDto Template { get; set; }
//public virtual List<ItemDto> Children { get; set; }
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
//throw new NotImplementedException();
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class BSRDto : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
OnPropertyChanged(NamePropertyName);
}
}
}
private string _Name;
public const string NamePropertyName = "Name";
public string Description
{
get { return _Description; }
set
{
if (_Description != value)
{
_Description = value;
OnPropertyChanged(DescriptionPropertyName);
}
}
}
private string _Description;
public const string DescriptionPropertyName = "Description";
public ObservableCollection<int> CategoryIds { get; set; }
public List<MainCategoryDto> Categories { get; set; }
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
//throw new NotImplementedException();
}
public event PropertyChangedEventHandler PropertyChanged;
}
我现在的问题是如何在不通过 Id 引用这些对象的情况下在 UI 中显示对象关系?我可以想办法做到这一点,但我的猜测是这将需要大量的编码。例如,我必须能够在 UI 中将 an 分配给ItemTemplateDto
anItemDto
并将多个ItemDto
s 作为子项分配给ItemDto
. 人们有没有标准的方式来解决这个问题?