Check arguments for null in C#

I had a comment recently from ‘The Dag‘ asking if I could create some code which would check to ensure that none of the parameters for a methods were null. While this is quite a simple piece of code I thought I would reply in a post rather than answering in the comments as it is more likely to be found by anyone else who is looking to do the same.

The request is to allow for any number of parameters to be passed in and they all be checked, the suggested format by The Dag was;

Guard.NoArgumentsNull(() => arg1, arg2, arg3);

I didn’t keep to the format suggested exactly but I feel that the result meets all of The Dags requirements fully.

[Read more...]

NodaTime: What date time is it?

If you have ever developed an application that is time sensitive you’ll appreciate just how much of a nightmare it can be to get right. Do you handle users in a different time zone to you? Do you handle day light savings? Do you handle leap seconds/days?

Time_Flies_by_janussyndicate

When we develop code that is error prone or we know it requires special care over making sure that we got the implementation correct we tend to check that it works through the use of unit tests. The problem that we face though is that writing unit tests that confirm date time specific functionality is correct can be problematic and potentially so troublesome so that you to avoid writing tests for that area of the application.

I’ve recently learnt about a library developed by Jon Skeet that deals with date times and while I’m sure it does plenty more than this the functionality that really got me excited is that it is designed in a fashion to allow for us to unit test date times more easily. To demonstrate how this is achieved I will run through an example that Jon has provided where we are testing a very basic licensing system.

[Read more...]

Logging SQL Queries in MVC With Entity Framework 6

As developers we’re inclined to want to understand what is happening under the hood or at least know that we can check if we wanted to. This is the case with Entity Framework, which performs some magic to provide access to a database and allowing us to perform actions against it. While 99% of the time we are more than happy to treat Entity Framework as a black box, sometimes we like to look under the covers and see what the Entity Framework is doing. In this article I will show you how to log the SQL queries that are being performed by the Entity Framework in an ASP.NET MVC application.

Entity Framework Logo

 

[Read more...]

Local NuGet Repository

NuGet is an amazing addition to Visual Studio, it’s one of those features that you wonder how we managed without it before. The only problem is that it requires that there is an active Internet connection to use it. This means by default you’re unable to use NuGet when you’re developing out and about.

NuGet Logo

One thing that NuGet does to avoid making unnecessary downloads of packages is it stores a cache of packages that it has downloaded, allowing it to confirm that it already has the latest version of the package and only download the package if there is a newer version available. This cache is stored in the same format as a NuGet repository, most likely to allow for reuse of the code that identifies a package on a server. What this means for us is that you’re able to point your NuGet Package Manager at the cache location and it will pick up the packages that are in the cache, allowing you to install them in a new project that you’re working on.

[Read more...]

Magic Strings: How to avoid them in C#

One of my least favourite things about the .NET Framework is that there are plenty of places where it expects you  use magic strings . These make maintaining your application harder as refactoring through Visual Studio will not pick up these references, which then get left behind with their original value. This blog post is to mostly share a very useful code snippet with you that helps us to avoid using magic strings and instead uses lambda expressions to specify the name we want.

Avoiding magic strings by using expression trees

[Read more...]

Get deep nested values without worrying about null

Failing to handle null reference’s is one of the most common bugs in an application, this happens when a developer assumes that the a value will always contain an object even though it could be null. One of the nasty things about checking for nulls is that if you’re looking to retrieve a value from a deeply nested value would end up with a large number of nested if statements or one rather long if condition. In this blog post I am going to show you a method that I have developed to avoid the need for these,  allowing us to write much cleaner and more readable code.

Null Reference Exception

We’ve all done it, you’ve written code where you either forgot or was to lazy to check that an object wasn’t null before trying to perform some action on it, whether that be call a method or retrieve a value. This is easily done and can be difficult to identify when visually inspecting your code, I dread to think how many times I have neglected to include checking for nulls in my code. To check that we aren’t going to cause a null reference exception, we need to go through each step checking that the returned value isn’t null before continuing on to the next, this leads to lots of nested if statements, which in my opinion aren’t very pretty. After coming across a requirement  to do this deep if nesting with one of my personal projects I decided that if there had to be an easier way and if there wasn’t an existing way I’d come up with one, which has lead to the creation of a pretty unimaginative class called ValueRetriever.

[Read more...]

Validating XML against XSD schemas in C#

I’m currently working on an XML File Explorer application, which I intend on highlighting XML files which do not conform to an XSD schema, as part of this I have created a class for containing the details of performing XSD validation against an XML document, which I thought I would share with you (although the XML File Explorer will be open source).

[Read more...]

Two Factor Authentication in ASP.NET MVC

It’s becoming more and more common for websites to provide the capability of having two factor authentication as part of you login process. Google, GitHub, Hotmail and Dropbox just to name a few of the organisations that have embraced two factor authentication and provided it as an optional security measure. In this article I will demonstrate how to implement two factor authentication in your ASP.NET MVC application using Google Authentication.Two Factor Authentication devices
[Read more...]

Using reCAPTCHA in your ASP.NET MVC app

The Internet is full of bots. There’s no denying it, bots have been created to perform tasks on the Internet for many purposes, a good example is Ticketmaster, who have calculated that approximately 60% of all bookings are made by bots (src nyt).

Example of a reCAPTCHA

One of the most common practices for fighting back against bots is by adding what is known as a ‘Completely Automated Public Turing test to tell Computers and Humans Apart’ or CAPTCHA for short. These are commonly either an image or audio clip with letters and numbers that the user has to type into the computer to prove that they are human. One of the most popular versions of the CAPTCHA is reCAPTCHA, which is provided free of charge by Google, this is what we will be using to add a CAPTCHA to our registration page, to avoid bots registering for our website.

[Read more...]

ASP.NET MVC4 AuthorizeAttribute throughout app

If you’re developing a website where there should only be a couple of publicly accessible pages in ASP.NET MVC, then at first you may try to go through all your controllers decorating them with the AuthorizeAttribute. Although you will still want to allow anonymous users access to the login page otherwise anonymous users can never become logged in users, in MVC4 you can specify that you want to allow anonymous access to a particular action within a controller which requires the users be logged in by the decorating the actions that you want to be publicly accessible with the AllowAnonymousAttribute

Control access to your webpages with the AuthorizeAttribute

[Read more...]