- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
查看 wiki对于 DryIoc,示例显示的似乎与我需要的相反,我想知道是否可能相反?
Wiki (部分示例)
public interface X {}
public interface Y {}
public class A : X, Y {}
public class B : X, IDisposable {}
// Registers X, Y and A itself with A implementation
container.RegisterMany<A>();
...
我想做以下事情
container.RegisterMany<X>();
// This would return one implementation each of A and B
container.ResolveMany<X>();
然而,这给出了这个错误:“注册抽象实现类型 X 应该是具体的。也没有 FactoryMethod 可以代替使用。”
这是否可能开箱即用,还是我需要通过从程序集中获取接口(interface)的所有实现并循环遍历并相应地注册来自己实现它?
更新
我发现这个示例对于我的情况来说可能有点简单,但对于上面的示例,@dadhi 提供的代码非常有效。
这里有一个更“复杂”的例子
namespace Assembly.A
{
interface IImporter { }
abstract class ImporterBase : IImporter { }
}
namespace Assembly.B
{
interface IStuffImporter : IImporter { }
interface IOtherImporter : IImporter { }
class StuffImporter : ImporterBase, IStuffImporter { }
class OtherImporter : ImporterBase, IOtherImporter { }
}
namespace Assembly.C
{
class Program
{
void Main()
{
var container = new Container();
container.RegisterMany( new[] { typeof(StuffImporter).Assembly }, type => type.IsAssignableTo(typeof(IImporter)) && type.IsClass && !type.IsAbstract);
//I would like DryIoc to do the following behind the scenes
//container.Register<IStuffImporter, StuffImporter>();
//container.Register<IOtherImporter, OtherImporter>();
var importers = container.ResolveMany<IImporter>();
importers.Should().HaveCount(2);
importers.First().Should().BeOfType<StuffImporter>().And.BeAssignableTo<IStuffImporter>();
importers.Last().Should().BeOfType<OtherImporter>().And.BeAssignableTo<IOtherImporter>();
}
}
}
像这样的东西是否可以开箱即用?或者我需要制作自己的扩展方法等吗? 真的不会有那么大的麻烦,我可能会在短时间内完成它,但是我想知道以供将来引用并学习 DryIoc 容器。提前致谢。 :)
最佳答案
RegisterMany
重载接受程序集和服务类型条件(wiki 中的示例之一):
container.RegisterMany(new[] { typeof(A).Assembly },
serviceTypeCondition: type => type == typeof(X));
上面注册了 A 的汇编中 X 的所有实现。
关于DryIoc Register接口(interface)的多种实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42368192/
我有一个以 DryIoc 作为容器的 Prism 应用程序。 我要 IHttpClientFactory提供HttpClient s 给我的 输入客户 ,它们是这样的: public class Ex
我正在尝试使用 DryIOC 实现事件聚合器。我有一个事件调度程序,如下所示: public class DryIocEventDispatcher : IEventDispatcher {
StructureMap 在 Container 类上有一个非常有用的调试方法,称为 WhatDoIHave() 它显示了容器中的每种类型及其生命周期、guid 和一堆其他信息。这对调试很有用。 这里
我想注册两个服务,A & B ,有不同的生命周期。 我的用例类似于“浏览器”和“标签”。 我在可执行生命周期(浏览器)上有一个范围,每个选项卡都有一个“子范围”。 我要 A成为浏览器范围内的单例(浏览
假设我有这项服务和两种策略: public class SomeService : ISomeService { public SomeService(ICurrentDbContext co
我正在尝试将对象解析为 Func<> 的结果.但我不知道该怎么做。 例如,这里我有一个变量(函数参数)lazyInt (这可能是某种长时间运行计算的结果)并且在以后可能使用之前不需要哪个结果。
尝试在 Prism.Forms ( https://github.com/joacar/Prism/tree/pr-517 ) 中构建对 DryIoc 的支持,并偶然发现了一个我无法弄清楚该怎么做的场
我在使用 DryIoC 时遇到了一个相当令人费解的情况。 好吧,实际上,这是我第一次使用 IoC 容器,所以我可能只是误解了一切:从依赖注入(inject)到 IoC 容器,再到 DryIoC 本身。
嘿,我已经从 Autofac 切换到 DryIoc。我的代码以前可以工作,但现在导致异常“解析时检测到递归依赖项”。 (代码已简化) public class AFactory { public
查看 wiki对于 DryIoc,示例显示的似乎与我需要的相反,我想知道是否可能相反? Wiki (部分示例) public interface X {} public interface Y {}
查看 wiki对于 DryIoc,示例显示的似乎与我需要的相反,我想知道是否可能相反? Wiki (部分示例) public interface X {} public interface Y {}
给出下面的小例子,有没有办法在 MyService2 中标记(属性,名称约定,...)MyInterface 参数,以便它解析正确,或者是传递 MyInterface[] 的唯一方法?我知道CaSTL
我有一个 Container注册一个 Presenter View 类: Container.Register(); Presenter的构造函数为其 View 接受一个参数: public List
我正在探索 DryIoc 在 .NET WebAPI 应用程序中的使用,并注意到初始化步骤有一个奇怪的行为。在一个简单的测试 webapi 应用程序中,我有以下 DryIoc 注册类,它在 WebAp
问题 我正在尝试使用 DryIoc 注册一个单例,但容器返回了我的单例类的多个实例。单例类注册为多个不同服务接口(interface)的实现类型。当从 DryIoc 请求任何上述服务接口(interf
很抱歉,我没有太多代码可以分享,因为我不确定这将如何工作。 我有一个名为 API.Tests 的测试项目,我正在 API 项目中为 NewsController 编写测试。我不确定如何将依赖项从测试传
我想使用一些具有良好性能结果的 .NET IoC 容器。我读了this article关于 IoC 容器性能,DryIoc 和 LightInject 似乎是最好的。但是我没有找到关于它们的一些评论,
我想在容器中注册一个类的多个实例。每个实例都由其 ID 属性标识。 public interface MyInterface { string Id { get; } } public cla
我想将 Microsoft.Extensions.Logging 与 DryIoc 容器一起使用。 默认方式是将工厂注册为实例,注入(inject)并创建记录器: ILoggerFactory log
我们目前正在使用 Autofac 并利用 AutofacSerilogIntegration 在将 ILogger 注入(inject)混凝土时自动设置日志记录上下文。集成使用记录器注入(inject
我是一名优秀的程序员,十分优秀!