tags:

views:

182

answers:

1

I would like to know if there is a way to make an instance of an entity immutable at runtime. I know that I can have readonly types, but I would like readonly instances of types at runtime. Reason being, that I want to use NH domain objects like a lightweight entity by modifying properties, but don't want those changes to persist when doing a Session.Flush () or similar. There is always Session.Evict(), but that won't prevent the unknowing developer from a Session.Save () on a "readonly" entity.

+1  A: 

Take a look at this article by Fabio Maulo, you can use that as a starting point and change it to track individual entities.

Mauricio Scheffer
This is an interesting thing to consider. I can't use this in our solution with legacy dependencies on existing flush behavior. However, I like seeing where the hooks are into instance state, which might inspire me to add overloads to NH for GetReadOnly or ListReadOnly<>, or something similar.
Trent