Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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# 批注册开放泛型实现_C#_Asp.net_Dependency Injection_Simple Injector - Fatal编程技术网

C# 批注册开放泛型实现

C# 批注册开放泛型实现,c#,asp.net,dependency-injection,simple-injector,C#,Asp.net,Dependency Injection,Simple Injector,我正试图以一种经得起未来考验的方式编写我的合成根目录,这样,当我添加更多使用SimpleInjector 4实现基本实体的命令时,它们会自动被拾取 域类如下所示: public interface ICommandHandler<TCommand> { . . . } public class UpdateCommandHandler<T> : ICommandHandler<UpdateCommand<T>> where T : En

我正试图以一种经得起未来考验的方式编写我的合成根目录,这样,当我添加更多使用SimpleInjector 4实现基本实体的命令时,它们会自动被拾取

域类如下所示:

public interface ICommandHandler<TCommand> { . . . }

public class UpdateCommandHandler<T> : ICommandHandler<UpdateCommand<T>> 
    where T : EntityBase { . . . }

public class UpdateCommand<T> where T : EntityBase { . . . }

public abstract class EntityBase { . . . }
// This works for all cases without as base entity e.g:
// private ICommandHandler<AddBlogEntryCommentCommand> addBlogEntryCommentCommandHandler;
container.Register(typeof(ICommandHandler<>), new[] { typeof(ICommandHandler<>).Assembly });


// And this works for when commands have a base entity but how do to I write it in a 
// generic, future-proof way like above?

container.Register<ICommandHandler<DeleteCommand<BlogEntryComment>>, DeleteCommandHandler<BlogEntryComment>>();
container.Register<ICommandHandler<UpdateCommand<BlogEntry>>, UpdateCommandHandler<BlogEntry>>();
container.Register<ICommandHandler<UpdateCommand<BlogEntryFile>>, UpdateCommandHandler<BlogEntryFile>>();
公共接口ICommandHandler{…}
公共类UpdateCommandHandler:ICommandHandler
其中T:EntityBase{…}
公共类UpdateCommand,其中T:EntityBase{…}
公共抽象类EntityBase{…}
然后,我的API控制器使用命令处理程序,如下所示:

// With base entity
private ICommandHandler<UpdateCommand<BlogEntry>> updateBlogEntryCommandHandler;
// Without base entity
private ICommandHandler<AddBlogEntryCommentCommand> addBlogEntryCommentCommandHandler;
//带有基本实体
私有ICommandHandler updateBlogEntryCommandHandler;
//无基础实体

私有ICommandHandler,但它没有回答我的问题。

您可以使用开放泛型类型注册开放泛型实现的封闭泛型版本,而不是逐个进行所有注册:

container.Register(typeof(ICommandHandler<>), typeof(DeleteCommandHandler<>));
container.Register(typeof(ICommandHandler<>), typeof(UpdateCommandHandler<>));
container.Register(typeof(ICommandHandler),typeof(DeleteCommandHandler));
Register(typeof(ICommandHandler),typeof(UpdateCommandHandler));
Simple Injector将自动为您关闭这些类型。只有在添加新的泛型实现时,才能将其作为注册添加到YOUR组合根目录

如果希望添加许多泛型类型,可以使用以下代码一次注册所有实现:

var types = container.GetTypesToRegister(typeof(ICommandHandler<>),
    new[] { typeof(ICommandHandler<>).Assembly },
    new TypesToRegisterOptions { IncludeGenericTypeDefinitions = true });

container.Register(typeof(ICommandHandler<>),
    types.Where(t => !t.IsGenericTypeDefinition));

foreach (Type type in types.Where(t => t.IsGenericTypeDefinition))
{
    container.Register(typeof(ICommandHandler<>), type);
}
var types=container.getTypeStoreRegister(typeof(ICommandHandler),
新[]{typeof(ICommandHandler.Assembly},
新的TypeStoreRegisterOptions{IncludeGenericTypeDefinitions=true});
容器寄存器(类型为(ICommandHandler),
其中(t=>!t.IsGenericTypeDefinition));
foreach(types.Where(t=>t.IsGenericTypeDefinition)中的类型)
{
容器寄存器(typeof(ICommandHandler),type);
}

由于v4.0(即v4.1)中的限制
寄存器
寄存器(类型,IEnumerable)
不允许将打开的泛型类型传递到类型列表中。这就是为什么您必须单独注册泛型类型(目前)。

第一个代码段可以正常工作,但第二个代码段不行
GetTypeStoreRegister
要求第二个参数为IEnumerable of Assembly。所以我尝试了
var types=container.getTypeStoreRegister(typeof(ICommandHandler),new[]{typeof(ICommandHandler).Assembly},new typeStoreRegisterOptions{IncludeGenericTypeDefinitions=true})
但这会引发以下错误:
提供的类型列表包含一个或多个打开的泛型类型,但此方法无法处理打开的泛型类型,因为它只能将关闭的泛型服务类型映射到单个实现。
@CatsPajamas:Appologies。更新了一百万张,效果很好。我正在关注您在Github repo上的公开问题,以便在您修复代码时更新代码。干杯