简体   繁体   English

我如何在类库中为依赖注入配置StructureMap?

[英]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 我正在创建一个程序集,一个尚待开发的应用程序将使用该程序集通过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. 我想使用StructureMap将接口传递给一个类,该类将充当SerialPort的包装器,该类表示将与之通信的设备的构造函数。

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. 之前,我已将StructureMap与具有Application_StartMain方法的Web /桌面应用程序一起使用,可以将其用作配置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? 因此,对我而言,配置StructureMap以在装配中使用的最佳方法是什么?

Tell your users which classes they should add to the container. 告诉您的用户应将哪些类添加到容器中。 Don't forget to describe each class purpose. 不要忘记描述每个班级的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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