Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用Autofac装饰一种特定类型?_C#_Dependency Injection_Decorator_Autofac - Fatal编程技术网

C# 如何用Autofac装饰一种特定类型?

C# 如何用Autofac装饰一种特定类型?,c#,dependency-injection,decorator,autofac,C#,Dependency Injection,Decorator,Autofac,使用CQS模式,我有一个如下查询: public class MyQuery : IQuery<View<SingleView>> { public string Token { get; set; } public Criteria Criteria { get; set; } } public class MyQueryHandler : IQueryHandler<MyQuery, View<SingleView>> {

使用CQS模式,我有一个如下查询:

public class MyQuery : IQuery<View<SingleView>>
{
    public string Token { get; set; }

    public Criteria Criteria { get; set; }
}

public class MyQueryHandler : IQueryHandler<MyQuery, View<SingleView>>
{
    private readonly IProductRepository _productRepository;

    public MyQueryHandler(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    public View<SingleView> Handle(MyQuery query)
    {
        var data = // get my data

        return data;
    }
}
这是我想为MyQueryHandler使用的装饰器:

public class SaveMyQueryData : IQueryHandler<MyQuery, View<SingleView>>
{
    private readonly IQueryHandler<MyQuery, View<SingleView>> _queryHandler;
    private readonly IProductRepository _productRepository;

    public SaveMyQueryData(
        IQueryHandler<MyQuery, View<SingleView>> queryHandler,
        IProductRepository productRepository)
    {
        _queryHandler = queryHandler;
        _productRepository = productRepository;
    }

    public View<SingleView> Handle(MyQuery query)
    {
        var result = _queryHandler.Handle(query);

        // do something with result

        return result;
    }
}
公共类SaveMyQueryData:IQueryHandler
{
专用只读IQueryHandler\u queryHandler;
私有只读IPProductRepository\u productRepository;
公共存储MyQueryData(
伊奎里汉德勒·奎里汉德勒,
IPProductRepository(产品存储库)
{
_queryHandler=queryHandler;
_productRepository=productRepository;
}
公共视图句柄(MyQuery)
{
var result=\u queryHandler.Handle(查询);
//做一些有结果的事情
返回结果;
}
}

那么,我如何才能使用Autofac将
SaveMyQueryData
注册为
MyQueryHandler
的装饰器呢?

我终于让它按我想要的方式工作了。以下是我的解决方案:

使装饰器打开而不是关闭类型:

public class SaveMyQueryData : IQueryHandler<Q, R>
{
    private readonly IQueryHandler<Q, R> _queryHandler;
    private readonly IProductRepository _productRepository;

    public SaveMyQueryData(
        IQueryHandler<Q, R> queryHandler,
        IProductRepository productRepository)
    {
        _queryHandler = queryHandler;
        _productRepository = productRepository;
    }

    public Q Handle(Q query)
    {
        var result = _queryHandler.Handle(query);
        var view = result as View<SingleView>; 

        // do something with view

        return result;
    }
}
公共类SaveMyQueryData:IQueryHandler
{
专用只读IQueryHandler\u queryHandler;
私有只读IPProductRepository\u productRepository;
公共存储MyQueryData(
伊奎里汉德勒·奎里汉德勒,
IPProductRepository(产品存储库)
{
_queryHandler=queryHandler;
_productRepository=productRepository;
}
公共Q句柄(Q查询)
{
var result=\u queryHandler.Handle(查询);
var view=结果为视图;
//有远见地做事
返回结果;
}
}
然后,在使用Autofac注册类型时,请执行以下操作:

builder.RegisterGenericDecorator(
  typeof(SaveMyQueryData<,>), 
  typeof(IQueryHandler<,>), 
  context => context.ImplementationType == typeof(MyQueryHandler));
builder.RegisterGenericDecorator(
类型(SaveMyQueryData),
类型(IQueryHandler),
context=>context.ImplementationType==typeof(MyQueryHandler));
最后一行是魔法。使用第三个参数
RegisterGenericDecorator
可以指定何时应用decorator的条件。这是自Autofac 4.9以来的新功能,它解决了我的问题

builder.RegisterGenericDecorator(
  typeof(SaveMyQueryData<,>), 
  typeof(IQueryHandler<,>), 
  context => context.ImplementationType == typeof(MyQueryHandler));