Tuesday 27 August 2013

Resharper's extract interface moves class property attributes to the interface

When I use ReSharper's magnificent feature of extract interface (Refactor -> Extract Interface), I have mixed feelings. On one hand I am happy that it saved me a lot of dirty work of manual cop-pasting the attributes, methods etc. On the other hand I am frustrated why class's attributes are copied to the interface. What the interface has to do with WCF attribute DataContract or Data Annotation attributes? I would expect ReSharper at least has an option whether to extract with or without the attributes. Apparently there is an open issue on TeamCity youTrack. The issue was open 7 (seven!!!) years ago and has only 3 votes (including myself).
Up vote for the issue: 


Tuesday 20 August 2013

How to install a development machine for .NET development

Recently I had to install a development machine from scratch. Like many developers I hate doing things twice, thus I summarized my experience in a few lines of command-line script. The script is divided to three logical parts:

  1. .NET Development environment
  2. Repositories
  3. Database

Let me introduce Chocolatey: the magical command-line installer that made my life much easier since I was introduced to it. You need to install it once by pasting  the magical piece of code to command-line console:
C:\>@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin - See more at: http://chocolatey.org/#sthash.6Kd97faR.dpuf

.NET Development contains 
  1. VS2012 Professional. From my experience Professional edition is good enough for most of the development tasks. The Express edition is not supported by Resharper, so it is not acceptable for me. 
  2. Resharper - needless to explain its importance here.
  3. Nuget Package Manager - same
  4. Resharper Nuget plugin - a very nice plugin for Resharper, that significantly made my life easier: when I type a class ,which is not referenced in my project, the plugin looks whether a corresponding package is already referenced is adds reference to that package (instead of adding reference to bin\Debug\some_package.dll )
cinst VisualStudio2012Professional
cinst resharper
cinst NugetPackageManager
cinst resharper-nuget

Repositories:
These command-line lines install Tortoise SVN and VisualSVN.
cinst tortoisesvn
cinst visualsvn

Database:
You may choose any database, I chosen MySql for the small project I am working on. These lines of code would install MySQL server. Please note that root has empty password after the installation. It would also install a free SQL Client tool: HeidiSQL
cinst mysql
cinst HeidiSQL


P.S. A few last words about licensing. I do not encourage anyone working with illegal software, the opposite. All the above programs, except Visual Studio and Resharper, are free. They were especially chosen for the small development projects and are free of charge. Visual Studio license can be acquired by applying to BizSpark program of Microsft, for more details . Resharper license can be provided for free by JetBrains for open source projects.

Monday 19 August 2013

How to kill ASP.NET Development Server or IIS Express

When working on a web project I need to kill all those instances of ASP.NET Development Server IIS Express. A simple and quick solution for this is a KillCassini extension for Visual Studio. Using a shortcut, Shift+Alt+K it stops all the instances of ASP.NET Development Server, also known as Cassini, and IIS Express as well.

Download from Visual Studio Extensions Gallery:




Sunday 4 August 2013

Type safe configuration mapper

Almost every project needs some sort of configuration, that is external of the source code. Sometimes we spend time to find the best library that fits our needs and sometimes we give up and write something in-house. I would like to save your time in both these tasks and suggest what I have found and I use in my projects.

I was looking for a library that would allow me to represent all parameters as an interface or class. The parameters would be strongly typed with minimum overhead in mapping parameter to property. Providing default values would be also nice, but not necessarily. The best library that exactly fits these needs is ConfigReader. Here are a few simple steps how to use it in your project:

1. Put your configuration parameters in the regular app.config or web.config file. Parameters may be of any primitive data types:
 

 <appsettings>
    <add key="ApplicationConfiguration.Duration" value="30" />
    <add key="ApplicationConfiguration.Distance" value="3" />
    <add key="ApplicationConfiguration.Author" value="Boris Modylevsky" />
 </appsettings>

2. Create an interface with properties to represent the configuration in code. Give properties the same names as parameters names. Properties may be read-only - having only getter without setter. We don't need to specify no special mapping attributes, nothing. Just an interface with properties. Here is a corresponding interface:
 

public interface IApplicationConfiguration
{
    int Duration { get; }
    double Distance { get; }
    string Author { get; }
}
3. Next step we need to initialize our configuration interface from the configuration file. If you are using Inversion of Control (IoC) containers, it is better to to in the bootstrapper - where all the classes are registered in the container. I will show how this could be done in Autofac. First of all we create a ConfigurationModule class, which defines configuration registration in Autofac:
 
using System.Diagnostics.Contracts;
using Autofac;
using ConfigReader;

public class ConfigurationModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        Contract.Assert(builder != null, "Builder container is null");

        var configReader = new ConfigurationReader().SetupConfigOf<IApplicationConfiguration>();
        var configuration = configReader.ConfigBrowser.Get<IApplicationConfiguration>();
        builder.RegisterInstance(configuration).As<IApplicationConfiguration>();
    }
}
4. Then we just register the ConfigurationModule in our container builder and that's all!
 

using Autofac;

public static class Bootstrapper
{
    public static IContainer Container { get; private set; }

    public static IContainer Initialize()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterModule<ConfigurationModule>();
        Container = containerBuilder.Build();
        return Container;
    }
}
5. Use it by defining as constructor parameter. IoC container will take care to transfer the correct instance into your constructor:
 
public class SomeClassThatUsesConfiguration
{
    private readonly IApplicationConfiguration _configuration;

    public SomeClassThatUsesConfiguration(IApplicationConfiguration configuration)
    {
        _configuration = configuration;
    }

    public double GetVelocity()
    {
        return _configuration.Distance/_configuration.Duration;
    }
}
If you like the above and would like to use it in your project, download it from nuget.org:

PM> Install-Package ConfigReader