小编Mag*_*eus的帖子

向 Scrutor 注册开放通用装饰器

我有一个标准的 .Net core Api,想要使用 Open Generic IRepository 并用 DomainEventPublisher 装饰它,以便在持久化后将事件推送到 servicsBus。然而,我很早就使用过 Simple Injector,我是它的忠实粉丝。但现在使用 MediatR 时,我尝试仅使用 .net Core DI 和 Scrutor 包进行装饰来简化 DI。

问题是我收到的错误:“提供的泛型参数数量不等于泛型类型定义的数量。” 尝试在 Startup 中注册装饰器时来自 Scrutor(下面第二行)。

services.AddSingleton(typeof(IRepository<>), typeof(Repository<>));
services.Decorate(typeof(IRepository<>), typeof(DomainEventPublisher<>));
Run Code Online (Sandbox Code Playgroud)

我已经关闭了这些通用类/接口,然后它就可以工作了。但我不擅长这个。我会像以前在 Simpleinjector 中那样以正确的方式注册开放通用装饰器。

有什么建议可能是什么问题吗?

 public class Repository<TEntity> : IRepository<TEntity>
{
    private readonly CosmosClient _client;
    private readonly IDataContext<TEntity> _context;
    private readonly Container _container;

    public Repository(CosmosClient client, IDataContext<TEntity> context)
    {
        _client = client;
        _context = context ?? throw new ArgumentNullException(nameof(context));
        _container = _client.GetContainer(_context.GetDatabase(), _context.GetContainer());
    }


    public virtual async Task Add(TEntity entity) …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection decorator scrutor

4
推荐指数
1
解决办法
2746
查看次数

标签 统计

c# ×1

decorator ×1

dependency-injection ×1

scrutor ×1