有没有办法让ASP.NET 5依赖注入解析一个没有引用的DbContext? [英] Is there a way to have ASP.NET 5 Dependency Injection resolve a DbContext without a reference?

本文介绍了有没有办法让ASP.NET 5依赖注入解析一个没有引用的DbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC 6进行一些原型设计,并且遇到了一个困境。我们的项目架构很简单:



数据层(实体框架6)

服务层(类库,参考数据层)

演示层(MVC 4,参考服务层,参考数据层)



我试图将设计保持为即使在阅读(并同意)组合根图案后,尽可能类似。这意味着我的新MVC 6应用程序不知道我的 DbContext 类型。您可以猜测当我尝试将一个服务类注入控制器时会发生什么:



无法解析My.Data类型的服务。 Tier.DbContext尝试激活My.Service.Tier.SomeService。



我相信我们以前的实现(我没有写)通过反映bin文件夹中的程序集来解析 DbContext 。有一种我可以使用ASP.NET 5 / MVC 6内置的新的 Microsoft.Extensions.DependencyInjection 命名空间来做这个(或类似的东西),所以我可以避免对我的演示文稿层的数据层进行严格的引用?



更新

阅读Joe Audette的答案后,我想出了一个非常简单的扩展方法,我添加到我的服务层:

  public static class ServiceCollectionExtensions 
{
public static void RegisterDbContext(this IServiceCollection services)
{
services.AddScoped< My.Data.Tier.DbContext,My.Data.Tier.DbContext>();
}
}

然后,在我的MVC应用程序的Startup.cs: / p>

 使用My.Service.Tier.ExtensionNamespace; 

public void ConfigureServices(IServiceCollection services)
{
services.RegisterDbContext();
}


解决方案

我认为在内置DI一切您需要注册的DI服务。



为了保持数据引用您的MVC应用程序,您可以在服务层中有一个IServiceCollection扩展方法将注册dbcontext,否则它可以调用数据层上的扩展方法。



这样你的mvc应用程序只需要在服务层上引用一个引用。


I am doing some prototyping with MVC 6 and have run into a quandary. Our project architecture is straightforward enough:

Data Tier (Entity Framework 6)
Service Tier (Class Library, references Data Tier)
Presentation Tier (MVC 4, references Service Tier, does not reference Data Tier)

I'm attempting to keep the design as similar to the original as possible, even after reading (and agreeing with) the Composition Root pattern. This means that my new MVC 6 application is unaware of my DbContext type. You can guess what happens when I attempt to inject one of my service classes into a controller:

Unable to resolve service for type My.Data.Tier.DbContext while attempting to activate My.Service.Tier.SomeService.

I believe that our previous implementation (I didn't write it) resolves the DbContext by reflecting assemblies in the bin folder. Is there a way I can do this (or something like it) with the new Microsoft.Extensions.DependencyInjection namespace that's built-in to ASP.NET 5/MVC 6 so I can avoid a hard reference to my data tier from my presentation tier?

Update
After reading Joe Audette's answer, I came up with a very simple extension method that I added to my service tier:

public static class ServiceCollectionExtensions
{
    public static void RegisterDbContext(this IServiceCollection services)
    {
        services.AddScoped<My.Data.Tier.DbContext, My.Data.Tier.DbContext>();
    }
}

Then, in my MVC app's Startup.cs:

using My.Service.Tier.ExtensionNamespace;

public void ConfigureServices(IServiceCollection services)
{
    services.RegisterDbContext();
}

解决方案

I think in the built in DI everything you need must be registered with the DI services.

To keep the data references out of your MVC app, you could have an IServiceCollection extension method in your Services tier that would register the dbcontext, or it in turn could call an extension method on the data tier.

That way your mvc app only has to have a reference on the services tier.

这篇关于有没有办法让ASP.NET 5依赖注入解析一个没有引用的DbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆