nakiya
nakiya

Reputation: 14413

How to use Dtos with NHibernate loaded objects

I have a class hierarchy like below with which I load the data from DB through NHibernate. I use 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; }
}

I wrote my DTOs like so:

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

My problem now is how do I get to show object relationships in UI without referring to those objects by Id? I can think of ways to do it but my guess is that will require lot of coding. For example, I have to be able to assign an ItemTemplateDto to an ItemDto in the UI and assign multiple ItemDtos as children to an ItemDto. Is there a standard way people go about this?

Upvotes: 0

Views: 144

Answers (1)

Firo
Firo

Reputation: 30813

if you do not need to transfer the objects somehow then why not use the entities directly?

see Databindingfactory here http://msdn.microsoft.com/en-us/magazine/ee819139.aspx to automaticly implement INPC on the entities.

For assigning child items to an item maybe show some kind of tree and enable drag and drop one item under another which should effectivly add one item object to another items child collection.

Upvotes: 1

Related Questions