使用Simple Injector注册单个类

时间:2019-03-21 08:16:14

标签: asp.net-mvc simple-injector

我该如何注册没有接口的类并获得服务。项目在.NET 4.5 MVC中。我第一次尝试使用SimpleInjector并在Startup类中创建ConfigureConfigs方法。我的类是:

  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

1 个答案:

答案 0 :(得分:0)

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

您可以执行以下操作:

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

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

相关问题