Clarification of NHibernate ‘Update’ – used to ‘Attach’

Some of the wording on the NHibernate Session functions can be a tad misleading, or hard to understand.  One is ‘Update’, Update in many ways should be called ‘Attach’!  So, let’s review what Update does:

Session.Update

This sounds like something you call to ‘save’ something right?  Actually it’s not.  The ‘save’ occurs on Save/Commit/Flush.  ie.

using var tran = new Transaction
  Order order = Session.GetById(…)
  order.Updated = true;
  Session.Update(order)     <— not needed
tran.Commit()

Update is actually for ‘tracking’ – not saving.  Let’s say you have an object that is detached from a session – by calling Update on that object will tell NHibernate to start tracking the object again.

ie let’s say we get Order from the HttpSession…

Order order = Session[“Order”] as Order;
Session.Update(order)
tran.Commit()

So, in summary ‘Update’ is for re-associating an object back to the the NHibernate session.   Here is the catch as well – it is telling NHibernate that it is expecting it to be ‘saved’.  You can use Lock and define a LockMode to tell NHibernate that a ‘save’ might not be required.

In a test simulating a n-tier environment, I did detach (Evict) the object, then call ‘Update’ and ‘Lock’, it was able to save the object.  I plan on seeing how to work with optimistic concurrency.

What I find interesting is how badly Linq to Sql appears to handle this vs. NHibernate.  This is what I’m researching at the moment, so if anyone has advice in this area let me know.

Advertisements

Clarification of NHibernate ‘Update’ – used to ‘Attach’

Some of the wording on the NHibernate Session functions can be a tad misleading, or hard to understand.  One is ‘Update’, Update in many ways should be called ‘Attach’!  So, let’s review what Update does:

Session.Update

This sounds like something you call to ‘save’ something right?  Actually it’s not.  The ‘save’ occurs on Save/Commit/Flush.  ie.

using var tran = new Transaction
  Order order = Session.GetById(…)
  order.Updated = true;
  Session.Update(order)     <— not needed
tran.Commit()

Update is actually for ‘tracking’ – not saving.  Let’s say you have an object that is detached from a session – by calling Update on that object will tell NHibernate to start tracking the object again.

ie let’s say we get Order from the HttpSession…

Order order = Session[“Order”] as Order;
Session.Update(order)
tran.Commit()

So, in summary ‘Update’ is for re-associating an object back to the the NHibernate session.   Here is the catch as well – it is telling NHibernate that it is expecting it to be ‘saved’.  You can use Lock and define a LockMode to tell NHibernate that a ‘save’ might not be required.

In a test simulating a n-tier environment, I did detach (Evict) the object, then call ‘Update’ and ‘Lock’, it was able to save the object.  I plan on seeing how to work with optimistic concurrency.

What I find interesting is how badly Linq to Sql appears to handle this vs. NHibernate.  This is what I’m researching at the moment, so if anyone has advice in this area let me know.

MSDN Article: ‘Employing the Domain Model Pattern’

I’m a big advocate of domain driven design.  One of the ‘gurus’ in the community, Udi Dahan (NServiceBus creator, etc…) has written an article for the latest version of MSDN magazine titled ‘Employing the Domain Model Pattern’

Topics include:

  • Domain model pattern
  • Scenarios for using the domain model pattern
  • Domain events
  • Keeping the business in the domain

While I’m here, I’d like to make a plug for NCommon – which is a framework that helps fulfill the DDD pattern (including recent addition of domain events).  NCommon currently supports NHibernate, Linq to Sql, and the Entity Framework.  The trunk already has parts of EF 4.0 being setup as well.

This article builds off the concepts here:

‘Domain Events – Salvation’ by Udi

For usage in NCommon see ‘Domain Events pattern in NCommon

This is a very powerful way of keeping with the SRP.  One technique I hope to deploy is the ability to separate out service to service calls.

ie. EmailService handles emailing.  I don’t want to inject an ‘EmailService’ into my ‘OrdersService’

When the SendEmail event is fired, it can be handled by a handler that has an EmailService injected into it instead.

I might as well also mention an article in the same issue ‘N-Tier Application Patterns’ – although it should be titled ‘Entity Framework with WCF’  🙂

This article discusses:

  • N-tier design patterns
  • Entity Framework
  • Microsoft .NET Framework 4

This article uses the following technologies:
Entity Framework, Windows Communication Foundation

Included in this month’s MSDN is ‘Entity Framework v2 and Data Access Architecture Best Practices’

MSDN Article: ‘Employing the Domain Model Pattern’

I’m a big advocate of domain driven design.  One of the ‘gurus’ in the community, Udi Dahan (NServiceBus creator, etc…) has written an article for the latest version of MSDN magazine titled ‘Employing the Domain Model Pattern’

Topics include:

  • Domain model pattern
  • Scenarios for using the domain model pattern
  • Domain events
  • Keeping the business in the domain

While I’m here, I’d like to make a plug for NCommon – which is a framework that helps fulfill the DDD pattern (including recent addition of domain events).  NCommon currently supports NHibernate, Linq to Sql, and the Entity Framework.  The trunk already has parts of EF 4.0 being setup as well.

This article builds off the concepts here:

‘Domain Events – Salvation’ by Udi

For usage in NCommon see ‘Domain Events pattern in NCommon

This is a very powerful way of keeping with the SRP.  One technique I hope to deploy is the ability to separate out service to service calls.

ie. EmailService handles emailing.  I don’t want to inject an ‘EmailService’ into my ‘OrdersService’

When the SendEmail event is fired, it can be handled by a handler that has an EmailService injected into it instead.

I might as well also mention an article in the same issue ‘N-Tier Application Patterns’ – although it should be titled ‘Entity Framework with WCF’  🙂

This article discusses:

  • N-tier design patterns
  • Entity Framework
  • Microsoft .NET Framework 4

This article uses the following technologies:
Entity Framework, Windows Communication Foundation

Included in this month’s MSDN is ‘Entity Framework v2 and Data Access Architecture Best Practices’