Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EntityFramework: Context per Presenter or long conversation pattern for Windows Forms Applications?

For now I did not use MVP and I tried the long conversation pattern like this:

private void SaveItem(object sender, EventArgs e)
{
    using (var transaction = _businessTransactionFactory.Create())
    {
        var currentMobileDevice = GetCurrentMobileDevice();

        if (currentMobileDevice.Id == Guid.Empty)
        {
            transaction.MobileDeviceRepository.Save(currentMobileDevice);
        }
        else
        {
            transaction.MobileDeviceRepository.Update(currentMobileDevice);
        }

        transaction.Commit();
        LoadData(transaction);
    }
}

private MobileDevice GetCurrentMobileDevice()
{
    return (MobileDevice)MobileDevicesBindingNavigator.BindingSource.Current;
}

Problems I encountered:

  • I am always have to work with detached entities.
  • Working with detached entites forces EntityFramework to update ALL columns instead of those that changes:

    public void Update(T entity)
    {
        if (_objectContext.ObjectStateManager.GetObjectStateEntry(entity).State == EntityState.Detached)
        {
            _objectSet.Attach(entity);
            _objectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
        }
    }
    

How do you handle the context in a windows forms application?

What are pros and cons of both?

like image 952
Rookian Avatar asked Apr 09 '26 09:04

Rookian


1 Answers

Typical scenario is new context per presenter where presenter can handle more than one view in case of different views on the same data. There is very nice article about this in MSDN magazine. It targets NHibernate's session but EF's context management should follow the same approach to solve exactly the problems you are encountering with detached entities.

like image 139
Ladislav Mrnka Avatar answered Apr 10 '26 22:04

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!