1

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Paul Hinett
  • 1,951
  • 2
  • 26
  • 40

1 Answers1

1

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.

Kurt Johnson
  • 411
  • 5
  • 15
  • yeah that kind of makes sense, however I was using the RulesException in my Domain objects layer / project...your code references System.Web.Mvc, is there any way to use this in my domain layer without adding a reference to System.Web.Mvc? – Paul Hinett Jun 28 '11 at 10:33