Dependency Injection and Circular References

I’ve been using dependency injection (DI) in my code for over a year now. I wasn’t a huge fan of it when I started. The only benefit I have seen from using it is with unit testing. When code is unit tested, its easier to “hide” parts of code you don’t want to test.

I’ve also read about the performance hit for using DI. All code has a cost and I wanted to be preemptive about getting the most out of our code. The company I work for uses Ninject for their DI tool. Many performance benchmarks place Ninject near the bottom of the list for performance. DI’s performance hit is mostly during startup, but that is one of the places I feel we need to be faster.

We didn’t do anything complex with Ninject/DI. Just basic constructor injection. This is the act of using the DI container to populate the IWeapon object when it creates the object based on a Ninject config file.

--Code from Ninject's site.
class Samurai
{
    readonly IWeapon weapon;
    public Samurai(IWeapon weapon) 
    {
        this.weapon = weapon;
    }
    public void Attack(string target) 
    {
        this.weapon.Hit(target);
    }
}

class Sword : IWeapon
{
    public void Hit(string target) 
    {
        Console.WriteLine("Chopped {0} clean in half", target);
    }
}

When we use the code, we can just call the following.

Samurai sam = new Samuari();

If you’re new to DI, you’ll assume that this code will break, but Ninject/DI will use its config file to add whatever class you have bound to IWeapon.

This means that removing Ninject and its config file will break the code as the IWeapon object isn’t defined. Using our this is where I was updating our code to manually bind on the default constructor. The updated code would look like the following

--Code from Ninject's site.
class Samurai
{
    readonly IWeapon weapon;
    
    --Default Constructor replaces Ninject.
    public Samurai()
    {
        this.weapon = new Sword();
    }

    public Samurai(IWeapon weapon) 
    {
        this.weapon = weapon;
    }
    public void Attack(string target) 
    {
        this.weapon.Hit(target);
    }
}

class Sword : IWeapon
{
    public void Hit(string target) 
    {
        Console.WriteLine("Chopped {0} clean in half", target);
    }
}

If you’re still reading, thanks 🙂 Ninject purists will state that I’m loosing the advantages Ninject provides. If an implementation changes, I can’t make a fix in 1 Ninject config file, but must update code in several places. I’m okay with this performance tradeoff. My implementations don’t change that much and I have find/replace tools that I am comfortable with. Using this method, I still get the advantages of basic DI for unit testing that I wanted in the first place.

Here is where the problem comes in and the title of the article. By using a DI tool such as Ninject, I can easily create circular dependencies in my projects. It turns out that in the project I was attempting to remove Ninject from, two assemblies were each referencing each other. Because we are using Ninject, there is not direct reference in the project references for the other project. Its all brought in via runtime with the DI tool.

At this time, my plans to remove Ninject are on hold as it would take a significant refactoring of the code to remove the circular reference. I am by no way saying not to use DI, but to be careful as it makes it easier to do some things that you don’t want to do.

Hogan Haake

Hannah and Company – 6/26/2014

My sister Hannah and her friends Tiffany, Shelby, and Kaley came to visit for a few days.  Being poor college students, Hannah depended on her big brother to give her free room and board plus some entertainment. And being the good brother, I didn’t let them down. The weekend started with a call from Hannah telling me what time she and her friends would be in town. I was then instructed on what type of alcohol to purchase. This was late Thursday night, but I was up to the challenge.

After a trip to the grocery store for supplies, I laid on the couch for a nap before their expected 1am arrival. I had about an hour’s refreshing sleep before the girls arrived.  Once they were settled, I did my usual telling of silly jokes while all participated in many drinks.

With my kids away at camp while the girls were there, they were free to use the kids rooms for their stay. They didn’t wait too long before getting comfortable. 20140627_033747

Danelle and I had to work on Friday and gave the girls a ton of ideas of what they should do and see while in St. Louis. This included a warning that rain was coming. Like any good visitor to the city, they proceeded to visit the arch.20140627_112248

They ended up getting wet walking around downtown. I suggested they visit the Graffiti wall, but they couldn’t find it.   They came back to the house and Danelle and I hopped in the van with the girls and headed back downtown to find it. The girls were quite impressed with the art and we may have done some decorating of our own…20140627_181715 20140627_181357 20140627_181136 20140627_180742 20140627_180718 20140627_180641 20140627_180618

Later that night, we ended up at a karaoke bar.  The girls got up and attempted to perform “Benny and the Jets”, but it was worse than horrible.  We all laughed at it.

Danelle and I are looking forward to hosting the girls again this coming summer.

Hogan Haake

Welcome to WordPress!

For the few people that follow my log, you may have noticed a change… I was using BlogEngine.NET for my site. I started noticing that things were acting up. It ends up that somebody hacked my site and started modifying every post I put up on the site. It has taken me a few days to upgrade to WordPress, but I’m done now. Here is to hoping that WordPress is a more stable platform!

If you have any issues with a post or want more information, please comment and I’ll do my best to fix it!

Hogan Haake

Meramec River Training Run – 6/19/2014

While Danelle and the kids were at the St. Louis Cardinals Game, Jo and I were on the Meramec river training for the MR340. Of course, this was an equipment test, more than an endurance run. We paddled approximately 4 miles downstream to the Meramec Marina. There, Jo and I tied up the boat to the dock and had pizza and beer on the floating bar.

Of course we didn’t just eat. We spotted a fawn sitting on the shore hidden from most. Look in the center of the photo.

Finally, we tested out the home made spray skirt for the boat. Jo made this in her warehouse with some help from me. We found that it was perfect for keeping the water out of the boat when we double bladed.

The equipment check went off perfectly and it was a day well spent on the river!

Hogan Haake