A couple of days ago I was thinking about using an ORM product to work with my database. It gives me objects to work with and I need less time coding the data access classes. However, there are some side effects to this.
One of the problems with data access is that you should be very careful what to load from the db into objects at which moment. Sometimes you only need the name of a certain entity and not all it’s related entities or other properties. Loading other data besides the name is a waste of memory and processing power. However, loading partial objects isn’t always smart as others may use your code to load the same set of data but with a different intention.
One solution to this problem is lazy loading, this makes it possible to load data when it is first used by the object that loaded the object. This limits the amount of data loaded at once, but provides developers with new challenges. For example if you are serializing the objects that you loaded using lazy loading will cause all data to be loaded, because the serializer accesses all properties on the object. This isn’t really what you want. Another problem I had with lazy loading was the fact when I closed the session with the database at the end of the execution of a method, I also lost the lazy loading functionality. (I used NHibernate here).
I’m not 100% sure what to do with this, but I have a strong urge to drop the whole NHibernate thing and go with a more tailored solution, that works for my application but maybe not for a future application I might build using the same assemblies. The chances are not really high that I will, but it’s something to think of.
Scott Bellware also wrote an article on this: http://codebetter.com/blogs/scott.bellware/archive/2007/05/30/163592.aspx. Which I think is worth a read J