The reference to Coding Instinct seems to cover how to update the database using disconnected objects okay. I have a slightly different problem with disconnected though. If I set lazy="false" and terminate the session, then put my object into a WinForms DataGridView then the DataGridView has a ghoulish tendancy to cause lazy load errors because it is looking into related objects that it best not look into. I would prefer a disconnected object to act like a POCO, not an nHibernate connected object.
Say I have a peice of code like this:-
ISession session = nHib_SessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();
CustomerBO customer = session.Get<CustomerBO>(CustomerID);
if (customer != null)
{
AddressBO defaultaddress = customer.DefaultAddress;
}
tx.Commit();
session.Close();
The line that goes "AddressBO defaultaddress = customer.DefaultAddress;" is there to ensure that the DefaultAddress is available to the disconnected customer object. However, if I put the DefaultAddress into a GridDataView (inside an IList<>) then the GridDataView will cause a lazy load error (even though lazy load is false) because it wants to explore the DefaultAddress.Customers property which represents the one-to-many join in the opposite direction.
I can solve this by putting another line in like this:-
IList<CustomerBO> defaultaddresscustomers = defaultaddress.Customers;
This means that that customer.DefaultAddress.Customers has results in it, even though I have no need for them. What this means is that for every object that I want to put into a DataGridView I have to return every object that is referenced by a property that that object contains, which could turn out to be a lot of data and a lot of waste, because I have no intention of using that data.
What is more, if someone on the client tinkers with these properties that I did not want to send to them and then I call session.SaveOrUpdateCopy I could end up with data being trashed that should never have been shown to the client. A bit of a security nightmare at the least.
Has anyone had any similar experiences, or have any better solution to this problem. Have they experienced this problem with any other data binding controls?