Introducing SQL2JSON

If you’ll ever try to generate JSON file using Sql Server’s Management Studio, probably there’ll be lots of pain in your future. If you wanna save the pain, you can use SQL2JSON, an open source project I started last week.

The idea was born out of a need to generate a huge JSON file containing data from MSSQL database. After seeing our DBA struggling with formatting, escaping weird characters and unicode strings, I decided to write a small utility that will do just that - execute a sql query and capture its results as a json file. You can grab it from Github repository.

Usage

Suppose you have a table called “users” with the following records:

1
2
3
4
5
|user_id | first_name | last_name
|--------|------------|----------
|1       | john       | johnson
|2       | scott      | scottson
|3       | paul       | paulson

Unicode File Names in Python 2.7

Today I wrote a small script to find and delete duplicate files. To do this task I needed to iterate over files in a specific folder, and calculate md5 checksum for each file:

1
2
3
4
5
for folder, subs, files in os.walk(path):
    for filename in files:
        file_path = os.path.join(folder, filename)
          with open(file_path, 'rb') as fh:
              ...

If the source folder contains a file or a folder with unicode characters in it, execution of the code results in this bummer:

1
IOError: [Errno 22] invalid mode ('rb') or filename: 'files\\????????? ????? ????????.txt'

Uncoupling Configuration Files

Tight coupling is a known source for inflexible and hard to test code. In this post I want to talk about a rather unexpected source of tight coupling - configuration files. Configuration files are external dependencies. As other external dependencies, its infrastructure may change in the future, and it should be easily mocked for unit testing. Modern software frameworks provide means for easy access to the values stored in configuration files. In the .NET framework configuration files can be accessed using the ConfigurationManager:

1
2
3
4
5
6
7
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="Foo" value="1"/>
    <add key="Bar" value="2"/>
  </appSettings>
</configuration>
1
2
int foo = int.Parse(ConfigurationManager.AppSetttings[Foo]);
int bar = int.Parse(ConfigurationManager.AppSettings[Bar]);

ConfigurationManager makes it trivial to access data in the config file, however in most cases it also introduces various code smells that make the code tight coupled and hard to test. In the next sections I’ll introduce a simple class and will use it to demonstrate the code smells and violations of principles of clean object oriented design. The code will be gradually refactored to a better and cleaner solution.

Learning From Mistakes: Leaky Abstractions

On the project I’m working on I’ve had a requirement to store and read files from the file system. Alse the files had to be accessible from the web.

Having a gut feeling that the infrastructure may change as the business will grow, I decided to hide operations on the file system behind an interface:

1
2
3
4
5
6
public interface IFilesStorage {
    string StoreFile(Stream stream, string fileName);
    Stream GetFile(string virtualPath);
    string GetFileUrl(string virtualPath);
    string GetFilePath(string virtualPath);
}

As it looks, if someday I’ll need to switch from the file system to another storage mechanism, I’ll be able to do get the job done by writing another implementation of the interface. Right? Wrong! The requirement did come in - I’ve had to store the files in S3. And only then I realised that IFilesStorage is a leaky abstraction.

Dependency Injection Patterns

Choosing the right pattern for implementing dependency injection is an important task and can affect your class’s usability and functionality. In this post I’ll overview 3 patterns of implementing dependency injection - constructor injection, property injection, builder (Joshua Bloch’s pattern, not GoF pattern). For demonstration purposes we will work with a class called TextTranslator, that requires 3 dependencies: TextReader, TranslationService and TextWriter:

1
2
3
4
5
6
public class TextTranslator
{
    protected TextReader textReader;
    protected TranslationService translationService;
    protected TextWriter textWriter;
}

The sample code will be written in C#, but the examples are applicable to Java and other object oriented languages.

Disposing Objects Created by DI Container

The general rule says that if you created an object, then it is your responsibility to dispose it. Things get a bit tricky when objects are created by a dependency injection container. The responsibility of containers is to construct objects and inject the dependencies they need. An error I’ve seen people doing again and again is to dispose objects that were injected into an object via constructor or property injection. Consider the class “Foo” that requires an instance of class “Bar”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Foo : IDisposable {
    protected Bar bar;
    public Foo(Bar bar) {
        this.bar = bar;
    }
    public void Dispose() {
        bar.Dispose();
    }
}

public class Program {
    static void Main() {
        var foo = container.Resolve<Foo>();
        ...
        foo.Dispose();
    }
}

The problem with this approach is that the “Foo” class will know too much about the environment it runs in. At least it will think it does. An instance of “Foo” can’t and shouldn’t know who passed in the instance of “Bar”, and what he intends to do with it after the instance of “Foo” will be disposed.