[英]How do I register multiple concrete types using assembly scanning in Autofac?
I have an assembly with multiple classes that I'd like to register using assembly scanning in Autofac.我有一个包含多个类的程序集,我想在 Autofac 中使用程序集扫描进行注册。 Some requirements:一些要求:
abstract
classes.它不应该注册abstract
类。 Based on the documentation, it seems like the straightforward way of doing this shown below.根据文档,这似乎是如下所示的直接执行方法。 Note that the Module
implementation is inconsequential here;请注意, Module
实现在这里无关紧要; I'm just showing it for completeness).我只是为了完整性而展示它)。
ContainerBuilder builder;
// ...
builder.RegisterAssemblyTypes(_assembly).AssignableTo<ExpandableDialog>();
builder.RegisterAssemblyTypes(_assembly).AssignableTo<Form>();
Does this cause assembly scanning with each line?这是否会导致每条线的装配扫描? Or can I make this more performant by combining them like so?或者我可以通过像这样组合它们来提高性能吗?
ContainerBuilder builder;
// ...
builder.RegisterAssemblyTypes(_assembly)
.AssignableTo<ExpandableDialog>()
.AssignableTo<Form>();
I feel like the bottom one isn't correct because AssignableTo
uses logical AND, so it would require classes to implement both classes above which is wrong.我觉得最下面的一个不正确,因为AssignableTo
使用逻辑 AND,所以它需要类来实现上面的两个类,这是错误的。 It should be an OR condition.它应该是一个 OR 条件。
What is the correct solution here given my list of requirements above?鉴于我上面的要求列表,这里的正确解决方案是什么?
Usually registration is one time operation and I think in most cases you should not be conserned that much about one time operations performance.通常注册是一次性操作,我认为在大多数情况下,您不应该对一次性操作性能有太多关注。 Also based on the docs description the second one should register only types which are assignable both to Form
and ExpandableDialog
.同样基于文档描述,第二个应该只注册可分配给Form
和ExpandableDialog
。
If you are concerned about multiple scans you can use Where
with predicate:如果您担心多次扫描,您可以使用Where
和谓词:
builder.RegisterAssemblyTypes(_assembly)
.Where(t => t.IsAssignableTo<ExpandableDialog>() || t.IsAssignableTo<Form>())
.AsSelf();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.