Andy
Andy

Reputation: 5268

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

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;
    }
}

Upvotes: 3

Views: 594

Answers (2)

Tor Andersson
Tor Andersson

Reputation: 109

What i would do.

Dependency Injections is something you sooner or later run into when applying the S.O.L.I.D principles, so instead of jumping right into the action take som time and read this excellent pdf.

http://www.lostechies.com/blogs/chad_myers/archive/2008/03/07/pablo-s-topic-of-the-month-march-solid-principles.aspx

And then checkout.

http://structuremap.net/structuremap/

For examples.

It was the best ride of my life, hope it will be an equaly thrilling experience for you.

Upvotes: 0

DaveC
DaveC

Reputation: 2050

Hard to answer such a broad question, but your specific question of where/who creates the connection object is simpler.

You could pass in a factory object that knows how to create connection objects as well as or instead of the mode. This way, if you want to create different (e.g. mock) connections you just pass in a different factory object that creates a different implementation of the connection when asked to create one.

Does that help, or did I miss the real question?

Upvotes: 1

Related Questions