Well, I use the Entity Framework and Code First to manage my data access, and I'm thinking of working with a single, static, global instance of my DataBaseContext. But I have two questions:
Here is an example of how to access the data in my bank:
CURRENT - Each method calls an instance of BaseDates
using (var db = new BaseDados())
{
var fatura = db.Contas_Receber.Where(x => x.Chave == chaveFatura).FirstOrDefault();
}
POSTERIORLY - There will be only one instance of BaseDates , and all DAOs in my system will use this instance.
public static class DAO<T> where T : class
{
private static SETCOM_BaseDados db;
public static void Create(T entidade)
{
db.GetInstance();
try
{
db.Set<T>().Add(entidade);
((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.ChangeObjectState(entidade, System.Data.Entity.EntityState.Added);
}
catch (Exception ex)
{
throw new Exception("Erro ao inserir dados : " + ex.Message);
}
}
}
public static class BaseDadosExtension
{
public static SETCOM_BaseDados GetInstance(this BaseDados db)
{
return (db == null ? new BaseDados() : db);
}
}
Call to create a new Reward Accounts :
Contas_Receber cr = new Contas_Receber();
cr.Valor = 0;
cr.Cliente = new Cliente();
DAO<Contas_Receber>.Create(cr);