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:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With