如何在NHibernate加载的对象中使用Dtos

时间:2012-05-17 04:06:35

标签: c# wpf nhibernate .net-4.0

我有一个像下面这样的类层次结构,我通过NHibernate从DB加载数据。我使用Automapping。

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;
}

我现在的问题是如何在UI中显示对象关系而不通过Id引用这些对象?我可以想办法做到这一点,但我的猜测是需要大量的编码。例如,我必须能够在用户界面中为ItemTemplateDto分配ItemDto,并将多个ItemDto作为子项分配给ItemDto。人们有这样的标准方式吗?

1 个答案:

答案 0 :(得分:1)

如果您不需要以某种方式传输对象,那么为什么不直接使用实体?

请在此处查看Databindingfactory http://msdn.microsoft.com/en-us/magazine/ee819139.aspx以自动在实体上实施INPC。

为了将子项目分配给项目,可以显示某种树,并将一个项目拖放到另一个项目下,这样可以有效地将一个项目对象添加到另一个项目子项集合中。