Adding Validation MetaData to Entity Framework Auto Generated Classes

Entity Framework’s Code First is one of my favorite tools, but in my experience it is somewhat uncommon to build a database from scratch. More often you are accessing and editing data in an existing database, or modifying its schema. Thankfully, Entity Framework 6.1 introduced “reverse code first”, which is more officially known as “Code First From Database”. This pattern allows you to choose an existing database, and Entity Framework will generate the appropriate entities and DbContext classes for it. This can also be a great option if you are a fan of code first syntax but still prefer to design your databases through SQL or Management Studio.

When you first start using Code First From Database, however, it won’t be long until you run into a problem. The framework generates correct entity classes for you based on your database tables, but what happens when you want to add additional meta data to these classes? Maybe you want to enforce certain model validation rules that don’t actually exist in the database. How do we add this meta data? Sure, you can add them directly to the classes Entity Framework generates, but if the database changes and you need to regenerate the classes, you’ll loose these updates. There must be a better way! Continue reading

CategoriesASP.NETASP.NET MVCEntity Framework

Creating a Generic Repository with Entity Framework and MVC

Hey there! So, I really like Generic Repositories.  They are a very useful design pattern and are enjoyable to use once configured properly.  However, when I first started learning about this design pattern I found a lot of the tutorials out there to be overly convoluted. The goal of this article is to get you up and running with a simple but powerful Generic Repository.

This tutorial assumes general working knowledge of Entity Framework and is based around MVC, though it could be applied to other frameworks and patterns.

As a side note or disclaimer, most of the code shown here is actually slightly modified code from the open source NopCommerce ecommerce platform.  I think NopCommerce has a great implementation of a generic repository, so why rewrite the whole thing?  The whole point of this in the first place is to avoid rewriting code.  Don’t worry if you don’t even know what NopCommerce is – the code here is stand alone and has no dependency on the platform. I have simply extracted some of it out and simplified it for a tutorial.

Let’s quickly review the purpose and benefits of a Generic Repository. Continue reading


Efficient MVC Redirects Using an HTTP Module

Redirects are a common necessity for websites operating in the real world.  Perhaps your marketing team wants a URL changed, or perhaps there has been an architectural change in your application that impacts its URL structure.  Maybe you’re just experimenting with SEO.  Regardless of the reason, there are a lot of ways to handle redirects in MVC.

The most common way I have seen people handing redirects is inside of an action method of their controllers.  This is probably the easiest and most straightforward way to do things.  You might see some code like this:

public ActionResult About()
    ViewBag.Message = "Your application description page.";

    return RedirectToAction("Contact");

public ActionResult Contact()
    ViewBag.Message = "Your contact page.";

    return View();

There is nothing really wrong with this strategy, especially if you’re okay with recompiling code.  However, this isn’t the most efficient way to handle redirects from a performance standpoint.  The reason for this is because ASP.NET still needs to process the entire MVC framework pipeline, just to have another request start up again.  This is not an article on the ASP.NET Request Life Cycle, but for those who are unaware, MVC Controller instantiation and Action Method invocation actually happen fairly late into the overall request pipeline.  The MVC framework is implemented through the MvcHttpHandler, which gets excuted late in the HttpApplication life cycle events.  We wanted to prevent this Handler from ever being executed.  To do this we can hook into an event that gets triggered before it and handle our redirect there.  You can see a complete list of the events at this link – we need our code to run before the PreRequestHandlerExecute event.  This is the event that begins execute of the MVC handler. Continue reading


General Thoughts on Passing Exam 70-486 (MVC Web Applications)

It’s a beautiful day!

This week I was fortunate enough to pass the Microsoft Certification Exam 70-486, which focuses on web application development with MVC and Azure.  Due to the non-disclosure agreement I signed (digitally) I can’t reveal and specific questions or example from the test, but I can offer some general thoughts and guidance for preparation.

Personally I thought this was a very difficult test, but I admit that I don’t have real world experience with a number of topics the exam covers.  I have worked fairly extensively with MVC, so I thought I would have a great shot at passing.  However, I may have mislead myself a bit.  MVC may be in the title of the exam, which I think led me to believe it would be very MVC centric, but this is not entirely true.  The exam focuses very heavily on related topics like Azure, Authentication methodologies, security, and IIS concerns like routing.  This is no secret – if you look at the official Microsoft exam guide it will tell you as much – but I just want to remind people that straight MVC (views, models, controllers, actions, filters, routing, etc.) is only part of this test.  Subconsciously it’s easy to gravitate towards studying topics you’re already familiar with.

Long story short: The exam has MVC in the name but that’s really only a fraction of the test. Continue reading


Streaming Authenticated Video with Amazon Web Services and .NET

Welcome! This post is designed to alleviate much of the confusion surrounding AWS video streaming, specifically with .NET applications. If you are looking for a complete guide to setting up authenticated streaming for both HTML 5 and Flash from start to finish, this is the place to be. If you are already familiar with AWS, hopefully there will be a few helpful tips along the way.

Note: AWS does feature some fairly helpful tutorials on their own site for setting up basic, public video streaming. However, setting up fully authenticated video with .NET is another story, and a lot of the information out there is fragmented and confusing.

This tutorial is available as both a video and as a written tutorial.

Download PDF version by clicking here.

The contents of the PDF are broken down by section, whereas the video is one segment. The two are almost identical in content, though they may not be organized quite the same. Here are the approximate time marks for each section of the video if you are interested in jumping around:

Setting up a solution for testing: 00:00

Creating the buckets and uploading our resources: 3:00

Adding Distributions and an Origin Access Identity: 6:08

Generating a properly formatted private key and policy for signed urls: 10:50

Generating Signed URLs: 20:05

Adding flash fallback and alternate video formats: 27:53

Check out the full video below: