Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Clean up post and validating ViewModel to convert to Domain instance, how?

$
0
0

Hello Guys, I have some questions about how is the best way to use fluent validation with best pratices of asp.net mvc. Actually, I would like to know, how have you been using Fluent Validation.

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. 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:

private IProductRepository _repository;

[Post]
public ActionResult Update(ProductViewModel viewModel) {

 // what kind of validations should I do here? 
	
 // how can I return the erros to my View?
	
 // Is here any best pratice to transform from ViewModel to Domain instance?
   Product product = ???

   _repository.Save(product);
	
   return RedirectToAction("Index");
}

Thank you guys!

 


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>