Reputation: 9013
I have Business logic layer and DB layer (Entity framework). For example, I receive some data from DB.
DB layer:
public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
SmartphonePhotographerResponseManage response = (from i in db.SmartphonePhotographerResponses
where i.RequestID == RequestID
select new SmartphonePhotographerResponseManage()
{
ResponseID = i.ID,
FormattedAddress = i.EditorialPixlocateRequest.FormattedAddress
}).FirstOrDefault();
return response;
}
BL layer (it's the simplest example, sense of BL layer - just "throw" result from DB to client (ASP.NET MVC in my case, but no matter). Of course, BL method can have any additional logic):
public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
return _repository.ResponseManage(RequestID);
}
It works and works fine. But I want to throw my own exception if record does not exists (i.e. record was deleted, but user has link to his bookmarks):
public class RecordNotFoundException<T> : Exception
{
public RecordNotFoundException(T ID) : base(String.Format("No Records for passed ID={0}", ID.ToString()))
{
}
}
I have 2 way to throw it: 1. In DB layer:
public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
SmartphonePhotographerResponseManage response = (from i in db.SmartphonePhotographerResponses
where i.RequestID == RequestID
select new SmartphonePhotographerResponseManage()
{
ResponseID = i.ID,
FormattedAddress = i.EditorialPixlocateRequest.FormattedAddress
}).FirstOrDefault();
if (response == null)
throw new RecordNotFoundException<int>(RequestID);
return response;
}
or in the BL layer:
public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
var response = _repository.ResponseManage(RequestID);
if (response == null)
throw new RecordNotFoundException<int>(RequestID);
return response;
}
and then catch this exception on client side (controller of ASP.NET MVC for example) and handle it by appropriate way. Both approach will work, but where is more logical place to throw such exception?
EDIT: Also, there is a difficult to throw this exception in BL when I want to edit/delete record. I.e. I have code:
public async Task AcceptOrDeclineFileAsync(int ElementID, bool accept, string smsSid)
{
var element = (from i in db.SmartphonePhotographerResponseElements where i.ID == ElementID select i).FirstOrDefault();
if (element == null)
throw new CommonLibrary.RecordNotFoundException<int>(ElementID);
element.ApprovedByEditorial = accept;
element.SmsSID = smsSid;
await db.SaveChangesAsync();
}
If I not throw exception in DB layer I get common exception type (I assume, NullReferenceException) in BL. Maybe, it's enough? Any other situations, when I can get NullReferenceException?
Upvotes: 1
Views: 733
Reputation: 7562
I would put the exception in your Business Logic error. The Database Layer should simply report what it returns; nothing in this case. This is only a problem because your business logic requires that it be. So, throw the error there rather than at a lower level.
Upvotes: 3