Monday, March 23, 2015

C# Dependency Injection Simple Example



People are often confused about what Dependency Injection is and when they might need or want to use it. Some time ago I wrote an article Managing Dependency Injection in C# with Autofac which explains how to manage DI in C#, but today I want to show by simple code sample what actually Dependency Injection is.

Imaging situation where you have a class, let's say Employee, and two or more different loggers for that class. Each logger prints messages in his own particular way and you want to have control of which logger to use for Employee during its instantiation.

Just take a look at this code and I'm sure you will get the idea of Dependency Injection:

public class Employee
{
    public Employee(ILogger logger)
    {
        logger.WriteToLog("New employee created");
    }
}

public interface ILogger
{
    void WriteToLog(string text);
}

public class LoggerOne : ILogger
{
    public void WriteToLog(string text)
    {
        Console.WriteLine(text);
    }
}

public class LoggerTwo : ILogger
{
    public void WriteToLog(string text)
    {
        Console.WriteLine("***********\n {0}\n***********", text);
    }
}

Thursday, November 27, 2014

How to Avoid Any Global Variables in JavaScript



In JavaScript, objects and functions, are also variables. And any variable declared outside a function has global scope. You probably heard that because JavaScript is a dynamically type language global variables are bad and that they easily become source of confusion and bugs. So how can we avoid of creating any global variable, but still have our code executed?
There is such technic, it's called Immediately Invoked Function Expression (IIFE).

Suppose we have this JavaScript code:

var func = function(){
  
  console.log("entering func");
  
  var counter = 0;
  
  var innerFunc1 = function(){
    counter += 1;
    console.log("innerFunc1: " + counter);
  }
  
  var innerFunc2 = function(){
    counter += 1;
    console.log("innerFunc2: " + counter);
  }
  
  return {
    inner1: innerFunc1,
    inner2: innerFunc2,
  };
}

var f = func();
f.inner1();
f.inner2();

So here we have two global variables - func and f.
Now I'll show how to run this code without creating ANY global variable by applying IIFE.

Monday, November 3, 2014

WPF Customized ComboBox with Non Existing Value



How do you show a non existing value inside a WPF ComboBox? Good question! And the sad truth is that you just cannot do it! WPF ComboBox shows only the values that are in its ItemSource. 

But don't worry, today I'll show you a workaround to this problem. In this particular example inside a ComboBox will be shown a phantom value and when user clicks on it and choose another value from the list he will be prompted to confirm in order to give him a chance not to loose this phantom value in case if he clicked on the ComboBox by mistake.

WPF Customized ComboBox with Phantom Value

The trick is that I create additional custom ComboBox with TextBlock, binding it to the original one and overlaying it.