DEV Community

Robin van der Knaap
Robin van der Knaap

Posted on • Originally published at Medium on

1

Setting up FluentValidation for ASP.NET MVC using Ninject

FluentValidation is a powerful library to validate your model objects fluently. Setting up FluentValidation is easy, import the NugGet package to your project and you’re ready to go. It’s possible to setup FluentValidation using an IoC container, which is very useful when your validators contain some dependencies which you would like to remove using dependency injection. In this article I’ll show you how to use Ninject to setup FluentValidation.

There’s a NuGet package for that!

I’m assuming you already have FluentValidation and Ninject set up for your project. There’s just one little package you need, ninject.web.mvc.fluentvalidation, which you also can grab from NuGet. This package contains a custom validatorfactory which puts Ninject in charge of creating instances of your validators.

First you have to create the ninject factory and configure FluentValidation to use this factory instead of the default one. Add the following lines of code to the Application_Start method in Global.asax or if you’re using webactivator in the start method of the fluentvalidation startup class in App_Start:

NinjectValidatorFactory ninjectValidatorFactory = new NinjectValidatorFactory(Kernel);
FluentValidationModelValidatorProvider
.Configure(x => x.ValidatorFactory = ninjectValidatorFactory);
DataAnnotationsModelValidatorProvider
.AddImplicitRequiredAttributeForValueTypes = false;
view raw gistfile1.cs hosted with ❤ by GitHub

FluentValidation is now using the ninject factory for creating instances of validators. The last step is to bind all validators to their implementation. You can do this with one line of code:

AssemblyScanner.FindValidatorsInAssembly( Assembly.GetExecutingAssembly())
.ForEach(match => kernel.Bind(match.InterfaceType).To(match.ValidatorType));
view raw gistfile1.cs hosted with ❤ by GitHub

This method scans the specified assembly (in this case the current assembly) for validators and binds their interfaces to their implementations. You have to add this method to your Ninject startup script.

By now, all validators can take dependencies which are resolved by Ninject.

Happy programming!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay