Jan Blaha

Blog about software development

.NET disposable pattern and [using] statment rocks

.NET

I remember the first time I saw the using statement in c#. It was actually used when reading file content.

using (var fileReader = new StreamReader(new FileInfo("filePath").OpenRead()))
{
}

I was very disappointed by this structure because it’s a lot of coding afford for something that could be done automatically. Why not to close file and release resources automatically when the file is garbage collected? But when I rethought this I realized that waiting for garbage collection does not need to be a good idea in many cases and .net disposable pattern is just great. It can be actually used in plenty other situations when you need to make sure that some finalizing work is done after main part is executed.

Last week I needed to solve the problem of running some parts of thread in different culture. It was a situation when I needed to send email to collection of users with the text in the preferred language. Because of using standard .net resources, the easiest way was to change current thread culture to user’s preferred one and let the .net resources do their job and get the correct language strings. The original culture should be obviously stored and switched back so the next code execution would not be affected by culture changes. This is a very nice example of [using] statement usage. Let’s take a look at the code.

foreach (var user in users)
{
    using (new LocalizedScope(user.PreferedCulture))
    {
         //get resources and send emails
    }
}

This was easy. We will switch to prefered culture for every user, take localized resources and switch the culture back after we are done. LocalizedScope implementation than look like this.

public class LocalizedScope : IDisposable
{
    private string _storedCulture;

    public LocalizedScope(string preferedCulture)
    {
        _storedCulture = Thread.CurrentThread.CurrentCulture.Name;

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(preferedCulture);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(preferedCulture);
    }

    public void Dispose()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_storedCulture);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(_storedCulture);
    }
}

Conclusion

The Disposable pattern and using statment is not there only for .net core libraries and streams. There are plenty of situations where you can use it and make your code much more readable. Use it loudly and proudly, it will identify the quality of your code immediately.

last blog posts


09-12-2017 18:40 jsreport

Quite some time ago I blogged about rendering pdf reports in c#. Recently we have added excel reports into jsreport and it was released with a little delay also into .NET. This means you should be able to use both html-to-xlsx and xlsx recipes to create excel files from your .NET environments now.

read more

09-10-2015 14:09 AWS

Such a very common thing like adding an existing external volume to Amazon elastic beanstalk is not easily supported out of the box. The official blog mentions only how to attach a snapshot or how to attach and overwrite a new volume every time the service starts. It took me a while to make the config file actually adding an existing volume without formatting it every time so I share it here with you...

read more

04-10-2015 14:09 jsreport

The best practice when adding email notifications feature to your system is to separate as much as you can from email body assembling to email sending outside of the core system. The emails templates quite likely often changes and you don't want to deploy the system because of every single notification change. The best is to just separate everything into an external system and give the access to your PR or Marketing department so they change emails as the time goes without affecting the core system.

read more

Jan Blaha

About author

Hi! My name is Jan Blaha. I'm software developer and startup enthusiastic. See my current work.