使用 Simple Injector 注册单个 class

Register single class with Simple Injector

我如何注册没有接口的 class 并在此之后得到他的 services.Project 在 .NET 4.5 中 MVC.I 第一次尝试使用 SimpleInjector 并制作方法 ConfigureServices在启动 class.My class 中是:

  public class LawyersDB : DBFunction
{
    #region "MemberFields"
    Int16 _CommandTimeout;
    #endregion

    #region Constructors

    public LawyersDB()
    {
        _CommandTimeout = 300;
    }

    public LawyersDB(SQLDatabase conDatabase) :
        base(conDatabase)
    {
        _CommandTimeout = 300;
    }

    #endregion

    #region LIST

Simpleinjector 不喜欢 classes 或具有多个构造函数的服务。它认为它们是代码味道 IIRC。

你可以这样做:

container.Register(() => new LawyersDb(container.GetInstance(typeof(SQLDatabase)), Lifestyle.Scoped);

您需要设置范围并获取要在 class 中使用的 SQLDatabase 实例。