// Persist library : Persistence layer
// Copyright (C) 2003 Vincent Daron
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
namespace Persist.Attributes{
/// <summary>
/// This is the IMessage sink implementation used in the transaction mechanism
/// </summary>
public class TransactionMessage : IMessageSink
{
private IMessageSink theNextSink;
/// <summary>
/// Constructor
/// </summary>
/// <param name="nextSink"></param>
public TransactionMessage(IMessageSink nextSink)
{
theNextSink = nextSink;
}
#region IMessageSink Members
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public IMessage SyncProcessMessage(IMessage msg)
{
Preprocess(msg);
IMessage returnMethod = theNextSink.SyncProcessMessage(msg);
PostProcess(msg, returnMethod);
return returnMethod;
}
/// <summary>
///
/// </summary>
public IMessageSink NextSink
{
get
{
return theNextSink;
}
}
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
/// <param name="replySink"></param>
/// <returns></returns>
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
throw new InvalidOperationException() ;
}
#endregion
private void Preprocess(IMessage msg)
{
Persist.PersistenceManagerFactory.Instance.BeginTransaction();
}
private void PostProcess(IMessage msg, IMessage msgReturn)
{
// We only want to process method return calls
if (!(msg is IMethodMessage) ||
!(msgReturn is IMethodReturnMessage)) return;
if (((IMethodReturnMessage)msgReturn).Exception != null)
{
Persist.PersistenceManagerFactory.Instance.Rollback();
}
else
{
Persist.PersistenceManagerFactory.Instance.Commit();
}
}
}
}
|