Hi
I want to throw an ArgumentException if the id passed to my GetX factory is not valid. The best (most proper) way I can think to this is:
public static Day GetDay(Guid id)
{
if (!Day.Exists(id))
{
throw new ArgumentException("Invalid id.", "id");
}
return DataPortal.Fetch<Day>(new Criteria(id));
}
However it involves to calls to the DB, so to increase performance it seams I could just throw a custom exception in the dataportal when no results are returned and catch this in the factory and rethrow as an argument exception. Like this:
public static Day GetDay(Guid id)
{
try
{
return DataPortal.Fetch<Day>(new Criteria(id));
}
catch (DataPortalException ex)
{
if (ex.BusinessException is NoResultException)
{
throw new ArgumentException("Invalid id.", "id", ex);
}
else
{
throw;
}
}
}
private void DataPortal_Fetch(Criteria criteria)
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand cm = db.GetStoredProcCommand(FetchDaySP);
db.AddInParameter(cm, DayIdParam, DbType.Guid, criteria.DayId);
using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(cm)))
{
bool isResult = dr.Read();
if (!isResult)
{
throw new NoResultException("No results.");
}
_DayId = criteria.DayId;
_Date = dr.GetDateTime(DateCol);
}
}
Does anybody see any problems with this? I presume the stack trace will be preserved in the inner exception in ArgumentException. But I may be missing something, I've just starting to get my head around error handling concepts using exceptions. Thanks to a previous thread I've got my logging plan sorted now I just want to focus the exceptions coming from my BOs.
Regards, George.
Hi Marjon,
Thanks for posting. I presumue you're talking about the second method I outlined?
Thanks
George
Thanks Marjon.
George.
Copyright (c) Marimer LLC