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

Removing Data Validation from MVC Controllers

Published on May 6, 2015 by Jamie Munro

Problem


You require custom data validation that cannot be accomplished via the built-in data annotation validation attributes - or - your data validation requires access to multiple properties in your model.

Solution


Many times the easy answer is to place the validation inside of your controller. However, I'm a strong believer of placing data validation outside of your controllers and within your data model. This is commonly accomplished by adding data annotation above the properties, such as the [Required] attribute. Another common way is to create your own validation attributes.

This example will use a third way and that is to implement the IValidatableObject interface. This interface defines a Validate function that must be implemented and because it is inside of your data model it has access to all properties within your model.

Read more >

Tags: validation | ASP.NET | mvc5 | 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

ASP.NET MVC 5 with Bootstrap and Knockout.js

Published on Apr 21, 2015 by Jamie Munro

I can't believe 9 months has gone by since I came to an agreement on writing two books with O'Reilly Media! The first book was on Knockout.js which is a great framework that focuses on the Model-View-ViewModel (MVVM) architecture pattern. I finished the initial draft at the end of September 2014. During October and November, I multi-tasked by writing the second book while working through copy edits and multiple rounds of QC on the first book.

The first book official released (in e-book format) in December with the print version releasing early in January.

December, January, and February were all busy months while I was working on the second book. I cannot stress how difficult this book was to write. Each chapter would take days to write, tweak, and finalize.

March, and now April contained more copy edits and QC rounds for the second book. I think the book is about to be finalized and the e-book version should be ready sometime in the middle of May with the print copy following shortly after!

As the title of this blog states, the book is titled ASP.NET MVC 5 with Bootstrap and Knockout.js. The (un)official back cover reads as follows:

"Bring dynamic server-side web content and responsive web design together to build websites that work and display well on any resolution, desktop or mobile. With this practical book, youíll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.jsóthe JavaScript implementation of the Model-View-ViewModel pattern.

Author Jamie Munro introduces these and other related technologies by having you work with sophisticated web forms. By the end of the book, experienced and aspiring web developers alike will learn how to build a complete shopping cart that demonstrates how these technologies interact with each other in a sleek, dynamic, and responsive web application.

  • Build well-organized, easy-to-maintain web applications by letting ASP.NET MVC 5, Bootstrap, and Knockout.js do the heavy lifting

  • Use ASP.NET MVC 5 to build server-side web applications, interact with a database, and dynamically render HTML

  • Create sleek and responsive views with Bootstrap that render on a variety of modern devices; you may never code with CSS again

  • Add Knockout.js to enhance responsive web design with snappy client-side interactions driven by your server-side web application"


I think this does a great job of describing why I chose these three technologies for the book and how they come together allowing you to easily build dynamic and responsive websites.

While writing these two books I jotted down a lot of ideas for examples. Unfortunately (or fortunately), I was unable to include them all into the books. So over the next little while, I will work to bring them as examples on my blog. Stay tuned. If you have any questions about the books, feel free to post questions/comments here on my blog or find me on Twitter @endyourif

Read more >

Tags: ASP.NET | JavaScript | KnockoutJS | Bootstrap

Speeding up my unit tests

Published on Apr 28, 2014 by Jamie Munro

I hope this isnít too extremely obvious, but I found that I had to take a step back and re-examine my unit tests to find this simple improvement to speed up my unit tests.

When I wrote about how Iím hooked on test-driven development (TDD), the example in that post was too simple and time savings are not noticed.† However, letís dive in to something a little deeper where we have a full class to test oppose to a single internal function.

Read more >

Tags: ASP.NET | Theory | fizzbuzz | tdd | Testing | testing

Advanced Automatic Ninject Bindings

Published on Jan 13, 2014 by Jamie Munro

Automatic Ninject Bindings

Published on Jan 7, 2014 by Jamie Munro

Automapper Performance Testing

Published on May 27, 2013 by Jamie Munro

Why the Repository Pattern

Published on May 20, 2013 by Jamie Munro

The Best Way to Prevent SQL Injection

Published on Sep 19, 2012 by Jamie Munro

What I Learned This Summer

Published on Sep 7, 2012 by Jamie Munro

MVC 3 Routing Example

Published on Oct 17, 2011 by Jamie Munro

20 Recipes for Programming MVC 3

Published on Oct 12, 2011 by Jamie Munro