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?
Like you, we wanted to be prepared for MVC3 and the removal of xVal. We removed the dependency by writing our own RulesException, ErrorInfo classes and extension method AddModelStateErrors added to our Framework.
In this way, you only have to remove xVal, and resolve namespaces. If you have a refactoring tool, this is trivial.
I have a gist with these classes.