Another .NET Blog

To content | To menu | To search

Tag - NHibernate

Entries feed - Comments feed

Monday 23 August 2010

[Ninject] Use one database session per view-model

One very useful web page I've read before beginning to code on my new project was this one: Data Access - Building a Desktop To-Do Application with NHibernate, from the well-known Ayende. In this article, among other hints and best-practices, he says, at the end of the Managing Sessions chapter:

The recommended practice for desktop applications is to use a session per form, so that each form in the application has its own session. Each form usually represents a distinct piece of work that the user would like to perform, so matching session lifetime to the form lifetime works quite well in practice. The added benefit is that you no longer have a problem with memory leaks, because when you close a form in the application, you also dispose of the session. This would make all the entities that were loaded by the session eligible for reclamation by the garbage collector (GC).

There are additional reasons for preferring a single session per form. You can take advantage of NHibernate’s change tracking, so it will flush all changes to the database when you commit the transaction. It also creates an isolation barrier between the different forms, so you can commit changes to a single entity without worrying about changes to other entities that are shown on other forms.

While this style of managing the session lifetime is described as a session per form, in practice you usually manage the session per presenter.

We'll see now how to implement this behavior, with Caliburn as the MVVM client framework, and Ninject as the dependency injector.

Continue reading...

Wednesday 4 August 2010

[PostSharp] How to virtualize all the methods of a class

When using NHibernate, it is recommended to declare the public and protected properties and methods of your entities as virtual, to be able to use them with proxies.

But missing a virtual keyword for a member of a POCO is easily forgettable, and unfortunately, you will only be warned of your mistake on runtime, when your mappings are being built.

In this article, I will show you how to 'not care' anymore about that, and let PostSharp do this stuff for us.

Continue reading...