.NET Core 2.0模块化依赖项注入 [英] .NET Core 2.0 Modular Dependency Injection

查看:180
本文介绍了.NET Core 2.0模块化依赖项注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个包含核心和扩展包(如Entity Framework及其数据库提供程序)的库.

I am trying to build a library that has core and extensions packages like Entity Framework and its database providers.

我想做的是当我通过依赖注入注册该库时,我想给出特定的实现作为参数.

What I am trying to do is when I register that library with dependency injection, I want to give specific implementation as a parameter.

想想EF.为了在EF上使用sql provider,我们需要向作为选项参数传递的SQL provider注册它,如下所示.

Think EF. In order to use sql provider on EF we need to register it with SQL provider passed as option parameter like the following.

services.AddDbContext<ApplicationDbContext>(options =>
{
   options.UseSqlServer(Configuration["ConnectionString"]);
});

我想建立类似的结构.可以说我的框架将提供电影制片人.它将具有与框架相关的类的producer.core软件包和两个名为Producer.Extensions.HollywoodProducer.Extensions.Bollywood的扩展软件包.

I would like to build similar structure. Lets say my framework will provide film producer. It will have producer.core package for framework related classes and two extensions package called Producer.Extensions.Hollywood and Producer.Extensions.Bollywood.

如果要使用Hollywood提供程序,则需要安装核心软件包和Hollywood扩展软件包.注册后,它应该看起来像

If I want to use Hollywood provider, I need to install core package and Hollywood extension package. On registration it should look like

services.AddFilmProducer(options =>
{
   options.UseHollywoodProducer();
});

我什至找不到能为我指明方向的关键字.我试图阅读实体框架的源代码,但是对于我的情况来说太复杂了.

I could not find even a keyword that will point me a direction. I tried to read entity framework's source code but it is too complicated for my case.

有人可以为我指明方向吗?

Is there anyone who could point me a direction?

谢谢.

推荐答案

我不确定我是否完全理解您的要求,但是DI和扩展名在.net core中是一件容易的事.

I'm not sure if I completely understand your requirements, but DI and extensions are an easy thing in .net core.

假设您要在Startup.cs中使用它

Let's say you want this in your Startup.cs

services.AddFilmProducer(options =>
{
   options.UseHollywoodProducer();
});

要实现此目的,请创建您的库并添加静态扩展类

To implements this, create your library and add a static extension class

public static class FilmProducerServiceExtensions
{
    public static IServiceCollection AddFilmProducer(this IServiceCollection services, Action<ProducerOptions> options)
    {
        // Create your delegate
        var producerOptions = new ProducerOptions();
        options(producerOptions);

        // Do additional service initialization
        return services;
    }
}

您的ProducerOptions实现可能会出现的位置

where your ProducerOptions implementation might look like

public class ProducerOptions
{
    public void UseHollywoodProducer() 
    { 
        // Initialize hollywood
    }

    public void UseBollywoodProducer() 
    { 
        // Initialize bollywood
    }
}

如果您希望在服务中使用传递的ProducerOptions,则有两种方法可以使用.再次使用依赖项注入,或在扩展方法中使用服务提供商直接访问服务

If you wish to use the passed ProducerOptions in your service, there are two ways to do it. Either use dependency injection again, or directly access the service by using service provider in your extension method

var serviceProvider = services.BuildServiceProvider()
IYourService service = sp.GetService<IYourService>();

现在您可以使用原始的 Use 初始化了.

And now you have the original Use piece of initialization working.

希望有帮助.

要澄清.要将选项注入服务,您可以使用

To clarify. To inject your options in the service, you can use

services.Configure(ProducerOptions);

在您的扩展方法中,并通过

in your extension method, and pass to your service constructor via

public YourService(IOptions<ProducerOptions>)

然后,您可以根据需要尽可能简化或复杂化选择.

You can then simplify or complicate your options as much as you want.

用于此类扩展的有用链接可能是.net核心的CORS存储库: https://github .com/aspnet/CORS

A useful link for this kind of extensions might be the CORS repository for .net core: https://github.com/aspnet/CORS

在评论后

我想我已经明白了.您希望软件包扩展并实现特定的选项,就像serilog对不同的接收器所做的那样.小菜一碟.

I think I've got it now. You want packages to extend and implement specific options, kind of like what serilog does with different sinks. Piece of cake.

取消ProducerOptions实现.

Scrap the ProducerOptions implementation.

假设您有一个带有初始空结构的基本软件包(BaseProducer库)和一个接口

Lets say you have a base package with initial empty structures (BaseProducer library) and an interface

public interface IProducerOptions
{
    // base method signatures
}

您的服务扩展名现在变为

Your service extension now becomes

public static class FilmProducerServiceExtensions
{
    public static IServiceCollection AddFilmProducer(this IServiceCollection services, Action<IProducerOptions> options)
    {
        // Do additional service initialization
        return services;
    }
}

现在,您将创建一个具有特定好莱坞生产者"选项的新程序包,并希望扩展基本选项集

Now you create a new package with specific "Hollywood producer" options and you want to extend the base option set

public static class HollyWoodExtensions
{
    public static void UseHollywoodProducer(this IProducerOptions options)
    {
        // Add implementation
    }
}

根据需要创建尽可能多的程序包和IProducerOptions扩展,添加的方法将开始出现在Startup.cs中.

Create as many packages and IProducerOptions extensions as you like, and the added methods will start appearing in your Startup.cs

services.AddFilmProducer(options =>
{
   options.UseHollywoodProducer();
});

这篇关于.NET Core 2.0模块化依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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