Sponsored by Gluten Free Waffles and Sliced Bananas

Tuesday, April 2, 2013

ASP.NET Web API, Autofac & Self-Hosting

8:40 PM Posted by Tyson Nero , , , , 23 comments

Well Structured Projects

Separation of concerns comes into play even when laying out the projects in your code solution. For instance, you may have separate projects for your contract classes (DTOs), data layer, service layer, and sometimes even finer granularity among those.

When building such projects using ASP.NET's Web API for example, you may be tempted to lump everything into a single MVC 4 project, but each code solution has different needs and requirements, and even this type of solution can be broken out into multiple projects.

Self-Hosted Web API w/ external Controllers

Let's say you are building an IIS hosted Web API solution. Mainly, all of the clients connecting to your API will be web applications using AJAX. However, your API is also expected to have the ability to expose itself to other .NET clients who may be using the HttpClient object to perform operations. In this case, you may decide to break your solution out into 2 separate projects (I'm simplifying for brevity):
  1. Web Host Project (Empty MVC 4 Project)
  2. API/Service Project (Class Library)
The Web Host Project will be that which actually hosts the service, and the API/Service Project will be the implementation.

I highly recommend taking some time to review the PingYourPackage open source project on GitHub for a reference to such an implementation.

Implementing a Self-Hosted Web API with external assembly dependencies

API Host

Create a Console Application project to host the Web API by following this tutorial: Self-Host a Web API.

Controllers & Service Implementation

Create a new Class Library project for your controllers and service implementation
Implement a controller that inherits ApiController (ProductsController.cs)

Reference AutoFac.WebApi

Add an AutoFac.WebApi reference to your your Class Library project. This can be done via NuGet.

Wire Up AutoFac

You basically have two options here...

1) Add the AutoFac.WebApi dependency registration code to Program.cs.

2) Or, optionally create a configuration class (AutoFacConfig.cs). This will most likely be created in your service implementation project so any host could use it. Be sure to register your controller within this code.

With this, you will be able to call AutoFacConfig.Initialize(config) within your host.

Final Implementation

Friday, March 22, 2013

Event Namespacing with jQuery

2:21 PM Posted by Tyson Nero 2 comments
It's a standard practice to bind and unbind events using general event types such as "click". You would probably do something like this.

However, there are cases, especially with third party libraries for instance, where event collisions could occur. Let's say you bring in a plugin that does some type of magic on a button using click events. You too want to do your own magic and also bind to the click event. Time passes, and for some reason, the plugin gets destroyed and unbinds the click event. Not only has it cleaned up itself, but your event handlers are gone too!

The solution is event namespacing, which is simply adding class names to your events:

Event namespacing comes in extra handy when writing your own plugins. Remember, there are many other plugins out there, and just like writing C# code, you want to define unique namespaces that will hopefully not collide with other libraries.

Organization Just Doesn't End With Code

10:49 AM Posted by Tyson Nero 2 comments

This morning, as I reached into my dishwasher to put some more items in, I noticed a broken glass. It seems all the items on the top shelf weren't loaded in an organized manner, and a coffee mug handle had been pushed into a glass when an unnamed person tried to overload the area.

Well, I proceeded to remove the broken glass and reorganize the top shelf to avoid any other catastrophes of such magnitude. Then, I noticed my finger was bleeding and the situation was brought to a whole other level.

Several minutes later, my unnamed spouse entered the room to notice the broken glass, a reorganized dishwasher, and some bloodshed on a few other items in the dishwasher. With a quick glance she turned to me and said:

“Maybe if you hadn't reorganized the dishwasher, you wouldn't have got blood on everything.”

Monday, March 18, 2013

Simple Reverse Iterations in C#

11:05 AM Posted by Tyson Nero 2 comments
There are some situations that require the use of iterating in reverse. It's not such a hard concept to iterate backwards, however, there are cases where using the syntax of a for loop isn't the most satisfactory method. Thankfully, in .NET 3.5 the Reverse() extension method was added, which allows you to simply use a for each in cases where you want minimal code pollution such as an ASP.NET MVC view.

Let's say you have this simple array:

 string[] names = { "Barry", "Bob", "Bill" };  

Here is an example of using a for loop to do a traditional reverse iteration:

 for (int i = names.Length - 1; i >= 0; i--)  

Now, here's an example of using the .NET 3.5 Linq extension:

 foreach (var name in names.Reverse())  

Note: Using the Reverse() method is actually slower. So, use with caution when speed at the lowest level is not a factor.

Tuesday, February 19, 2013

Adding and Removing Classes Using jQuery's .toggleClass()

8:26 AM Posted by Tyson Nero 1 comment
I wrote a function to toggle the states of some navigation buttons on a form submission page that has multiple tabs.

If you are on the first tab, the Previous button is disabled.

If you are on the last tab, the Next button is disabled and the Save button turns green.

My first implementation was around 20 lines and had two sets of if/else blocks. There wasn’t anything truly wrong with it, but I felt it could be refactored by somehow removing the need for if/else conditions.

So, I did a little research and found that jQuery has a toggleClass function that you can pass in a Boolean value to determine whether the class should be added or removed. I then converted my if/else conditions into single line expressions that returned a Boolean which I could then pass to the toggleClass function. Here is the final code.

The key in refactoring this code was leveraging an existing function from a library that did the work for me. At first, I didn't know it existed, but after a Google search and reading the jQuery documentation, I was able to implement and refactor in little time.