Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm using FluentValidation for DataBase validations. just pass the Validation class the session in the Ctor. and do the validation inside the action something like:</p> <pre><code>var validationResult = new ProdcutValidator(session).Validate(product); </code></pre> <hr> <p><strong>Update:</strong> Based on your example I add my example...</p> <pre><code>public class CreateProductViewModel { public string Name { get; set; } public decimal Price { get; set; } } public class CreateProductViewModelValidator : abstractValidator&lt;CreateProductViewModel&gt; { private readonly ISession _session; public CreateProductViewModelValidator(ISession session) { _session = session; RuleFor(m =&gt; m.Name).NotEmpty(); RuleFor(m =&gt; m.Code).Must(m, Code =&gt; _session&lt;Product&gt;.Get(Code) == null); } } Controller: public ActionResult Create(CreateProductViewModel model) { var validator = new CreateProductViewModelValidator(); var validationResult =validator.Validate(model); if(!validationResult.IsValid) { // You will have to add the errors by hand to the ModelState's errors so the // user will be able to know why the post didn't succeeded(It's better writing // a global function(in your "base controller" That Derived From Controller) // that migrate the validation result to the // ModelState so you could use the ModelState Only. return View(model); } var product = new Product { Name = model.Name, Price = model.Price }; repository.AddProduct(product); return RedirectToAction("Index"); } </code></pre> <hr> <p><strong>Second update:</strong><br> If you insist using parameterless constructor you will have to use some <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow">Inversion Of control container</a>, a static class that is something like the Factory of your objects. use it like this: </p> <pre><code>public class CreateProductViewModelValidator : abstractValidator&lt;CreateProductViewModel&gt; { private readonly ISession _session; public CreateProductViewModelValidator() { _session = IoC.Container.Reslove&lt;ISession&gt;(); RuleFor(m =&gt; m.Name).NotEmpty(); RuleFor(m =&gt; m.Code).Must(m, Code =&gt; _session&lt;Product&gt;.Get(Code) == null); } } </code></pre> <p>You can find many IoC containers, most famous are <a href="http://www.castleproject.org/container/" rel="nofollow">Windsor</a> and <a href="http://ninject.org/" rel="nofollow">Ninject</a>, You will need to register- instruct the container once to resolve all the ISession to return your's session object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload