C# SQLDependency与DI

C# SQLDependency与DI,c#,entity-framework,dependency-injection,sqldependency,C#,Entity Framework,Dependency Injection,Sqldependency,我想在我的项目中使用SQLDependency和依赖注入。这是我的密码 public interface IAreaRepository { string GetAreaQuery(); List<Area> GetAreas(); } public class AreaRepository: IAreaRepository { public AreaRepository(DbContext dbContext) { _dbContext

我想在我的项目中使用SQLDependency和依赖注入。这是我的密码

public interface IAreaRepository
{
   string GetAreaQuery();
   List<Area> GetAreas();
}

public class AreaRepository: IAreaRepository
{
    public AreaRepository(DbContext dbContext)
    {
       _dbContext = dbContext;
    }

    public string GetAreaQuery()
    {
       return _dbContext.Areas.ToString();
    }

    public string GetAreas()
    {
       return _dbContext.Areas.ToList();
    }
}
现在我的问题是如何调用initialise AreaDataProvider,因为如果我使用下面的DBContext传递IAreaRepository

using (AppContext context = new AppContext())
{
   AreaDataProvider provider = new AreaDataProvider(new AreaRepository(context));
   provider.RegisterForNotification();
}
在第1行和第2行中,我的DbContext将不同。跨AreaDataProvider类实现单个DbContext的最佳方法是什么

第1行和第2行中的DbContext将不同


不会的。它将与传递给构造函数的存储库相同。

第1行和第2行是什么?无论如何,您可以为
AreaDataProvider
对象创建一个默认构造函数,该构造函数创建自己的DbContext实例并将其传递到存储库。上面的代码对第1行和第2行进行了注释,基本上,它们是访问存储库(Db)的两个单独调用。
using (AppContext context = new AppContext())
{
   AreaDataProvider provider = new AreaDataProvider(new AreaRepository(context));
   provider.RegisterForNotification();
}