I have some questions about how is the best way to implement DDD principles with best pratices of asp.net mvc. Actually, I would like to know, how have you been doing the validation (in viewmodel or model)?
I have this DomainModel and ViewModel:
public class Product {
public int Id { get; protected set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Stock { get; set; }
public Category Category { get; set; }
public Supplier Supplier { get; set; }
public string Code { get; set; } // it has to be unique
}
public class ProductViewModel {
public int Id { get; set; }
/* other simple properties */
public int IdCategory { get; set; }
public int IdSupplier { get; set; }
public string Code { get; set; }
}
Ok. The model is mapped with NHibernate and works fine. I want to know, if it's better create a validation for ViewModel or the DomainModel? I mean, when I receve the ViewModel on a action of asp.net mvc, I will validate it, but if I add business rules on viewmodel, won't I doing wrong? I ask this because I know it's better to add these business validation to my domain, but should I do two validations on my post before persist? Look my action on asp.net mvc:
[Post]
public ActionResult Update(ProductViewModel viewModel) {
// what kind of validations should I do here?
if (invalid) {
return View(viewModel);
}
// how can I return the errors to my View?
// Is here any best pratice to transform from ViewModel to Domain instance?
Product product = ???
_repository.Save(product);
return RedirectToAction("Index");
}
Could someone do an example by code?