Hi,
After reading the hibernate in action book, I decided to do the following:
I have my DomainModel classes like Member.cs, Project.cs, Task.cs, Address.cs, etc etc.
These classes have property and method, but are not involved in retrieving or saving to the DB.
These classes are in DomainModel.dll
I also have another dll called Repositories.dll which contains
MemberRepo.cs which has a Save, Delete, GetById, GetByEmail, GetByFirstname methods to save/retrieve from the DB.
These are all statics classes.
Now, I have a website project with a reference to the two dll.
I also have an HttpModule that will Commit the Nhibernate transaction in PreSendRequestHeaders event of the page.
Anytime I want to create a new object, a member for instance, I do something like
Member m = new Member('Firstname', 'Lastname',
'Email@domain.com');
MemberRepo.Save(m);
and it will be saved in my DB.
When I want to modify a member, I'd do the following:
Member m = MemberRepo.GetById(10); // retrieve the IdMember = 10
m.Firstname = 'NewFirstname';
and it will get saved as well since the CommitTransaction will get call automatically by the HttpModule.
All of this works pretty good. One of the drawback, is that I won't be able to catch any exception from commiting a transaction in my code since it only gets called when all my code has executed. So I have to use global.asax to catch the Application_Error.
As for where I get the ISession object, I created another dll Persistance.dll and use the Factory pattern. I have INHibernateProvider interface that define method like GetSession, CloseSession, BeginTransaction, CommitTransaction, RollbackTransaction and a GetFactory method. From that, I created AspNetNHibernateProvider that implement the interface. It store the Factory/Session into HttpContext.Current.Application and use the singleton pattern so there is only one factory across the application.
I also created WinFormNHibernateProvider which store the factory/session into a static class and again, use the singleton pattern.
In the config file on my app, I tell which of the Provider to use.
I was wondering if this is a good design. I'm hoping other could share their design and/or comment mine.
Thanks.