Comparing object instances with FakeItEasy
Read more →I had the task of writing a new application recently and of course I chose Nancy. One of the many great reasons is the testing capabilites it offers (For more on that see this great series of articles).
The basics of a test with Nancy looks like this:
[Fact] public void Should_return_status_ok_when_route_exists() { // Given var bootstrapper = new DefaultNancyBootstrapper(); var browser = new Browser(bootstrapper); // When var result = browser.Get("/", with => { with.HttpRequest(); }); // Then Assert.Equal(HttpStatusCode.OK, result.StatusCode); }You set up a bootstrapper, this can be your live one or an inherited version of your live one with dependencies changed to mocks for example or use the
ConfigurableBootstrapper.Async Route Handling with Nancy
Read more →I don’t know about you but all I hear is “ASYNC ALL THE THINGS!”, I think this is partly down to its new and shiny and us developers love “the shiny” and partly a lot of the things we do in our applications are I/O based whether that be database or file system.
The problem that comes with the new and shiny bandwagon is you need to understand what you’re evangelising. Making asynchronous methods and executing them with no actual reason will not give your codebase any gains and could actually effect your application’s performance. There is more depth to that argument but for simplicity just remember this, only use asynchronous methods if you are doing some sort of I/O.
It could also be argued that only “use asynchronicity in a web framework if you expect high traffic in your web application”. If you only have 10 requests on a small site you’re not going to benefit from asynchronous execution as there are plenty of threads available to handle your application. If you start hitting 1000 concurrent requests (the default IIS limit) then requests will start getting queued up. If you make your routes asynchronous then any code that is being waited on, the thread that is being used there can be released to process another request thus speeding up the performance of your app and prevent the likely hood of large queues. I will show how simple it is to make your routes asynchronous with Nancy below.
A quick look at Visual Node
Read more →I came across Visual Node a few months ago and was excited by the looks of it. For those that didn’t click that link, it basically brings the power of Visual Studio debugging to a node.js app. You can write your node.js app in Visual Studio, fire up the debugger by hitting F5 and use breakpoints and watches to see what’s going on.
The hipster in me is screaming saying “You should be using Sublime Text and node-inspector for debugging” but to be honest I found it a bit hackety-hack and it seemed a bit odd debugging my server app in Chrome but maybe that’s just something I need to get over. JavaScript is getting a huge surge in popularity recently so its your duty as a developer to investigate this. I want to learn and understand JS better but I always get frustrated with it after 10mins and swear that I’m never going to touch a dynamic language again, “give me a statically typed language every time with a compiler”. I have a bit of a Jekyll and Hyde situation going on that I need to overcome.
Modifying files within Git history
Read more →If you have been doing code changes and committing as you go and then look back at the changes you may see something you don’t like the look of. Assuming no-one has a copy of your code changes you can go back and modify the files at a certain point in time within your commit history.
I use Git Bash by default but the editor sucks compared to Sublime Text so first things first lets setup the Git editor.
For Sublime Text run this in the command line:
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe'"Next up is finding the commit id you want to go back to, to edit:
git logMake a note of the parent commit id of the commit you want to edit.
Why .Net should become independent!
Read more →I recently changed jobs and as usual was at the mercy of recruitment agents. The advert for my job contained things like ASP.Net MVC, Entity Framework & TFS (luckily there were other cool pieces of technology on that list and what the role entailed interested me and once I had joined the company I saw they were open to other tech/approaches that made people’s workflow and output more beneficial to developers as well as the company. In fact I implemented an API written in Nancy on my first day and paved the way for Git in the first week).
My point being that whenever I hear from recruiters or look for jobs all the adverts basically list the full Microsoft stack. I recently heard from a friend who runs his own company that he gave his CV to a recruitment agent and was basically rejected because his .Net experience was not MS based enough. I know his .Net skills are very good but because those .Net skills were put to good use using OSS projects he is unlikely to get a job in the mainstream .Net market.
These adverts usually contain a list of tech/experience similar to: MVC, Webforms, Visual Studio, SQL Server, Entity Framework, WCF, LINQ.
Q: What’s the common denominator here?
A: They are all owned by Microsoft.Q: What operating system do these all run on?
A: Microsoft WindowsQ: What framework and programming language do they run on?
A: Microsoft .Net and C#Spot a pattern?
So lets point out the obvious, the operating system, the frameworks, the language, the tooling and the data storage are all owned and implemented by one company (and they say Apple tries to lock its users in).
Mocking HttpContext with Fake It Easy
Read more →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.
Up & Running with TypeScript and WebStorm
Read more →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.
Using a Markdown ViewEngine with Nancy
Read more →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:
#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/)<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 use Markdown as your preferred syntax. I currently use WordPress for my blog, it’s ok but its quite bloated for probably what I need. I looked into Calepin and Scriptogr.am as alternative blogging platforms but I felt it didn’t quite offer what I wanted but the approach was a good idea. It meant you could write a blog post and simply put the file in dropbox and it would appear on your blog.
Using DateTime in C# and SQL
Read more →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()The Old Development Language Switcheroo
Read more →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 it easier to learn and know what I can do when I have decent intellisense showing me what I can do with my code.