Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have been spending quite a bit of time thinking about this exact same issue. I am using ninject to inject my repository into my web UI layer so that my web UI only accesses the database through an interface.</p> <p>I am wanting to be able to validate things that access the database such as checking for duplicate names and hence my validation needs to access the injected repository. I think that the best way to do this is to just setup Fluent Validation via the manual method rather than the MVC integrated way. For Example:</p> <p>Create your validation Class (can pass in repository Interface):</p> <pre><code>public class CategoryDataBaseValidation : AbstractValidator&lt;CategoryViewModel&gt; { private IRepository repository; public CategoryDataBaseValidation (IRepository repoParam) { repository = repoParam; RuleFor(Category =&gt; Category.Name).Must(NotHaveDuplicateName).WithMessage("Name already exists"); } private bool NotHaveDuplicateName(string name) { List&lt;Category&gt; c = repository.Categories.ToList(); //Just showing that you can access DB here and do what you like. return false; } } </code></pre> <p>}</p> <p>Then in your controller you can just create an instance of above class and pass in the repository (that ninject would have injected in the controller constructor)</p> <pre><code> [HttpPost] public ActionResult Create(CategoryViewModel _CategoryViewModel ) { CategoryDataBaseValidation validator = new CategoryDataBaseValidation (repository); ValidationResult results = validator.Validate(_CategoryViewModel ); if (results.IsValid == false) { foreach (var failure in results.Errors) { //output error } } return View(category); } </code></pre> <p>Both the above files can live in the Web UI project and you can then also just use the standard MVC DataAnnotations for client side validation.</p> <p>Just thought that I would put this up for comment / help someone.</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.
    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