Readonly Strategies with Nhibernate and Fluent Nhibernate

I read nhibernate for beginners 3.0 and read about common mistakes (I made some of them)

I am wondering what strategies you want to make one or more readonly entries. Right now, I am returning all rows and viewing them, making them readonly through session.Readonly ().

I like in the book what they do with smooth

class EntityMap : ClassMap<Entity> { public EntityMap() { SchemaAction.None(); ReadOnly(); // Mappings } } 

What I'm not sure what will happen if I need these records so that they are not Readonly? For me, this means that I have to do this same display minus these two lines of code.

So, I would like to do this and have ReadonlyEntityMap and EntityMap, but I no longer need to duplicate all the settings twice.

Anyone have any ideas on how to do this? Or is it better to read ideas?

+4
source share
2 answers

Using ReadOnly() in your mapping will disable any updates back to the database when the properties of the object change.

If you want to apply some kind of ReadOnly and Writeable strategy, I would suggest using the Fluent Convention and a marker interface or similar.

You can also apply ReadOnly() to a property if you want to disable writing with only one property. I use this technique to avoid writing to computed columns in SQL Server.

Something like that:

 public interface IReadOnly{} //Mark entities with this interface public class ReadOnlyConvention : IClassConvention, IClassConventionAcceptance<IClassInspector> { public void Accept(IAcceptanceCriteria<IClassInspector> criteria) { criteria.Expect(x => x.EntityType Is IReadOnly); } public void Apply(IClassInstance instance) { instance.ReadOnly(); } } 

Update: If you want to do this, I would suggest creating an IUserType for your DateTime object that does the conversion to user time without changing the underlying data.

+4
source

It depends on what you are trying to achieve. If you are trying to optimize memory consumption, you should consider improving your session management strategy or using other methods to improve performance. You can, for example:

  • Clear the session that loaded your objects and it will free up all the memory allocated by the first level cache. Then the objects can be re-attached (combined) to a new session, if necessary. Thus, you do not need objects that need to be read only in the first place.

  • Cut some objects when you no longer need them. This will save the rest of the objects in the first level cache.

  • Use IStatelessSession , which does not use first level cache at all.

Again, the answer depends on the architecture of your application.

+1
source

All Articles