简体   繁体   中英

How do I need to configure StructureMap for Dependency Injection in a Class Library?

I'm creating an assembly that a yet-to-be-developed application will be using to communicate with a device via System.IO.Ports.SerialPort

I want to use StructureMap to pass an interface to a class which will act as a wrapper around SerialPort into the constructor of the class representing the device I'll be communicating with.

eg

public interface ISerialPort
{
    Handshake Handshake { get; set; }
    bool IsOpen { get; }

    event SerialDataReceivedEventHandler DataReceived;

    void Close();
    void Open();
    string ReadLine();
    string ReadExisting();
}

public class MyDevice
{
    private readonly ISerialPort _serialPort;

    public MyDevice(ISerialPort serialPort)
    {
        _serialPort = serialPort;
    }
}

I also intend to use this approach to facilitate unit testing.

I've used StructureMap before with web/desktop applications that have had either Application_Start or a Main method, which I could use as an entry point to configure StructureMap. However, as this is a stand-alone assembly without a single entry point I can't do the same.

As such, what is the best way for me to configure StructureMap for use in an assembly?

Tell your users which classes they should add to the container. Don't forget to describe each class purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM