Hi,

I have seen a lot of examples about session management where the idea is
trying to hide the ISession from the application ( as far as I understood ).
However implementing session management seems to be a complex task and there
is few documentation about how to do that that for both Winforms and
Webforms apps.

Because of that I was thinking if an explicit way would be better for sake
of simplicity by using a class like that below:

public class ConnectionManager
{
    private static AppType _appType;
    private static ISessionFactory _sessionFactory;
    public static void Iniciar(AppType appType, ISessionFactory
sessionFactory)
    {
        _appType = appType;
        _sessionFactory = sessionFactory;
    }

    //////////////////////////////////////////////////////////////////
    // ONLY USED WHEN CONNECTION MANAGER IS IN WINFORM MODE !!!
    //////////////////////////////////////////////////////////////////
    private static ISession _currentWinFormSession;
    private static ITransaction _currentWinFormTransaction;
    //////////////////////////////////////////////////////////////////

    public static ISession CurrentSession
    {
        get
        {
            if (_appType == AppType.Web) {
                   return (ISession)HttpContext.Current.Session["session"];
}
            else { return _currentWinFormSession; }
        }
        set
        {
            if (_appType == AppType.Web) {
HttpContext.Current.Session["session"] = value;}
            else { _currentWinFormSession = value; }
        }
    }

    private static ITransaction CurrentTransaction
    {
       get
       {
           if (_appType == AppType.Web) {
              return
(ITransaction)HttpContext.Current.Session["transaction"];
           }
           else { return _currentWinFormTransaction; }
       }
       set
       {
         if (_appType == AppType.Web)
{HttpContext.Current.Session["transaction"] = value;}
         else { _currentWinFormTransaction = value; }
       }
    }

    public static void Open()
    {
       ISession sessao = ConnectionManager.CurrentSession;
       if (sessao != null) {throw new SystemException("More than on e
session is opened");}
       ConnectionManager.CurrentSession = _sessionFactory.OpenSession();
    }

    public static void Close()
    {
       ISession sessao = ConnectionManager.CurrentSession;
       if (sessao != null) sessao.Close();
       ConnectionManager.CurrentSession = null;
    }

    public static void BeginTransaction()
    {
       ISession sessao = ConnectionManager.CurrentSession;
       ConnectionManager.CurrentTransaction = sessao.BeginTransaction();
    }

    public static void CommitTransaction()
    {
       ConnectionManager.CurrentTransaction.Commit();
    }

    public static void RollbackTransaction()
    {
       ConnectionManager.CurrentTransaction.Rollback();
    }
}

When an event is triggered by the application, the workflow would be:

void SomeButton_Clicked(...........)
{
  try
  {
    ConnectionManager.Open( );
    ConnectionManager.BeginTransaction( );

     : : : : code block : : : :

    ConnectionManager.CommitTransaction( );
  }
  catch(Exception ex)
  {
    ConnectionManager.Rollback( );
    : : : exception handling : : : :
  }
  finally
  {
    ConnectionManager.Close();
  }
}

What do you think about this approach ?
What would be the problems behind it that would prevent so many people not
to use it ?

Thanks in advance.





-- 
Humberto C Marchezi
---------------------------------------------------------
Master in Electrical Engineering - Automation
Software Consultant and Developer
at the Town Hall of Vitória

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to nhusers@googlegroups.com
To unsubscribe from this group, send email to 
nhusers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to