我们将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/