I have WCF Service using .NET 4.0.
I have similar methods, similar code and I want use good patterns for improvement the code for get high level quality.
I want reuse code, maybe using Action<T>
, and improve exception handling, ...
Is it possible?
Interface
[FaultContract(typeof(MyCompany.VSIntegration.SvcDespliegue.FaultContracts.DefaultFaultContract))]
[OperationContract]
QueryLogRes QueryLogsAdvanced(QueryLogReq req);
[FaultContract(typeof(MyCompany.VSIntegration.SvcDespliegue.FaultContracts.DefaultFaultContract))]
[OperationContract]
QueryLogRes QueryLogsByPeticionEV(QueryLogsByPeticionEVReq req);
Implementation
public QueryLogRes QueryLogsAdvanced(QueryLogReq req)
{
try
{
IList<LogsDesplieguesDto> listLogs;
bool result;
ArgumentHelper.AssertNotNull<QueryLogReq>(req, "request");
Log.Trace(LogCategoryNames.DebugGeneral, "QueryLogsAdvanced(" + req.ToString() + ")");
using (var rpy = new LogsRepository())
{
listLogs = rpy.QueryLogs(req.Proyecto, req.Fecha, req.Servidor, req.Entorno, req.Usuario, req.Maquina, req.result, req.Etiqueta, req.PeticionEV);
}
result = (listLogs != null && listLogs.Count > 0);
Trace.WriteLine("QueryLogsAdvanced Res: " + result);
var res = new QueryLogRes(result, result ? "" : "No se han encontrado registros");
if (!result) return res;
Trace.WriteLine("QueryLogsAdvanced listLogs: " + listLogs.Count);
foreach (var row in listLogs)
{
var dl = new DetailLog();
dl.Entorno = row.Entorno;
dl.Fecha = row.Fecha;
dl.Log = row.Log;
dl.Maquina = row.Maquina;
dl.result = row.result;
dl.Servidor = row.Servidor;
dl.Usuario = row.Usuario;
dl.Etiqueta = row.Etiqueta;
dl.PeticionEV = row.PeticionEV;
res.DetailsLogs.Add(dl);
}
Log.Trace(LogCategoryNames.DebugGeneral, "QueryLogsAdvanced. Logs encontrados: " + listLogs.Count);
Trace.WriteLine("QueryLogsAdvanced. Logs encontrados: " + listLogs.Count);
return res;
}
catch (Exception exc)
{
Log.Trace(LogCategoryNames.Errores, "EXCEPCION en 'QueryLogs': " + exc.Message);
Exception auxExc = exc;
bool rethrow = false;
MyCompany.Frk.Exceptions.ExceptionHandler.HandleExceptionNotRethrowing(exc, NOMBRE_POLITICA_VSINTEGRATION, out auxExc, out rethrow);
if (rethrow)
{
throw new FaultException<DefaultFaultContract>(new DefaultFaultContract(-1, auxExc.Message, Guid.Empty));
}
return new QueryLogRes(false, exc.Message);
}
}
public QueryLogRes QueryLogsByPeticionEV(QueryLogsByPeticionEVReq req)
{
try
{
IList<LogsDesplieguesDto> listLogs;
bool result;
ArgumentHelper.AssertNotNull<QueryLogsByPeticionEVReq>(req, "request");
ArgumentHelper.AssertNotNullOrEmpty(req.PeticionEV, "PeticionEV");
using (var rpy = new LogsRepository())
{
listLogs = rpy.QueryLogsByPeticionEV(req.PeticionEV);
}
result = (listLogs != null && listLogs.Count > 0);
QueryLogRes res = new QueryLogRes(result, result ? "" : "No se han encontrado registros");
if (result)
{
foreach (var row in listLogs)
{
DetailLog dl = new DetailLog();
dl.Entorno = row.Entorno;
dl.Fecha = row.Fecha;
dl.Log = row.Log;
dl.Maquina = row.Maquina;
dl.result = row.result;
dl.Servidor = row.Servidor;
dl.Usuario = row.Usuario;
dl.Etiqueta = row.Etiqueta;
dl.PeticionEV = row.PeticionEV;
res.DetailsLogs.Add(dl);
}
}
return res;
}
catch (Exception exc)
{
Log.Trace(LogCategoryNames.Errores, "EXCEPCION en 'QueryLogs': " + exc.Message);
Exception auxExc = exc;
bool rethrow = false;
MyCompany.Frk.Exceptions.ExceptionHandler.HandleExceptionNotRethrowing(exc, NOMBRE_POLITICA_VSINTEGRATION, out auxExc, out rethrow);
if (rethrow)
{
throw new FaultException<DefaultFaultContract>(new DefaultFaultContract(-1, auxExc.Message, Guid.Empty));
}
return new QueryLogRes(false, exc.Message);
}
}