使用Simple Injector注册单个类

问题描述 投票:0回答:1

如何注册没有接口的类,然后获取他的服务。项目在.NET 4.5 MVC中。我首次尝试使用SimpleInjector并在Startup类中创建方法ConfigureServices。我的类是:

  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
asp.net-mvc simple-injector
1个回答
0
投票

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

你可以这样做:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.