I enjoy doing anything by myself instead of using well know frameworks. I am not saying that I am using only my own frameworks. It just gives me more knowledge about how these frameworks doing their job. Inversion of Control Framework it is one of these examples. I have a blog post on Russian about simple implementation of IoC container. You can look on it here Simple IoC Container (I used Microsoft Translator for this link). Actually right now is more than just one class, I have my own framework, which I use for some my applications, like gMusic (you can follow this project on GitHub – Framework).
Couple months ago I met an article IoC Container Benchmark - Performance comparison, where this guy compare different IoC containers (when I first time met this article it was only about performance). So I decided to check performance of my simple container. This is result (I have different count of iterations, so do not compare it to the original blog post):
In this list: "Outcold" – this is my current framework, "IoC A" – it is a class which I showed in this blog post Simple IoC Container (based on Activator.CreateInstance), "IoC E" – it is a different version of IoC class, but based on Expressions (I showed in in one of my comments here). "LightInject" – it is one of the faster frameworks from original blog post about IoC performance, "Unity" – it is one of the famous frameworks. As you can see that, the version of IoC class which is based on Expressions has the worst performance. The funny thing, that I used it everywhere, I really thought that it should be faster than Activator, somebody told me… and I did not verify that…. Anyway, after this article, I spent some time to improve performance of my IoC container, and this is how I did this.The main problem of IoC container which I implemented is that I did not cache the compiled Expression. Each time when I ask IoC to resolve some object – it compiles expression and after this resolve it:
First optimization is simple. Just need to cache expression after first time somebody will ask to resolve object. However, first I need to change how I invoke constructor, because my expression expects constraints I need to change it to parameters, so each "resolving" can use it is own parameters:
Now I have Delegate, which can create for me a new object of specific type. The only one problem, that this delegate can have different set of parameters, so I cannot cast it to something like Func<Type1, … , TypeN, Result> and invoke it. I need to call DynamicInvoke, which is slow:
This is the result of this optimization (as you can see it is very good, but still variant with Activator works better):
The second step I made – I just changed Expression in such way, so it always takes just one parameter "array of objects". In expression I use this array to cast each item of this array to the parameter in constructor. This is how I do this:
This is the result:
It is faster than version with Activator! But it still slower than LightInject. I guess it is because LightInject used before System.Reflection.Emit instead of Expressions, or maybe they have better idea how to create this expression for type resolving.
Anyway, it was a good result for me.
Computer Software can have bugs and some of these bugs are Memory Leaks. It is usual case that if your software is a Multi-Document application you need to pay more attention on memory leaks. The main question in these applications is how I can be sure that when user will close document I will release all instances and resources related to this document? What if you already found a Memory Leak and fixed it? How you can protect yourself to be sure that after some changes you will not have this memory leak again?
I had the same thought after one of my Memory Leak fix. Do we have any software or tools which can validate that after some interaction with my application all instances are released, which I want to be released? In native world there are should be a lot of different instruments / practices to do this (I'm not an expert, but pretty sure that this is true). In managed world there are a lot of tools with names Memory Profiler, but to make them work you need to run them separately, analyze your application and after that analyze the report to find memory leaks. This can work, but I'd like to have some instrument in my application which can in runtime check my application and find memory leaks. The solution which I found is pretty simple. I store the list of WeakReferences to objects which I want to check after some interaction that they are released. Based on this I wrote Object Release Verification Framework with couple classes and interfaces. This framework is published on nuget.org as a portable library.
Let's take a look on example.
First time when I familiarized with unit testing was 5 or 6 years ago. It was start of my developing career. I remember that somebody told me about code coverage. At that time I didn’t write any Unit tests. Guy, who was my team lead, told me “Do you see operator if with three conditions? You should check all of these conditions”. So, after that I had written some code, I should go to interface and try to invoke all code which I wrote from user interface. Nice? At current time I know little more about tests and unit testing. I have not participated in projects, designed by Test Driven Development (TDD). Basics of my knowledge are a spying code of my colleagues, some articles and screencasts. I had decide that I should know much more, and became a real professional of unit testing, this is why I had start to read book The art of Unit Testing with Examples in .NET. More than, in my current job place looks like I’m just one who writing unit tests for my code. I should show good examples of my tests.
I had read about Code Contracts long time ago, but I didn’t see a reason to use contract instead of simple tests and argument's check with Exception's throws. Only now I’m trying to use them at real project. I didn’t see why I need to write:
Instead of like I did it before:
Couple of weeks ago I had reinstall my Windows 7 on my laptop (I bought SSD for my laptop). After I install ReSharper with Visual Studio 2010 after run I accidental choosed “Go to the metadata” instead of like usual “Go to Object Explorer” by Ctrl and Mouse Click. So when I clicked on some class of System.xxx (basic classes of .NET) I looked at source code of these classes and found that they are using Code Contracts. I thought this is why I need to understand why I need to use contracts in my code, so I started to use them in some small applications and tried to find some interesting case.
Couple days ago I got a question about how to register hotkey in Windows for WPF application. I remembered that one year ago I was solving the same problem in WinForms application, I was registering hot keys for my application, it was Vista Keys Extender project. I knew that my project worked, so I suggested author of question to use code of my project to solve his problem. But as we learned later in WPF message handle mechanism different from WinForms. So I started to find solution for WPF application. I copied my old code from my old project and started to rewrite it step-by-step.