Unit Testing Data Validation with MVC

Published on May 11, 2015 by Jamie Munro

Problem


You've added some data validation and you want to test it out in action. The data validation can be standard data annotations, custom data attributes, or data validation implemented via an IValidatableObject.

Solution


Unit testing data validation can be accomplished by creating a ValidationContext in conjunction with the Validator helper class. To demonstrate how this is accomplished, I will use the ValidationModel class I created in the Removing Data Validation from MVC Controllers. As you may recall this class contained an IValidatableObject that ensured if the boolean FixedQuantity was true, the MaxQuantity property must be null.

Read more >

Tags: validation | ASP.NET | mvc | Testing | unit testing | ivalidatableobject | validationcontext

Integrating Automapper with an MVC Result Filter

Published on May 4, 2015 by Jamie Munro

Problem


On a variety of actions inside your controllers you call the Automapper NuGet Package to convert from one model to another and you do not want to continue repeating this code each time.

Solution


Applying an MVC result filter to the action can execute the Automapper code after the action has been processed by the MVC framework and before the view is rendered.

Please note this is an excerpt from Chapter 9 of my ASP.NET MVC 5 with Bootstrap and Knockout.js book. In this chapter I provide a brief overview of the 5 different MVC filters and then over the next two chapters provide detail examples of each. The following post is leveraging the example of a Result Filter.

Read more >

Tags: ASP.NET | mvc | automapper | mvc5

Advanced Automatic Ninject Bindings

Published on Jan 13, 2014 by Jamie Munro

Problem


You have a common interface that is used by multiple concrete classes and you donít want to specify each Ninject binding manually.

Solution


The solution to this problem is very similar to my previous post on Automatic Ninject Bindings because it also leverages the NuGet package Ninject.extensions.conventions.

In this example, letís assume you have a single interface called IService<T>.† This interface contains a generic reference to a class or object (e.g. a database model).† Then you have multiple classes that implement the interface.† E.g. UserService : IService<User>.

With the following line of code, all of your classes that implement the IService interface will automatically bind to the corresponding class:


 ††††††††††† var kernel = new StandardKernel();

 ††††††††††† kernel.Bind(x =>

 ††††††††††† {

 †††† †††††††††††x.FromThisAssembly()

 ††††††††††††††††††† .SelectAllClasses()

 ††††††††††††††††††† .InheritedFrom(typeof(IService<>))

 ††††††††††† ††††††††.BindSingleInterface();

 ††††††††††† });

 

Read more >

Tags: Uncategorized | ASP.NET | c# | mvc | ninject

Automatic Ninject Bindings

Published on Jan 7, 2014 by Jamie Munro

Problem


You have a large project with many interfaces and many different concrete implementations.† Because of this, managing every single Ninject binding is becoming challenging and time consuming.

Solution


By leveraging an additional Ninject NuGet package called Ninject.extensions.conventions, you can write a single line (wrapped over several for readability ;) that will manage all of your Ninject bindings.


 IKernel kernel = new StandardKernel();



 kernel.Bind(x =>

 {

    x.FromThisAssembly()

    .SelectAllClasses()

    .BindDefaultInterface();

 });


Read more >

Tags: ASP.NET | c# | mvc | ninject

My First Node Application

Published on Oct 4, 2012 by Jamie Munro

I have the unique pleasure of being a technical reviewer for an upcoming book by a former co-worker of mine.† Since it will be his second book on Node (his first being a co-author of Node Up and Running), I thought I should probably get a feel for using Node.† I've of course read some of the documentation and have a thorough understand of JavaScript already, as well as event driven programming Ė working on some very large MMO projects.

I thought I would share how easy it is to really and truly get up and running with Node.

Read more >

Tags: JavaScript | node | express | mvc