Introducing QuickModules for ASP.NET MVC

INTERNET ALL THE THINGS!
Do what he says! He has a giant cotton bud in his hand!

Like you I often spend my weekends writing REST APIs for fun. And like you I am often frustrated by the amount of server-side code I need to write just to expose some data to JavaScript. Boilerplate, boilerplate, boilerplate. Ugh! So this weekend, in between bouts of erratic and unsatisfying jetlag-sleep, I decided to try and simplify the task of exposing an API to JavaScript. Introducing “QuickModules for ASP.NET MVC”, available now as a NuGet package!

QuickModules

QuickModules provides you with a basic, modular framework for hanging API endpoints from. Internally using the Managed Extensibility Framework (MEF) to load modules, QuickModules gives you the ability to build encapsulated CRUD services without needing to write, or understand, the usual plumbing.

Consumer Experience

As a software developer, and a human (vaguely), I like an easy life, so I’m all about the consumer experience when publishing code or frameworks (see my JavaScript business-object CRUD framework at http://richstokoe.github.io).

In order to ease any concerns you may have I will run through all the steps required (there are very few) to get QuickModules into your MVC application.

Step 0: Open Visual Studio (Duh!)

Step 1: Create a new MVC project

quickmodules_1

 

Step 2: Choose an Empty, Basic, Internet, Intranet or SPA template (actually, any will do, but these make the most sense)

quickmodules_2

 

Step 3: Notice how barren your project is without QuickModules. 🙁 (I’ve chosen a Basic template here)

quickmodules_3

 

Step 4: Install QuickModules using NuGet: Right-click on the References node in your Solution Explorer tree and click on “Manage NuGet Packages”:

quickmodules_4

 

Step 5: Search for “QuickModules” and choose “Install”. (Don’t worry if the details and version numbers look a little different on your PC, this is a screenshot of an early work-in-progress package).

quickmodules_5

 

That’s it! Hit F5 to run your application. If you’ve chosen an Empty or Basic template you’ll probably get a 404 Not Found error. Try navigating to the sample “Nearby Restaurants” module by adding /QM/Get?ActivityType=NearbyRestaurants to your URL (QM being shorthand for QuickModules of course!).

You should see a list of food places from The Simpsons as a wonderful, unformatted, JSON object array, ready to be consumed by your JavaScript:

quickmodules_7

 

But Wait There’s More!

Built into the framework (and optionally supported by your modules) is paging, and filtering. All for the low, low price of $9.95.  (Just kidding, it’s free). Use the LINQ-style Skip and Take arguments to control paging:

quickmodules_8

 

And use the Filter argument to narrow the result set down:

quickmodules_9 Continue Reading “Introducing QuickModules for ASP.NET MVC”

Learn to Code: Chapter 2 – Black Tie Function

She only wants you for your brain…

This is the third part of my Learn to Code series. The previous part is here: Chapter 1 – Meet Bob.

 

See Bob. See Bob Run. Run Bob, Run!

We last spoke about making Bob run 5 miles. We were going to do that by performing an action – a “function” in programmer parlance – called “run”, and give the number 5 (meaning the miles he is to run) to the function. We do that like this:

bob.run(5);

 

The problem is, we haven’t said what “run” actually means or does. Later we’ll use code that already exists but this time it’s all up to us. We’re going to have to write this “run” function ourselves. Again, this is a very easy thing to do once you understand the pattern you use.

function (miles) {
}

 

Here we say we want to create a “function” and that the function will take some information in brackets that we can refer to by the name “miles”. You see, when information (such as the number 5) is “passed” into the function, it is important that we can get to it because we can use it to change how the function operates. You can write a function once, but pass different things to it each time and it will behave differently every time. That is the real beauty of programming.

You will notice there aren’t any semi-colons in the bit of code above, this is because we are using a pattern that the computer will understand – those curly brackets (called “braces”) tell the computer when you’re starting and stopping to describe your function. However, when we write code inside those braces, we still must use semi-colons to differentiate one .

The devil is very much in the details when programming. Different programming languages have different requirements for WHERE those curly braces go. Sometimes they should be on the same line as the start of your function, and sometimes they go on the next line down. Like this:

function (miles)
{
}

 

But the language we are learning needs them to be on the same line as the word “function”.

Here’s an example of combining curly braces and semi-colons. Gosh it’s getting complicated… but stick with it, this is as complex as it gets. let’s write some code inside the function that uses that number 5 that was passed in, which we can refer to as “miles”:

function (miles) {
    return miles + miles;
}

 

This is now a fully-functioning function. If you’ll pardon the pun. Although… not a particularly useful one. Yet.  Continue Reading “Learn to Code: Chapter 2 – Black Tie Function”