Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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#_Autofac_Command Pattern - Fatal编程技术网

C# Autofac与通用命令模式

C# Autofac与通用命令模式,c#,autofac,command-pattern,C#,Autofac,Command Pattern,我试图装饰我的命令处理程序,并试图在我的处理器中解决它们 我这样注册了我的命令: builder.RegisterAssemblyTypes(typeof(ICommandProcessor).Assembly) .AsClosedTypesOf(typeof(ICommandHandler<,>)) .AsSelf().AsImplementedInterfaces().Named("implementor", typeof(ICommandHandler<,

我试图装饰我的命令处理程序,并试图在我的处理器中解决它们

我这样注册了我的命令:

builder.RegisterAssemblyTypes(typeof(ICommandProcessor).Assembly)
    .AsClosedTypesOf(typeof(ICommandHandler<,>))
    .AsSelf().AsImplementedInterfaces().Named("implementor", typeof(ICommandHandler<,>));

builder.RegisterGenericDecorator(
    typeof(CatchValidationErrorsDecorator<,>),
    typeof(ICommandHandler<,>), fromKey: "implementor")
    .AsImplementedInterfaces();
var handerType = typeof (ICommandHandler<,>)
    .MakeGenericType(command.GetType(), typeof (TResult));
dynamic handler = _container.Resolve(handerType);
builder.RegisterAssemblyTypes(typeof(ICommandProcessor).Assembly)
.AsClosedTypesOf(typeof(ICommandHandler))
.AsSelf().AsImplementedInterfaces()。命名为(“实现者”,类型为(ICommandHandler));
builder.RegisterGenericDecorator(
类型(CatchValidationErrorsDecorator),
typeof(ICommandHandler),fromKey:“实现者”)
.a实现接口();
问题是,当我不使用命名扩展时,泛型装饰器不起作用。 使用命名扩展时,我无法解析如下组件:

builder.RegisterAssemblyTypes(typeof(ICommandProcessor).Assembly)
    .AsClosedTypesOf(typeof(ICommandHandler<,>))
    .AsSelf().AsImplementedInterfaces().Named("implementor", typeof(ICommandHandler<,>));

builder.RegisterGenericDecorator(
    typeof(CatchValidationErrorsDecorator<,>),
    typeof(ICommandHandler<,>), fromKey: "implementor")
    .AsImplementedInterfaces();
var handerType = typeof (ICommandHandler<,>)
    .MakeGenericType(command.GetType(), typeof (TResult));
dynamic handler = _container.Resolve(handerType);
var handerType=typeof(ICommandHandler)
.MakeGenericType(command.GetType(),typeof(TResult));
动态处理程序=_container.Resolve(handerType);

有人知道如何解决这个问题吗?

这让我过去很头疼。最终为我做到这一点的注册是:

var assembly = typeof(ICommandProcessor).Assembly);

builder.RegisterAssemblyTypes(assembly).As(type =>
    from interfaceType in type.GetInterfaces()
    where interfaceType.IsClosedTypeOf(typeof(ICommandHandler<,>))
    select new KeyedService("implementor", interfaceType));

builder.RegisterGenericDecorator(
    typeof(CatchValidationErrorsDecorator<,>), 
    typeof(ICommandHandler<,>),
    fromKey: "implementor");
var assembly=typeof(ICommandProcessor.assembly);
builder.RegisterAssemblyTypes(assembly).As(type=>
来自type.GetInterfaces()中的interfaceType
其中interfaceType.IsClosedTypeOf(typeof(ICommandHandler))
选择新的KeyedService(“实现者”,interfaceType));
builder.RegisterGenericDecorator(
类型(CatchValidationErrorsDecorator),
类型(ICommandHandler),
fromKey:“实施者”);

谢谢您的回复。成功了!还有一个问题,如果我需要在命令中处理一个命令,并且不希望在另一个命令中触发commandhandler时对其进行修饰,该怎么办?@Manaus我建议不要这样做。命令应该是一个原子操作,ICommandHandler应该是一个整体抽象。您应该将重复代码提取到注入处理程序的服务中。根据层次结构中的位置有条件地包装处理程序非常困难,而且容易出错,即使使用其他容器也是如此。