Dependency Injection: I don't get where to start!

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2011-01-03T11:44:30Z Indexed on 2011/01/03 11:53 UTC
Read the original article Hit count: 247

I have several articles about Dependency Injection, and I can see the benefits, especially when it comes to unit testing. The units can me loosely coupled, and mocking of dependencies can be made.

The trouble is - I just don't get where to start.

Consider this snippet below of (much edited for the purpose of this post) code that I have. I am instantiating a Plc object from the main form, and passing in a communications mode via the Connect method.

In it's present form it becomes hard to test, because I can't isolate the Plc from the CommsChannel to unit test it. (Can I?)

The class depends on using a CommsChannel object, but I am only passing in a mode that is used to create this channel within the Plc itself. To use dependancy injection, I should really pass in an already created CommsChannel (via an 'ICommsChannel' interface perhaps) to the Connect method, or maybe via the Plc constructor. Is that right?

But then that would mean creating the CommsChannel in my main form first, and this doesn't seem right either, because it feels like everything will come back to the base layer of the main form, where everything begins. Somehow it feels like I am missing a crucial piece of the puzzle.

Where do you start? You have to create an instance of something somewhere, but I'm struggling to understand where that should be.

public class Plc()
{
    public bool Connect(CommsMode commsMode)
    {
        bool success = false;

        // Create new comms channel.
        this._commsChannel = this.GetCommsChannel(commsMode);

        // Attempt connection
        success = this._commsChannel.Connect();  

        return this._connected;
    }

    private CommsChannel GetCommsChannel(CommsMode mode)
    {
        CommsChannel channel;

        switch (mode)
        {
            case CommsMode.RS232:
                channel = new SerialCommsChannel(
                    SerialCommsSettings.Default.ComPort,
                    SerialCommsSettings.Default.BaudRate,
                    SerialCommsSettings.Default.DataBits,
                    SerialCommsSettings.Default.Parity,
                    SerialCommsSettings.Default.StopBits);
                break;

            case CommsMode.Tcp:
                channel = new TcpCommsChannel(
                    TCPCommsSettings.Default.IP_Address,
                    TCPCommsSettings.Default.Port);
                break;

            default:
                // Throw unknown comms channel exception.
        }

        return channel;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms