我们使用 Autofac 作为依赖注入(inject)的容器。我们想使用 NDepend 检查各种事情,以确保我们的 DI 设置正确并且没有被滥用(我们有一个非常大的解决方案)。
在单元测试中,我可能会采取一种方法:
private static IEnumerable<TestCaseData> TestCases
{
get
{
return from registration in Container.Value
.ComponentRegistry.Registrations
from service in registration.Services
where service is TypedService
orderby service.Description
select new TestCaseData(registration, service)
.SetName(service.Description);
}
}
然后:
[Test]
[TestCaseSource("TestCases")]
public void CanBeResolved(
IComponentRegistration componentRegistration,
TypedService typedService)
{
using (var scope = Container.Value.BeginLifetimeScope())
scope.Resolve(typedService.ServiceType);
}
我如何在 NDepend 中创建自定义规则以确保所有适当的类型都已注册到 Autofac 容器?
谢谢, 理查德
最佳答案
从评论中,我可以看到我将编写自己的集成测试,但希望将来在 NDepend 中支持它。
关于.net - 如何创建自定义 NDepend 规则来检查 DI 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825832/