I am currently refactoring my project and one thing I'm not quite sure on is how to handle business validation errors.
At the moment I am using the RulesException class from the xVal library, I beleive this library is no longer being developed so I probably need to update my code accordingly...here is an example of one of my methods on a domain model:
public virtual void AddComment(ProfileComment comment)
{
// ensure account is active before sending a message
if (comment.FromAccount.AccountStatus != Enums.Common.AccountStatus.Active) throw new RulesException("", "Your account is not active");
// throw exception if user has blocked this user
if (HasUserBeenBlocked(comment.FromAccount)) throw new RulesException("", "User has blocked this action being performed.");
TotalComments++;
Comments.Add(comment);
}
Then I catch this ecxception at the controller level like so:
// process adding new comment
try
{
accountTasks.PostProfileComment(user, account, profileComment);
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState);
}
if (!ModelState.IsValid)
{
return Json(ModelState.Errors());
}
What would you reccomend as an alternative, or would you reccomend I stick with what I have?