2

I would like to databind the foreign key property Product.CategoryId to a Devexpess Lookupedit in Windows Forms Application. So

        lookEditCategory.DataBindings
       .Add(new Binding("EditValue", Product, "CategoryId ", true,
        DataSourceUpdateMode.OnPropertyChanged));

        lookEditCategory.Properties.Columns.Clear();
        lookEditCategory.Properties.NullText = "";
        lookEditCategory.Properties.DataSource = CatCol;
        lookEditCategory.Properties.ValueMember = "CategoryId";
        lookEditCategory.Properties.DisplayMember = "CategoryName";
        var col = new LookUpColumnInfo("CategoryName") { Caption = "Type" };
        lookEditCategory.Properties.Columns.Add(col);

The problem is that Nhibernate does not expose the foreign key Product.CategoryId. Instead my entity and mapping are like this

public partial class Product
{
public virtual int ProductId { get; set; }
[NotNull]
[Length(Max=40)]
public virtual string ProductName { get; set; }
public virtual bool Discontinued { get; set; }
public virtual System.Nullable<int> SupplierId { get; set; }

[Length(Max=20)]
public virtual string QuantityPerUnit { get; set; }
public virtual System.Nullable<decimal> UnitPrice { get; set; }
public virtual System.Nullable<short> UnitsInStock { get; set; }
public virtual System.Nullable<short> UnitsOnOrder { get; set; }
public virtual System.Nullable<short> ReorderLevel { get; set; }

private IList<OrderDetail> _orderDetails = new List<OrderDetail>();

public virtual IList<OrderDetail> OrderDetails
{
  get { return _orderDetails; }
  set { _orderDetails = value; }
}

public virtual Category Category { get; set; }

public class ProductMap : FluentNHibernate.Mapping.ClassMap<Product>
{
  public ProductMap()
  {
    Table("`Products`");
    Id(x => x.ProductId, "`ProductID`")
      .GeneratedBy
        .Identity();
    Map(x => x.ProductName, "`ProductName`")
;
    Map(x => x.Discontinued, "`Discontinued`")
;
    Map(x => x.SupplierId, "`SupplierID`")
;

    Map(x => x.QuantityPerUnit, "`QuantityPerUnit`")
;
    Map(x => x.UnitPrice, "`UnitPrice`")
;
    Map(x => x.UnitsInStock, "`UnitsInStock`")
;
    Map(x => x.UnitsOnOrder, "`UnitsOnOrder`")
;
    Map(x => x.ReorderLevel, "`ReorderLevel`")
;
    HasMany(x => x.OrderDetails)
      .KeyColumn("`ProductID`")
      .AsBag()
      .Inverse()
      .Cascade.None()
;
    References(x => x.Category)
      .Column("`CategoryID`");
  }
}
}

I cannot add the property CategoryID in my Product entity and mapping because then it will be mapped twice. Is there any solution?

George Papadakis
  • 1,273
  • 13
  • 22

1 Answers1

0

Yes. Do NOT use your domain entities in the UI.
Sometimes your UI doesn't need (and shouldn't be aware of) all the properties of your domain objects.
Other times, it needs DTOs that contain data from different domain sources (for example- a list of CourseNames for the Student screen), or, like in your case- it needs the data to be represented in a slightly different way.
So the best way would be to create your DTOs with all (and only) the properties needed by the UI.
See this SO question for further details.

Community
  • 1
  • 1
J. Ed
  • 6,692
  • 4
  • 39
  • 55