简体   繁体   English

具有依赖项注入的单独类库项目中的dbContext

[英]DbContext in separate class library project with dependency injection

I'm currently creating a WebAPI for a school project. 我目前正在为学校项目创建WebAPI。 Before is worked with DI but i can't seem to get it working in the current context. Before与DI一起使用,但我似乎无法在当前环境下正常工作。

for the project i'm using multiple projects for the layer where my DAL needs to get the dbContext through DI. 对于项目,我在DAL需要通过DI获取dbContext的层使用多个项目。

DbContext: 的DbContext:

public FICTIEContext(DbContextOptions<FICTIEContext> options)
        : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
        }
    }

startup register: 启动注册:

      public void ConfigureServices(IServiceCollection services)
    {
       services.AddDbContext<FICTIEContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Database")));
    }

Call to the DAL Constructor: 调用DAL构造函数:

public class LoginBL
{
    private LoginDAL _LoginDAL;

    public bool LoginValidation(string login, string password)
    {
        _LoginDAL = new LoginDAL(); //Gives the error. As i remember given that dependency injection works correctly it should work like this.           
        return _LoginDAL.LoginValidation(login, password); ;
    }
}

for my DAL i'm using ac# class library project with a class where i have a constructor which has the DbContext as parameter: 对于我的DAL,我正在将ac#类库项目与一个类一起使用,其中我有一个构造函数,该构造函数将DbContext作为参数:

       private readonly FICTIEContext _Database;

    public LoginDAL(FICTIEContext database)
    {
        _Database = database;
    }

When using this code i can't seem to call the constructor without getting errors about not giving any parameters with the call to the DAL. 使用此代码时,我似乎无法调用构造函数,而不会收到关于在调用DAL时不提供任何参数的错误。

Which part am i missing withing my solution and how can i fix it. 我缺少解决方案的哪一部分,以及如何解决。

Of course you need to provide an instance of FICTIEContext when creating an instance of a LoginDAL given your current implementation. 当然,你需要提供的实例FICTIEContext创建的实例时LoginDAL鉴于你目前的执行情况。 That's the purpose of using dependency injection. 这就是使用依赖注入的目的。

The question is whether you actually need or want dependency injection in this case? 问题是在这种情况下您是否真正需要或想要依赖注入? I doubt it. 我对此表示怀疑。 There should be no point of using nor testing the DAL class without a DbContext anyway. 无论如何,在没有DbContext情况下使用或测试DAL类是没有DbContext的。

In this case you could simply let the DAL class create an instance of the FICTIEContext internally. 在这种情况下,您可以简单地让DAL类在内部创建FICTIEContext的实例。 You may of course also provide an overload of the constructor that accepts a context if you want to: 当然,如果您愿意,还可以提供一个接受上下文的构造函数的重载:

private readonly FICTIEContext _Database;

public LoginDAL()
{
    _Database = new FICTIEContext();
}

public LoginDAL(FICTIEContext database)
{
    _Database = database;
}

Then the consumer of the class can choose whether to supply a custom FICTIEContext or let the LoginDAL class create a default one. 然后,该类的使用者可以选择提供自定义FICTIEContext还是让LoginDAL类创建默认类。

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

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