Nesta parte do artigo, disponibilizaremos o código completo da classe DAL Genérica.
namespace GenDAL.Library.DataBaseLib.GenericDAL
{public class GenericDAL where T : IBaseModel, new()
{
public class GenericDALException : Exception
{
public string ErrorMessage
{
get
{
return base.Message;
}
}
public GenericDALException(string errorMessage) : base(errorMessage)
{
}
public GenericDALException(string errorMessage, Exception innerException)
: base(errorMessage, innerException)
{
}
}
private DBManager _dbManager;
protected DBManager TransactionScope()
{
DBManager man = new DBManager();
this._dbManager = man;
return man;
}
protected DBManager TransactionScope(string currentConn)
{
DBManager man = new DBManager(currentConn);
this._dbManager = man;
return man;
}
private string GetFieldName(PropertyInfo piInfo)
{
string retorno = "";
foreach (object attributo in piInfo.GetCustomAttributes(false))
{
if (attributo is AttFieldDB)
{
retorno = ((AttFieldDB)attributo).FieldName;
break;
}
}
return retorno;
}
private void SetPropertyValue(DataRow row, PropertyInfo piInfo, T obj)
{
foreach (DataColumn col in row.Table.Columns)
{
if (col.ColumnName.ToLower().Equals(this.GetFieldName(piInfo).ToLower()))
{
if (piInfo.PropertyType.Module.ScopeName.ToLower().Equals("commonlanguageruntimelibrary"))
{
if (row[col] != DBNull.Value)
{
piInfo.SetValue(obj, Convert.ChangeType(row[col], piInfo.PropertyType), null);
break;
}
}
}
}
}
private List DataSetToList(DataSet ds)
{
T obj = new T();
List retorno = null;
try
{
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
retorno = new List();
foreach (DataRow dr in ds.Tables[0].Rows)
{
obj = new T();
foreach (PropertyInfo piAux in obj.GetType().GetProperties())
{
SetPropertyValue(dr, piAux, obj);
}
retorno.Add(obj);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
obj = default(T);
}
return retorno;
}
private void ValidateUsageCustomAttributes()
{
T obj = default(T);
string methodName = "ValidateUsageCustomAttr";
try
{
obj = new T();
MethodInfo minfo = obj.GetType().GetMethod(methodName);
if (minfo != null)
{
minfo.Invoke(obj, null);
}
}
catch (Exception ex)
{
throw ex;
}
}
private void ExecProc(T pObj, ProcedureTypes pProcType)
{
object[] atributos = null;
AttParamProcDB attProcDB = null;
IDbCommand command = null;
IDbDataParameter param = null;
bool flagHasParamOut = false;
try
{
//Valida o uso dos atributos
this.ValidateUsageCustomAttributes();
if (pProcType != ProcedureTypes.SelectFilter &&
pProcType != ProcedureTypes.SelectNoFilter)
{
if (this._dbManager == null || this._dbManager.Conn == null)
{
throw new GenericDALException("Utilize um dos métodos 'TransactionScope' para setar o escopo da transacao atual");
}
foreach (PropertyInfo piInfo in pObj.GetType().GetProperties())
{
atributos = piInfo.GetCustomAttributes(false);
if (atributos != null && atributos.Length > 0)
{
foreach (object att in atributos)
{
if (att is AttParamProcDB)
{
attProcDB = (AttParamProcDB)att;
if (attProcDB.TipoProcedure == pProcType)
{
if (command == null || String.IsNullOrEmpty(command.CommandText))
{
command = this._dbManager.Conn.CreateCommand(); //this._conn.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = attProcDB.ProcName;
command.Transaction = this._dbManager.Trans; // this._transaction;
}
if (command != null)
{
param = command.CreateParameter();
param.ParameterName = attProcDB.ParamName;
param.DbType = attProcDB.DbType;
param.Direction = attProcDB.Direction;
if (attProcDB.Direction == ParameterDirection.Input)
{
param.Value = piInfo.GetValue(pObj, null);
}
else
{
flagHasParamOut = true;
piInfo.SetValue(pObj, param.Value, null);
param.Size = attProcDB.ParamSize;
}
command.Parameters.Add(param);
}
}
}
}
}
}
/*---------Preenche Outputs----------*/
if (command.Parameters != null)
{
command.ExecuteNonQuery();
if (flagHasParamOut)
{
foreach (IDataParameter paramAx in command.Parameters)
{
foreach (PropertyInfo piAux in pObj.GetType().GetProperties())
{
foreach (object attObj in piAux.GetCustomAttributes(false))
{
if (attObj is AttParamProcDB)
{
if (((AttParamProcDB)attObj).TipoProcedure == pProcType &&
((AttParamProcDB)attObj).Direction == ParameterDirection.Output )
{
if (paramAx.ParameterName.ToLower() ==
((AttParamProcDB)attObj).ParamName.ToLower())
{
if (paramAx.Value != null)
{
piAux.SetValue(pObj, paramAx.Value, null);
}
}
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{
this._dbManager.Dispose();
throw ex;
}
finally
{
atributos = null;
attProcDB = null;
command = null;
param = null;
}
}
Continuaremos a classe no próximo artigo