using System;
using System.Configuration;
using System.Data.SqlClient;

namespace Levelcode_licenseCliente.DataAccess
{
    public class DbConexao : IDisposable
    {
        private readonly SqlConnection _conexao;

        public SqlConnection Conexao => _conexao;

        public DbConexao()
        {
            string conn =
                ConfigurationManager
                .ConnectionStrings["DefaultConnection"]
                .ConnectionString;

            _conexao = new SqlConnection(conn);
        }

        public void Abrir()
        {
            if (_conexao.State != System.Data.ConnectionState.Open)
                _conexao.Open();
        }

        public void Fechar()
        {
            if (_conexao.State != System.Data.ConnectionState.Closed)
                _conexao.Close();
        }

        public void Dispose()
        {
            Fechar();
            _conexao.Dispose();
        }
    }
}
