The object-relational mapper for .NET

uNHAddins Persistence Conversation – Part 2: Configuring the conversation

In the first post I showed how to configure the uNHAddins conversation. Now I will show how to use the PersistenceConversation aspects to manage a uNHAddins conversation.

A class which will rule a conversation should be marked as [PersistenceConversational] every public method that is part of a conversation should be marked as [PersistenceConversation].

In the example code we have the interface

public interface IModifyOrderModel
  PurchaseOrder FindOrderOrCreateNew(string number, DateTime dateTime);
  void Persist(PurchaseOrder order);
  void AbortConversation();

which is implemented by the ModifyOrderModel class:

public class ModifyOrderModel : IModifyOrderModel

private readonly IOrderRepository orderRepository;

public ModifyOrderModel(IOrderRepository orderRepository)


this.orderRepository = orderRepository;


[PersistenceConversation(ConversationEndMode = EndMode.Abort)]

public void AbortConversation()

{ // Rollback the use case }


public PurchaseOrder FindOrderOrCreateNew(string number, DateTime dateTime)


var order = orderRepository.GetOrderByNumberAndDate(number, dateTime.Date);

if (order == null)


order = new PurchaseOrder { Date = dateTime, Number = number };


return order;


          [PersistenceConversation(ConversationEndMode = EndMode.End)]
          public void Persist(PurchaseOrder order)




This class manages the order modification conversation. As you see we don’t have references to NHibernate at this point. We do have an IOrderRepository which is going to provide the data we need for the conversation.

The implementation of the repository in the example is very simple, one important thing to notice is that we are injecting an ISessionFactory:

public class OrderRepository : Repository<PurchaseOrder>, IOrderRepository

public OrderRepository(ISessionFactory sessionFactory) : base(sessionFactory) { }

public PurchaseOrder GetOrderByNumberAndDate(string number, DateTime dateTime)

{ var query = Session.GetNamedQuery("GetOrderByNumberAndDate").SetParameter("number", number).SetParameter("dateTime", dateTime);

return query.UniqueResult<PurchaseOrder>();


The Repository<T> is a very thin class which gives minimum CRUD implementations using NH. The interesting part is how we are getting a session:

protected ISession Session
  get { return factory.GetCurrentSession(); }

From the doc: GetCurrentSession() Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use

In our case if you remember from the first post we are using the uNHAddins ThreadLocalConversationalSessionContext, which in general is good enough for a Windows Forms Application.

You may have noticed a property ConversationalEndMode in the PersistenceConversation attribute, for now there are three options:

EndMode.Continue: Can start a conversation and lives it alive.

EndMode.End: Flushes the session and commits current transactions, then it disposes the session

EndMode.Abort: Disposes the session (without accepting the changes)

Basically this is it. In the example code you can find an application using MVP with this ideas. Feel free to comment / ask in the uNHAddins forum about it.

Posted Tue, 10 February 2009 07:17:00 AM by Gustavo
Filed under: SessionFactory NHibernate Session uNHAddin

comments powered by Disqus
© NHibernate Community 2017