Software Design

How to use the ConfigurationManager class correctly

I want to talk about a practice that I have found very often in the C# community and it is the use of the static class ConfigurationManager. What I find annoying is how easily this class is spread around our application without thinking in the repercussions that it can bring to us. First of all I don’t consider a good practice that our code should know how we are getting the configuration for our application, tying it to the way that we decide to extract it, and how we know, this could change, let me explain myself better with a snippet of code.

var timeout = ConfigurationManager.AppSettings["timeout"];

In the example above we are telling to our code from where we are getting the timeout configuration, if in certain moment we think that it is a better option instead of defined a value in the appSettings, begin to use the timeout defined in sessionstate  we have to change how we are getting the timeout value completely, it would be:

var timeout = ((SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState")).Timeout;

If that line of code is spread over the application we have to do the same change through the entire code base getting the risk that we forgot to change it in some places.

A better approach to work with configurations it is create a class which contain all the logic to extract the configuration values, isolating how we do it and from where we are taking it, thus the code would not depends of ConfigurationManager (or whatever configuration provider we are using), it will use our class instead, in that way it is not going to change  in the future. An example of how the class could be is:

public class SystemConfiguration
{
    public long getSessionTimeout()
    {
        return ConfigurationManager.AppSettings["timeout"];
    }
}

We can use our new class in the following way:

SystemConfiguration configuration = new SystemConfiguration();
long timeout = SystemConfiguration.timeout();

Now the code that use the configuration it is disconnected of how we get the values. If we decide to begin to extract our configuration from a XML file we only have to make the changes in only one place and the client code is not going to be affected.

Other important problem of using ConfigurationManager directly in our code is the fact that made the code hard to test since we are not able to mock the ConfigurationManager class, but that problem could be solved using the same strategy that we use above, isolating the use of ConfigurationManager in other class. However we have to do some changes, first of all we have to create an interface

public interface IConfiguration
{
    long getSessionTimeout();
}

and make our class implement the new interface

public class SystemConfiguration : IConfiguration
{
    public long getSessionTimeout()
    {
        return ConfigurationManager.AppSettings["timeout"];
    }
}

Now our code is completely testable.

Concluding

The use of ConfigurationManager may seem very innocent and that innocence make it used indiscriminately in our code base making our code more susceptible to change and less testable, definitively not a good combination.

Problems solving

“No enough space on disk”, installing Ubuntu with UEFI and Windows 10

Hello guys,  how are you doing?. A few days ago I wanted to install Ubuntu 16.04 in my laptop(ASUS x555u) along side Windows 10 and what I think it could be a very straightforward process turn in a nightmare of investigation and “whys”, I couldn’t install the system due to in the second step of the installation dialog appear a message which said “No enough space on the disk”, finally I could solve the problem and today I want to share my experience with you.

I am going to focus the process in Windows 10( but I am sure that is the same for Windows 8 and 8.1) and Ubuntu 16.04 but I had the same problem with Ubuntu 14.04 and Ubuntu 15.10 so I think the solution should be the same. For landing more the solution I want to say you that I tested this process on two ASUS, one has a core I5 5th generation(Broadwell) and the other has a core I5 6th generation(Skylake) and a Nvidia 940M, the first two steps of the process works well with the Broadwell but with the Skylake I was to go until the end. First of all I think the solution is not change the boot mode to legacy, this is a lazy solution and bring more problems than it really solve and moderns computers is not bringing this option, moreover is very easy install Ubuntu with UEFI when you know how to do it. I recommend use a booteable USB drive because is faster than a CD and less error pron. Lets start:

  1. The first thing that we have to do is create a booteable USB with the Ubuntu image, for to do this I recommend use Rufus, Why?, due to a very important reason, to avoid conflicts between the scheme partition table of the hard drive and the scheme partition table of the booteable USB, the most of the tools for make bootables USBs use MBR like scheme partition table and  if our system has UEFI is very likely that it use GPT like scheme partition. Once we have clear this, is time to move forward, the first things that we are going to do with Rufus is select the Ubuntu image’s direction, we are doing this first because when you select a image in Rufus it reset all the fields in the interface so you have to reconfigure all again. After select the image we configure the booteable usb like always, paying special attention to the second field it is about the scheme partition:rufus
    The default option is “MBR partition scheme for BIOS or UEFI” we have to change this and select “GPT partition scheme for UEFI computer”, take care of the device in the first field is the device that you want to use, after that just press start.
  2. The second thing that we are going to do is turn off all the stuff that could hold our hard drive during the boot stage and prevent us continue with the installation successfully. For to do this the first thing that we going to do is turn off the hibernation and fast startup of Windows (is the same for 10,8 and 8.1), here is an article about this. Now is time to restart the system and enter in the computer set up( this depends strongly of the computer but normally is pressing “F2” or “Delete” when the computer start) and turn off  secure boot, fast boot, and  Intel Smart Connect Technology if you get it of course, after that select the booteable USB like the primary booteable device, save the changes, left the set up and we’ll be able to see a screen like thisUbuntu-16.04-Boot-ScreenIf we see this screen it is a good signal, that means that we are booting our USB in UEFI mode and that is exactly what we want now just select “Install Ubuntu” and enjoy the installation process.
  3. If during the installation process you get a error like this:error
    and when you computer are shutting down in the screen appear somethings like this that never end
    black_screen
    congratulations this post is for you, now is when the fancies things begin, the first thing that you have to do is restart your PC and boot with your USB, in the Ubuntu boot screenUbuntu-16.04-Boot-Screen
    press ‘e’ to edit the commands for booting and locate the cursor after “quiet splashkernel_parameters
    type “pci=nomsi” and press Ctrl-x, now you could be able of install Ubuntu. If when you finished the installation appear a error saying that you don’t have space of disk, don’t be afraid that is because the black endless screen with errors of the beginning save all the messages in the files kern.log and syslog this files are in the folder /var/log/, now we have to erase the files’ content and we are going to do that with the following commands:

    sudo cp /dev/null /var/log/kern.log

    sudo cp /dev/null /var/log/syslogNow your space problem has been solve, is time for a last configuration, now is necessary make the change of the commands booting permanent in your system, for to do that we have to modify the file /etc/default/grub and type “pci=nomsi” after “quiet splash” in GRUP_CMD_DEFAULT=”quiet splash” your file has to be like this:grup

OK that is all, I hope this could be of help for you, if you have any doubt or suggestion just let a comment. I’ll see you soon.

Related links: