using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RDH.Data
{
    public class ConnectionSessionScope : IDisposable
    {
        public bool HasOpen { get; set; }
        public ConnectionSessionScope()
        {
            if (ConnectionFactory.Current.GetSessionConnection().State != System.Data.ConnectionState.Open)
            {
                try
                {
                    ConnectionFactory.Current.GetSessionConnection().Open();
                    HasOpen = true;
                }
                catch (Exception ex)
                {
                    HasOpen = false;
                    throw ex;
                }
            }
        }
        public ConnectionSessionScope(String connString)
        {
            if (ConnectionFactory.Current.GetSessionConnection(connString).State != System.Data.ConnectionState.Open)
            {
                try
                {
                    ConnectionFactory.Current.GetSessionConnection(connString).Open();
                    HasOpen = true;
                }
                catch (Exception ex)
                {
                    HasOpen = false;
                }
            }
        }
        public void Dispose()
        {
            ConnectionFactory.Current.CloseSessionConnection();
        }
    }
}
