Sharp architecture; Accessing Validation Results

Posted by nabeelfarid on Stack Overflow See other posts from Stack Overflow or by nabeelfarid
Published on 2010-05-13T11:07:39Z Indexed on 2010/05/13 11:44 UTC
Read the original article Hit count: 594

Filed under:
|

I am exploring Sharp Architecture and I would like to know how to access the validation results after calling Entity.IsValid(). I have two scenarios e.g.

1) If the entity.IsValid() return false, I would like to add the errors to ModelState.AddModelError() collection in my controller.

E.g. in the Northwind sample we have an EmployeesController.Create() action when we do employee.IsValid(), how can I get access to the errors?

public ActionResult Create(Employee employee) { 
  if (ViewData.ModelState.IsValid && employee.IsValid()) { 
    employeeRepository.SaveOrUpdate(employee); 
  } 
  // ....
}

[I already know that when an Action method is called, modelbinder enforces validation rules(nhibernate validator attributes) as it parses incoming values and tries to assign them to the model object and if it can't parse the incoming values  then it register those as errors in modelstate for each model object property. But what if i have some custom validation. Thats why we do ModelState.IsValid first.]

2) In my test methods I would like to test the nhibernate validation rules as well. I can do entity.IsValid() but that only returns true/ false. I would like to Assert against the actual error not just true/ false.

In my previous projects, I normally use a wrapper Service Layer for Repositories, and instead of calling Repositories method directly from controller, controllers call service layer methods which in turn call repository methods. In my Service Layer all my custom validation rules resides and Service Layer methods throws a custom exception with a NameValueCollection of errors which I can easily add to ModelState in my controller. This way I can also easily implement sophisticated business rules in my service layer as well. I kow sharp architecture also provides a Service Layer project. But what I am interested in and my next question is:

How I can use NHibernate Vaidators to implement sophisticated custom business rules (not just null,empty, range etc.) and make Entity.IsValid() to verify those rules too ?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about nhibernate