What is a good way to setup CRUD methods and SessionFactory in Nhibernate?

This is because you are calling using(_db), which closes the session at the end of the block.

This is because you are calling using(_db), which closes the session at the end of the block. Rather than having the variable _db just call OpenSession for each operation public void Add(User user) { using (ISession session = NOpenSession()) { using (ITransaction transaction = session. BeginTransaction()) { IEnumerable userRoles = user.

UserRoles; user. UserRoles = null; session. Save(user); foreach (UserRole userRole in userRoles) { userRole.

UserID = user. UserID; session. Save(userRole); } transaction.Commit(); } } } UPDATE: public void Add(Login login) { using (ISession session = NOpenSession()) { Add(login, session); } } public void Add(Login login, ISession session) { //no longer need to create a session here - it is passed in //using (ISession session = NOpenSession()) ...Add using the session } It is the creation of the factory that is expensive, so your Helper is a good thing to use.

Opening the session is a cheap operation, so no need to have a shared session like this. Here is a link to another SO question that shows how to make your factory helper threadsafe: http://stackoverflow.com/questions/2362195/ensure-nhibernate-sessionfactory-is-only-created-once.

– Andi Aug 24 '10 at 8:45 No, the Factory is a static singleton. It is the ISession that will be created for each operation. See my edit for the reason that the _db is being closed.

- First line of my answer. – Daniel Dyson Aug 24 '10 at 8:47 Also, you can test whether a new factory is created by putting a breakpoint on the line after: if (_sessionFactory == null) – Daniel Dyson Aug 24 '10 at 9:11 1 You will only want one transaction per group of related activities. If you are performing multiple changes, create the transation straight after the call to OpenSession, as you do in the first code block in your question and then commit it just before the closing brace as you have done.

– Daniel Dyson Aug 25 '10 at 14:18 1 When I get a chance (i.e. When I get home), I will put together a sample app. Send me an email and we can set up a dialog offline, and perhaps post the results here as a new answer later.

– Daniel Dyson Aug 25 '10 at 14:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions