使用ASP.NET Core DI的MediatR

时间:2016-02-15 12:55:28

标签: c# asp.net asp.net-core mediatr

我正在使用新的ASP.NET Core,目前正在创建一个我想从JavaScript前端调用的API。

我想使用中介模式来减少耦合,我从Jimmy Bogard找到了库MediatR

我的问题在于使用DI中的内置进行连接,我已经尝试查看examples,但无法看到它如何绑定到启动类中的ConfigureServices方法。

有人有任何见解吗?

更新:我从我的ConfigureService方法开始工作:

services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));

services.Scan(scan => scan
        .FromAssembliesOf(typeof(IMediator), typeof(MyHandler.Handler))
        .AddClasses()
        .AsImplementedInterfaces());

4 个答案:

答案 0 :(得分:25)

截至2016年7月,MediatR的作者Jimmy Bogard发布了一个用于注册MediatR和Handlers的软件包,其中包含ASP.Net Core DI服务(实际上是接口IServiceCollection,在{{{ 1}}并且不限于仅在ASP.Net Core中使用)。

<强> MediatR.Extensions.Microsoft.DependencyInjection

Link to GitHub Project

Link to NuGet Package information

可以找到介绍该软件包及其功能的博客文章here

直接从(非常短的)博客文章中复制的注册示例:

Microsoft.Extensions.DependencyInjection

该软件包执行多个功能来启用MediatR,包括处理程序所需的程序集扫描:

  

您既可以传入处理程序所在的程序集,也可以从这些处理程序所在的程序集中传入Type对象。该扩展将IMediator接口添加到您的服务,所有处理程序和正确的委托工厂以加载处理程序。然后在您的控制器中,您可以使用IMediator依赖项:

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();

  services.AddMediatR(typeof(Startup));
}

答案 1 :(得分:3)

我的工作正常,我的代码:

public void ConfigureServices(IServiceCollection services)
{
      services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));
      services.AddScoped<MultiInstanceFactory>(p => t => p.GetRequiredServices(t));
      services.Scan(scan => scan
              .FromAssembliesOf(typeof(IMediator), typeof(MyHandlerOne.Handler))
              .FromAssembliesOf(typeof(IMediator), typeof(MyHandlerTwo.Handler))
             .AddClasses()
             .AsImplementedInterfaces());
}

我有一个实现MultiInstanceFactory所需的GetRequiredService的类:

public static class GetServices
{
    public static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
    {
        return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
    }
}

答案 2 :(得分:0)

我创建了一个DI helper for ASP.NET Core RC2,可以添加到您的启动中。它为您提供了基于约定的基本映射,因此如果您有类似的类:

MyClass : IMyClass

它会将IMyClass映射到IOC容器中,这将使其可用于注射。

我还添加了MediatR的映射。

使用它只需将类添加到项目中,然后在startup.cs类中添加ConfigureServices()方法所需的行:

public void ConfigureServices(IServiceCollection services)
{
    //Other Code here......

    var ioc = new PearIoc(services);

    //ioc.AddTransient<IEmailSender, AuthMessageSender>();
    //ioc.AddTransient<ISmsSender, AuthMessageSender>();

    ioc.WithStandardConvention();
    ioc.WithMediatR();
    ioc.RunConfigurations();
}

我添加了AddTransient()方法只是为了方便(您也可以使用services.AddTransient())但是它还会公开IServiceCollection,以防您需要对其进行更多操作。

您也可以像对.WithMediatR()扩展名一样对其进行扩展,并编写自己的自定义映射。

答案 3 :(得分:0)

https://dotnetcoretutorials.com/有一个很好的教程。这是正确安装和配置MediatR的示例代码。

安装MediatR

我们要做的第一件事是安装MediatR nuget软件包。因此,从您的包管理器控制台中运行:

安装包MediatR

我们还需要安装一个软件包,以使我们能够利用.NET Core中的内置IOC容器,以发挥我们的优势(我们将在不久后看到更多内容)。因此,还要安装以下软件包:

安装包MediatR.Extensions.Microsoft.DependencyInjection

最后,我们打开我们的startup.cs文件。在我们的ConfigureServices方法中,我们需要添加一个调用以注册MediatR的所有依赖项。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMediatR(Assembly.GetExecutingAssembly());
    //Other injected services. 
}

这是链接:https://dotnetcoretutorials.com/2019/04/30/the-mediator-pattern-part-3-mediatr-library/

我希望这会有所帮助。