Mocking HttpContext with Fake It Easy

Lets start with the conclusion first and say “use Nancy for your web applications and APIs” as its brilliant!

If you want to continue reading lets crack on.

I’m currently working on a ASP.Net MVC project and one of the controller methods writes directly to the Response, eg. Response.Write(“How will I mock thee?”);

Having moved over to xUnit and FakeItEasy recently I wanted to write a unit or integration test depending how you see it to assert against the Http Response.

Doing this is no easy feat with MVC (with Nancy its all done for you) and you have to mock a lot of things. I’m hoping that in later releases this will be fixed because I know that ASP.Net Web API has made things a bit easier for testing (and wrote a testing library for it) so I assume the two projects will use bits of each other or their roadmap will merge.

I found that there a quite a lot of samples with Moq but nothing for Fake It Easy(FIE) so I checked in at the FIE [...Read More...]

Up & Running with TypeScript and WebStorm

I love my iMac and I’m on a mission to find a language I enjoy that I can use my Mac for (no Windows fan boy jokes please). There’s something in my mind I associate with work and my Windows laptop. Therefore, I don’t feel to excited about getting my laptop out of my bag in the evenings/weekends to play with other stuff.

As I want to broaden my knowledge I wanted to find something ideally statically typed (although I’m currently looking into Python) that would work on OS X. I’ve said previously that JavaScript seems the way to go in my current situation so I thought I’d take a look at TypeScript and also use WebStorm from Jetbrains as my IDE seeing as I’ve heard so many great things about them and their products (don’t worry I use ReSharper).

TypeScript

So I went over to TypeScript’s website and followed the Hello World type code examples under the “Learn” tab.

After understanding the basics of it and wanting to learn more I spotted this demo [...Read More...]

Using DateTime in C# and SQL

I’m sure there are millions of blog posts out there that already discuss this but I think its worth noting down even if its just something for me to remember.

Store your datetimes in UTC format into the database. Unfortunately this mean executing something like:

myObject.ExpiryDate = TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(“timezoneid of users”)

In every central place where you update/insert DateTime values on your objects you will need the above.

When you display any DateTime information it must display as a local datetime value. You can do this by using myObject.ExpiryDate.ToLocalTime()

An example of this is if you stored a DateTime that was converted to UTC and it stored April 8th 14:30 2013, when a user in USA was given the date in their web application they would see it as April 8th 10:30 but UK users would read it as April 8th 15:30

If you have an existing application you need to make sure the server side logic regarding date storage converts to UTC and that your view layer converts to local time.

Its risky but if your users are all [...Read More...]

Using a Markdown ViewEngine with Nancy

Whilst using stackoverflow.com and Github gists I’ve become a frequent user of Markdown.

For those of you that don’t know what Markdown is, its essentially a shorter/cleaner syntax that can be parsed to produce HTML. Below are a few examples:

1
2
3
4
5
#Hello World!
##You’re awesome!
The quick brown fox jumped over the lazy coder
What the **hell** is this?
This is an [example link](http://example.com/)
1
2
3
4
5
<h1>Hello World!</h1>
<h2>You’re awesome!</h2>
<p>The quick brown fox jumped over the lazy coder</p>
What the <strong>hell</strong> is this?
This is an <a href="http://example.com/"> example link</a>

You can see more examples in the earlier link.

When you’re writing a blog post or a lengthy page in your web app with lots of HTML it maybe easier to [...Read More...]

The Old Development Language Switcheroo

As a C# developer I think I’m pretty safe in saying that its not going away any time soon however, its my opinion that to not become irrelevant you need to have options. Some may argue that its better to be master of one than a Jack of all trades but lets just say you can’t find your next job in the primary language you want to work in. What do you do?

I have looked more into JavaScript recently and with that comes server and client scope for the use of the language so with some understanding of JS that would be probably the best bet for me however, as much as this may annoy some I think I actually prefer statically typed languages.

Now I know there is TypeScript that I could use for my node.js apps but I see that as more of a workaround and not a core feature of JavaScript. I’m not saying anything bad against JS and the libraries/frameworks that I have used I like but I just find [...Read More...]

The many approaches to Entity Framework

I recently had a need to look into using Entity Framework (EF) for a ASP.NET MVC project. In the past I have always used PetaPoco as my ORM of choice and with hearing nothing but bad things about EF I was a little sceptical. There are various ways to use EF, Code First being one of them and the easiest from what I can gather and luckily the approach I needed to get up to speed on. This means you can define your model in code and EF will turn that into tables in your database.

The way I was going to see how EF could be architected in an application was to create a MVC application that provided CRUD capabilities for Customers, Orders and Products. Nothing complicated but something enough to see how EF could be fitted in with a MVC application. I would also like to use a unit of work pattern such as instantiate a model class, set some properties and call a save method. I would also [...Read More...]

Evaluating KnockoutJS and AngularJS – Part 2

In Part 1, I described how I was using the demo tutorial from SammyJS to get a better understanding of AngularJS and KnockoutJS.

In this blog post I will focus on what I found when using KnockoutJS.

Again if you just want to get to the code then feel free to take a look here for Angular and here for Knockout.

Knockout

Firstly Knockout should be commended on their documentation and online tutorials. Their website tutorials follows a step by step approach and once you have completed each step you can click through to the next section. You can also leave the tutorial and come back to it and it will remember where you left. There is great support in the KnockoutJS room in Jabbr and I’d like to thank David Spörri for answering my newbie JS and Knockout questions.

Knockout also uses HTML elements and attributes like Angular to determine behaviour but Knockout’s approach is more HTML5 in that the most common usage is using the data-bind attribute. For example, the below will use the artist [...Read More...]

Evaluating KnockoutJS and AngularJS – Part 1

As I stated in my earlier post “JavaScript is the future…maybe” so with that in mind I had to brush up my JS skills and get more involved with the language’s core concepts so after watching some videos and reading some articles I was ready to look at KnockoutJS and AngularJS.

Before I actually looked at these two I spent some time with SammyJS but realised afterwards it was mainly MVC based and not around 2-way binding that Knockout and Angular offer. However, I really liked it and it seemed very familiar and easy to use, the reason being it was inspired by Sinatra which we all know Nancy was also inspired by and we also know how much I like Nancy!

Getting to grips with any language or frameworks is tricky and the best way to do it is to write an application using it. The next difficult thing to overcome is an idea for writing an application. ToDo list’s are very common with JavaScript frameworks and there is a whole website for [...Read More...]

Using AngularJS/BackboneJS in Windows 8 JavaScript app

To help me expand my JavaScript knowledge as I said I would in my previous post I thought I’d write a Windows 8 application using JavaScript.

After following a few “Hello World” tutorials from Microsoft I thought I’d take a look at the ToDo list demos shown at TodoMVC.com.

This website/Github repository takes the ToDo demo and implements it in all the various JS frameworks and libraries out there. As I said previously its a minefield.

Anyhow, I thought I’d start with Backbone, copy the files, add the references to WinJS and hit F5 and bingo. However, I got the below error:

Unhandled Exception

A bit confused, I googled the issue and found that Microsoft have implemented security principles in WinJS applications to prevent un-sanitized markup to your page. So any time you might dynamically add some HTML to your page your application will throw an exception.

To get around this issue you can wrap your dynamic content calls with execUnsafeLocalFunction

You can read more about that from Microsoft’s documentation here

Here [...Read More...]

JavaScript is the future…maybe!

I’m not one for New Years resolutions but I thought it was time I looked at JavaScript more in depth.

I looked at Node.js a while back and found it very interesting and I probably need to go back to it. Over the last month or so there has been a large discussion about async in .Net frameworks and there appears to be a lot of misunderstanding about it (and lets leave it at that, I don’t want to start another flame war) but the thing we can definitely say with Node.js, well JavaScript to be fair is that it is perfectly asynchronous and non-blocking.

As a web developer I have used JavaScript from the early days of Response.Write moving onto frameworks such as script.aculo.us and MooTools and finally ending up with jQuery which has come pretty much a standard these days so my JavaScript skills are not completely new.

However, there has been a large push to use JS more and more for rich user friendly applications with things like KnockoutJS, AngularJS and BackboneJS on the [...Read More...]