Business object often have the need to enrolled on transaction. In order to implement it, we have the following choices:
1. Pass transaction to every business object;
2. Simply assume a pragmatic one transaction per session.
I'm presently choosing this last one on most of my implementations for simplicity sake. HEre are some of my implementation details:
a) create a session manager:
///
/// Handles creation and management of sessions and transactions. It is a singleton because
/// building the initial session factory is very expensive. Inspiration for this class came
/// from Chapter 8 of Hibernate in Action by Bauer and King. Although it is a sealed singleton
/// you can use TypeMock (http://www.typemock.com) for more flexible testing.
///
public sealed class NHibernateSessionManager
b) in the session manager, handle context by session:
///
/// Since multiple databases may be in use, there may be one transaction per database
/// persisted at any one time. The easiest way to store them is via a hashtable
/// with the key being tied to session factory. If within a web context, this uses
///instead of the WinForms specific .
/// Discussion concerning this found at http://forum.springframework.net/showthread.php?t=572
///
private ITransaction ContextTransaction
{
get
{
if (IsInWebContext())
{
return (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
}
else
{
return (ITransaction)CallContext.GetData(TRANSACTION_KEY);
}
}
set
{
if (IsInWebContext())
{
HttpContext.Current.Items[TRANSACTION_KEY] = value;
}
else
{
CallContext.SetData(TRANSACTION_KEY, value);
}
}
}
c) involve it on a TransactionContext class that derives from IDisposable, making use of NHibernateSessionManager for usage simplicity sake.
using (TransactionContext transactionContext = new TransactionContext())
{
// do some stuff
transactionContext.SetComplete();
}
Although this is an NHibernate implementation, the patterns themselves are general to transactions.
No comments:
Post a Comment