NUnit test method snippet

As I develop, I write quite a few unit tests. And to speed up the development I’m using code snippets. One of them most useful snippets for testing is to create test method. So I’ve came up with ntest snippet that will give you decorated public method.

Type ntest

ntest snippet for NUnit test method

and hit TAB. And you get ready test method:

Test method decorated and ready to go

Then you’ll need to type the test name and hit enter – your cursor will be placed inside of the curly braces. Here is the snippet file: ntest.snippet

To install the snippet in Visual Studio, you’ll need to go Tools -> Code Snippets Manager. Pick a folder where you would like new snippet to be stored. Click “Import..” button and point to the unzipped ntest.snippet file. Done. Profit!

More about creating your own snippets read on Microsoft Walkthrough: Creating a Code Snippet
Code Snippets Schema Reference

Raven DB

I’ll probably start a series of posts related to RavenDB, since I’m learning to use it. And here is the first one.

I’ve been using SQL queries most of my programmers life and I’m so used to ask database things like this: show me all projects that user has access to.

Or Select * from project where ProjectId in (select ProjectId from UserPermissions where UserId=1)

When you try to use the same logic in RavenDB, it throws you NotSupportedException and no hints of how to do it. Luckily, RavenDb is popular enough to have a good hit score in Google and I managed to find the answer on the interwebs pretty quick.

Here is the way to search in a subset:

#!csharp
var locations = ravenSession
    .Query<Location>()
    .Where(l => l.ProjectId.In<string>(currentUser.ProjectIds))
    .ToList();

And you’ll have to add using Raven.Client.Linq; to your class, so In<> becomes available.

Remove Server http header from ASP.NET MVC Application

There is an opinion in the online community that HTTP Headers in your application must not be giving out information about your site. But in every StackOverflow question about removing headers, there always will be someone saying “why bother?”. Well.. don’t bother if you don’t want to. You are quite right, there are ways to detect what server and technologies are used to serve the site. But I do not know any of these techniques, so I’ll keep removing headers from my applications.

Also it seemes that IIS insists on providing Server header with every request and there is no way to remove it via web.config. To get rid of this header I have seen people going out of their way with HTTP Modules and UrlScan. But in MVC4 (not sure about version 3 – never tried it there) and IIS 7.5 you can easily do that in you Glabal.asax.cs file.

In the application request life-cycle there is EndRequest() event that can do what we want:

    protected void Application_EndRequest()
    {
        // removing excessive headers. They don't need to see this.
        Response.Headers.Remove("Server");
    }

Much the same way you can remove any other header from the reply: X-Powered-By, X-AspNetMvc-Version and etc. But for these there are legit ways to do that through web.config.

jQuery Unobtrusive Validation of dates in Chrome: US vs GB format

UPDATE 31 Jan 2013: We have encountered this issue after we have updated our MVC3 project to MVC5. A year ago we have moved MVC3 to MVC4. And suddenly Chrome started to insist on incorrect date format, but everywhere else we have set Globalise locale to be en-Gb. The only other alternative was to disable date validations which can also work – I trust server side validation more than I trust JavaScript ;-)


Today I came across the magical problem in Chrome – it ignores locale information for date format. So it always uses US format: “mm/dd/yyyy”. But largest part of the world is not using this messed up month-first approach (wink-wink).

Continue reading

Mysql backup batch file

Whenever you work with MySQL databases, it is a good idea to do backups. I have used mysqldump --all-databases for ages for that.
But all the databases are dumped into one huge sql file. And when it comes to restoring of data, you are in trouble. You’d still get your backup, but it’ll take you a long time to separate out only one database (think about 20+ databases running on the server). So it is a good idea to separate databases into separate files and there is no solution out of the box for that. So one needs to come up with ways to do this. Thanks to Stackoverflow we have something already done for us. I’ve added some things for zipping and here we are:

@REM echo off
@REM this script creates a backup of all the mysql databases in the designated directory.
@REM Please provide day of a week as a parameter

set file=d:\Mysql_backup\%1%_backup

MKDIR %1%
mysql.exe -uUSERNAME -s -N -e "SHOW DATABASES"  | for /F "usebackq" %%D in (`findstr /V "information_schema performance_schema"`) do mysqldump %%D -uUSERNAME --routines > %1\\%%D.sql
7z a %file%.zip %1%
rmdir /s /q %1%

Where USERNAME is a dedicated backuping user in mysql. I set up my backuping user with select permissions on all databases and no password. None of my mysql databases are available to the outside world, so no problem with having no password there.

This script needs a parameter to run. I usually set up daily backup jobs that are giving day of the week as a parameter, so I get Monday_backup.zip, Tuesday_backup.zip, etc.

Remove empty comments in VS2012

To remove empty comments form Visual Studio 2012 use this regular expression:

^(?([^\r\n])\s)*//\r?$

and replace all the matches with nothing. Just make sure you are only searching in *.cs files. Because this can mess up other files, like JavaScript

Apparently whitespace is matched by (?([^\r\n])\s) in VS search and replace tool. While \s is a white space including new line and carriage return character

Reference to Visual Studio 2012 regular expression search is here: http://msdn.microsoft.com/en-us/library/2k3te2cs.aspx

IIS File permissions

Many times you need to give permissions to modify files for web-sites running on IIS.
If you are using the Application Pools identity, you’ll have to use the following usernames:
Set the domain as the local machine. For user type IIS AppPool\DefaultAppPool or IIS AppPool\<AppPoolName>.

Source: http://www.iis.net/learn/manage/configuring-security/application-pool-identities

FRASCATI Technique

Just for my future reference, must put on my CV:

FRASCATI Technique: Forget Requirements and Analysis Start Coding And Then Implement

By the way, if you found this page by Googling a strange word in my CV, well done to you! You have found an Easter egg in my resume, please mention this when we talk next time. I wonder how many people carefully read the tech terms in job applications.

And I did not come up with this term, it comes from here: Stackoverflow

p.s. that’s a joke by the way.

p.p.s. feel free to look through this blog – I write some technical stuff here.