NHibernate Unit of Work - Multiple Sessions (WinForms)

I am struggling with a learning curve for NHibernate.

We are currently porting our C # Winforms application to use NHibernate and using Unit of Work (UnitOfWork) as described by Gabriel Schenker.

http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx

We want to use UnitofWork based on Talk. For example, when a user opens a form, we open a UnitOfWork session and keep it open until the form is closed. Forms are just an easier way to define a business conversation, we can slightly change this depending on the implementation, but for this example, please use the open and close form as an example.

The problem arises when we have a script for opening a form on top of another form. In this case, we should have two UnitOfWork operating modes. One of them is still active for the base form and the new UnitOfWork module for the new form.

How do we implement this? Does the UnitOfWork functionality provided by Gabriel allow only one session per UnitOfWork? My initial thoughts are to store sessions in a dictionary so that sessions can be called from any form or part of the application.

Your thoughts?

+3
source share
2 answers
+2

- , , UOW, , UOW , , .

; , ISession UOW.

, , , -. , UOW, Program.cs ISession, :

private readonly ISession _session;

public frmPlayerViewer()
{
    InitializeComponent();
    // bail out if we're in the designer
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    {
            return;
    }
    _session = Program.OpenEvtSession(FlushMode.Commit);
}

, ISession , OnFormClosing:

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        if (!e.Cancel && _session != null)
        {
            if (_session.Transaction.IsActive)
            {
                const string msg = "OnFormClosing with active transaction.";
                log.Error(msg);
                throw new Exception(msg);
            }
            _session.Dispose();
        }
    }

, .

0

Source: https://habr.com/ru/post/1781364/


All Articles