'Is it acceptable to bind Entity Framework entities to Window Forms controls?

I have two entities: the first is SalesOrder and the second is SalesOrderDetails. In the SalesOrder entity, I have an ObservableListSource list type that keeps track of SalesOrderDetails. The entities look something like this:

public class SalesOrder{

   public int Id {get; set;}

   public DateTime Date {get; set;}

   ...

   public virtual ObservableListSource<SalesOrderDetails> OrderDetails { get; set; } 

   publi SalesOrder()
   {
      OrderDetails =  new ObservableListSource<SalesOrderDetails>()
   }   
}

public class SalesOrderDetails{

   public int Id { get; set; }

   public int Quantity { get; get; }

   public decimal Price { get; set; }

   ...
}

ObservableListSource extends ObservableCollection and implements IListSource. The GetList method returns a bindingList that stays in sync with the ObservableCollection. The GetList method is an extension method defined in the System.Data.Entity assembly. ObservableListSource looks like this:

public class ObservableListSource<T> : ObservableCollection<T>, IListSource
           where T : class
 {
    private IBindingList _bindingList;

    bool IListSource.ContainsListCollection { get { return false; } }

    IList IListSource.GetList()
    {
          return _bindingList ?? (_bindingList = this.ToBindingList());
    }
}

To bind the SalesOrder and SalesOrderDetails entities to my form, I use two binding source controls: salesOrderBindingSource and salesOrderDetailsBindingSource. The binding looks like this:

salesOrderBindingSource.DataSource = SalesOrder;
salesOrderDetailsBindingSource.DataSource = salesOrderBindingSource;
salesOrderDetailsBindingSource.DataMember = OrderDetails;

I bind every entity that needs change tracking the same way I bind SalesOrder and SalesOrderDetails. I've been reading that it's not recommended to bind the entities to the UI, that I should use view models and bind those to the UI instead. But that means that I would have to write the change tracking code or find a library that does change tracking.

What do you think?



Solution 1:[1]

If it's acceptable for your application to bind to DataSet and DataTable, then it's OK to bind to EF models.

In general, it depends to the requirements of your application; For example, usually for a small-scale or data-centric forms application it makes sense to bind to DataTable or Entity models, but usually in a large-scale application, you may want to consider better patterns for separation of concern.

If databinding to EF models makes sense to your application, then to do it in a correct way, follow this Microsoft Docs article:

Some notes about the IBindingList, IListSource

  • The interface that you need in Windows Forms to support databinding to lists, is IBindingList. BindingList<T> is a good implementation of that.
  • IListSource provides functionality to an object to return a list that can be bound to a data source. So while the object doesn't implement IBindingList, but it can return a bindable list from its GetList method. It's very well supported and used in Windows Forms. For example, DataGridView, ComboBox, ListBox, BindingSource check if the data source implemented IListSource and in this case, they get the data source by calling GetList method. That's why DataTable supports databinding without implementing IBindingList, instead, it returns a DataView in GetList, which implements IBindingList.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1