views:

218

answers:

1

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 ?

A: 

"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 ?"

You have to create a custom validation attribute:

here is an example:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class LoginUniqueAttribute : ValidationAttribute
    {
        private static readonly string DefaultErrorMessage = MUI.LoginNameInUse;

        public LoginUniqueAttribute()
            : base(DefaultErrorMessage)
        {
        }

        public override string FormatErrorMessage(string name)
        {
            return DefaultErrorMessage;
        }

        public override bool IsValid(object value)
        {
            if (string.IsNullOrEmpty((string)value))
            {
                return true;
            }

            var userService = IoC.Resolve<IUserService<User>>();
            return userService.GetByLogins(new[] { (string)value }).Count() == 0;
        }
    }

and usages into a UserInput dto

        [Required]
        [LoginUniqueAttribute]
        [RegularExpression("^[A-Za-z0-9.'\\s]+$", ErrorMessage = "Only characters and digits are allowed")]
        [DisplayNameLocalized(typeof(MUI), "LoginName")]
        public string LoginName { get; set; }

also do not forget to initialize the validation in your global.asax.cs file on Application_Start:

 private void InitializeValidator()
        {
            var provider = new NHibernateSharedEngineProvider();
            NHibernate.Validator.Cfg.Environment.SharedEngineProvider = provider;
        }
isuruceanu
Hi isuruceanu,Thanks for the response. Great Example. However I was wondering, Don't you think it would be a good idea to inject the userService object via LoginUniqueAttribute constructor or its property, instead of creating it in within the IsValid() Method ?Regards,Nabeel
nabeelfarid
Yes of course. I agree, only limitation is that Attribute class should have a parameterless constructor. Following by SOLID we should instantiate the private field in the constructor.
isuruceanu