6

What are your experiences with the latest version of NHibernate (2.0.1 GA) regarding disconnected scenarios?

A disconnected scenario is where I fetch some object graph from NHibernate, disconnect from the session (and database connection), do some changes in the object graph (deleting in collections, adding entities, updating entities) and then reconnect and save....

2
  • You should probably be more specific. What kind of experiences, what kind of "disconnected scenario"? Oct 21, 2009 at 12:01
  • Why ? I think I posted a scenario already, not very specific though, but in general. When I ask a very specific scenario the amount of responses could be less having a general one. Nov 9, 2009 at 8:38

2 Answers 2

4

We tried this in a client-server architecture. Now we are moving to DTO (data transfer objects). This means, the detached entities are not directly sent to the client anymore, but specialized objects.

The main reason to move in this direction is not NHibernate, it is actually the serialization needed to send entities to the client. While you could use lazy-loading (and you will!) while you are attached to the session, you need to get all references from the database to serialize it.

We had lots of Guids instead of references and lots of properties which are mapped but not serialized ... and it became a pain. So it's much easier to copy the stuff you really want to serialize to its own structure.

Besides of that - working detached could work well.

  • Be careful with lazy loading, which will cause exceptions to be thrown when accessing non loaded objects on a detached instance.
  • Be careful with concurrency, the chance that entities had changed while they where detached is high.
  • Be careful if you need some sort of security or even if you want your server alown to make some data changes. The detached objects could potentially return in any state.
4
  • If you look at LLBLGenPro for example: before generating code, you can decide which template you will use. One will use the active record pattern called "self-servicing" and the other one will use the adapter (like the ADO.NET DataAdapter). The last one is handy when having a disconnected scenario (remoting or webservices). Oct 21, 2009 at 10:53
  • 2
    @Patrick: I have to admit that I don't have a clue what you are talking about. Oct 21, 2009 at 11:55
  • With the self servicing template LLBLGenPro will create a class (or more) where you can get and set properties (POCO-style) and then call a method like "Save". This object is self-servicing regarding state management and object-tree loading and SQL creation. The adapter style generates a light-weight class with only properties and is serializable; this object can be passed to an other object that is performing all the state management and SQL creation, etc... Oct 21, 2009 at 19:02
  • 1
    Ok, but you don't need this when you have NH. So I don't know what you want to tell me with that. The serialization problem is independent from the database stuff. If you have a complex object graph, where the aggregate root references half of the database. Of course you can serialize half of the database, but usually you don't want to do it. Oct 22, 2009 at 7:27
4

You may take a look on session methods SaveOrUpdateCopy and Merge.

Here is an article which gives you more details: NHibernate feature: SaveOrUpdateCopy & Merge

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.