POST of ViewModel and Validation on DomainModel
I've been developing a web app using asp.net mvc and nhibernate. I'm trying to follow some principles of DDD and best pratices of Asp.Net MVC. My question is about Clean up POST with VIewModels. To illustrate my question, look this entity on my domain model:
[Validator(typeof(EntityValidator))]
public class MyEntity {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual decimal UnitPrice { get; set; }
public virtual Category Category { get; set; }
public MyEntity() { }
}
It's mapped with nhibernate and works fine. For validation, I'm using Fluent Validation, and I have this class:
public class EntityValidator : AbstractValidator<MyEntity>
{
protected IEntityRepository EntityRepository { get; set; }
public EntityValidator(IEntityRepository entityRepository) { // injected by a IoC Container
this.EntityRepository = entityRepository;
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.UnitPrice).GreaterThan(0);
RuleFor(x => x.Category).NotNull();
RuleFor(x => x.Email).NotEmpty().EmailAddress().Must((entity, email) => EntityRepository.ExisteEmail(entity.Email));
}
}
I preffer to use Fluent Validation than Data Annotations because it's more flexible and works fine with Asp.Net MVC. It's configured and works fine too. So, I have this ViewModel:
public class EntityViewModel {
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public decimal UnitPrice { get; set; }
public int IdCategory { g开发者_运维问答et; set; }
}
Now, I'm trying to do a ViewModel to make a clean up POST of my entity in some actions (like INSERT, UPDATE) because I'm using nhibernate. I
don't know if it's right to create a validation for viewmodel, because I have my own on my entity, so, how can I do this? How have you been doing
to POST your entity on actions to persist it? How you validate it and post erros on ModelState of MVC?
I'd like to see some code of how to do it, if it's possible.
Thanks everyone!
don't know if it's right to create a validation for viewmodel, because I have my own on my entity, so, how can I do this?
Personally I define fluent validators for my view models. They contain validation rules for simple things like required properties, formats, etc... so that those validation could be handled directly on the view.
Domain models could also contain validations but those will be business validations. Here's the usual flow of a POST controller action:
[HttpPost]
public ActionResult Insert(UpdateViewModel viewModel)
{
if (!ModelState.IsValid)
{
// the surface validation on our view model failed => redisplay the view so
// that the user can fix errors
return View(viewModel);
}
// at this stage the view model is valid => we can map it back to a domain model
// I use AutoMapper for this:
var domainModel = Mapper.Map<UpdateViewModel, DomainViewModel>(viewModel);
// then we pass the domain model to the service layer for processing:
string error;
if (!_service.Insert(domainModel, out error))
{
// something wrong happened on the service layer
ModelState.AddModelError("key", error);
return View(viewModel);
}
// everything went fine
return RedirectToAction("Success");
}
As you can see from this example we are delegating the processing to the service layer. The way this service is implemented is out of any interest or meaning for an ASP.NET MVC application. You could be using NHibernate, Entitiy Framework, call some other services on the could, anything you could think of, all that we should care is that we are handling our domain model ready for being processed and it reports us back errors if any. The way you handle your validation on the service layer is also out of any importance for the ASP.NET MVC application => you could be using Castle Validator, Fluent Validation, Data Annotations, whatever ...
As far as the mapping is concerned there could be an additional step if for example you were updating an existing domain entity. In this case the view model probably doesn't contain all the properties of the domain model because in this specific view for example we allow for editing only some of the properties. In this case the flow would be like this:
// Fetch the original domain model we want to update
var domainModel = _service.Get(viewModel.Id);
// Update only the properties that are present on the view:
Mapper.Map<UpdateViewModel, DomainViewModel>(viewModel, domainModel);
// Pass to the service layer for processing:
string error;
if (!_service.Update(domainModel, out error))
{
...
}
精彩评论