Dependency Injection via Constructor Injection

The most common way of implementing Dependency Injection (DI) is to use Constructor Injection with classes. Let’s suppose we have a Customer class defined as follows:

We can create a service to fetch customer records and define an interface for this service:

An actual implementation of the DataService class could look like this:

Obviously the real thing would fetch the real records from somewhere, e.g. a database. When we fetch these records we’d like to put them in a Customer repository. Whenever we create an instance of the CustomerRepository class we’ll pass in the dependency to the…
Read more →

Unity – Constructors with Parameters

Microsoft Unity is a great library to use for a dependency injection container. It has support for constructor, property, and method call injection. Typically you’ll use constructor injection. I wanted to use it for a class whose constructor took parameters. Here’s an example I’ve created to show what I wanted to do: First I have an interface for logging – ILogger:

Then I have an actual class that implements this interface – FileLogger:

In my program I create the Unity container. I then want to register my FileLogger class as an implementation of the ILogger interface. This part…
Read more →

Kinect Point Cloud Normals Rendering – Part 4

In this post I’ll create a WPF front-end to the C++ template code I’ve been using so far. I’ll use a CLR wrapper to achieve this. The first step is to create a WPF project. I like to use MVVM Light Toolkit for my WPF projects. I’ve installed the Visual Studio plugin so it comes up as on option when I’m creating a new project. I started by created a project called DepthViewer2D: Once I created this project I added a new project to the solution called DepthImageRenderer. This is a C++/CLR class library project. You’ll find it under “Other…
Read more →

Polymorphism and Abstract in C#

Let’s say you want to define a base class that other classes will inherit from:

We want to make sure that everything that inherits from this class will implement its own version of the CreateBrushes() method. If this method isn’t implemented then we will get a runtime exception every time our inherited class is instantiated. This is one way of ensuring that inherited classes provide an implementation of a method. However, we only find out the problem at runtime. A better solution would be to get an error at compile time. This is where the abstract keyword comes in….
Read more →

Polymorphism in C++ and C#

I went for an interview for a C++ job one time and they showed me the following code and asked me what the output would be:

Really this is a question about inheritance and polymorphism in C++. The class description is the base class and the classes sphere and cube inherit from it. Because the description has declared it’s method print() as virtual a vtable is created under the hood for that class and method. Any class that derives from description is allowed to override it’s definition of print() and use its own. The definition of the print() method…
Read more →