.net 如何创建自定义独立规则以检查DI注册
我们使用Autofac作为依赖注入的容器。我们希望使用NDepend检查各种情况,以确保我们的DI设置正确并且没有被滥用(我们有一个非常大的解决方案) 在单元测试中,我可能会采取一种方法:.net 如何创建自定义独立规则以检查DI注册,.net,autofac,ndepend,.net,Autofac,Ndepend,我们使用Autofac作为依赖注入的容器。我们希望使用NDepend检查各种情况,以确保我们的DI设置正确并且没有被滥用(我们有一个非常大的解决方案) 在单元测试中,我可能会采取一种方法: private static IEnumerable<TestCaseData> TestCases { get { return from registration in Container.Value .ComponentRegistry
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);
}
}
如何在NDepend中创建自定义规则,以确保所有适当的类型都已注册到Autofac容器
谢谢,
Richard从评论中,我可以看出我将编写自己的集成测试,但希望将来在独立测试中支持这一点。这些注册通常是动态的,这意味着您必须运行应用程序来检查其是否正常工作(与单元测试一样)。然而,NDepend使用静态代码分析;它不会运行你的代码。我不认为你会为此成功地使用NDepend(或任何静态代码分析工具),或者至少你会滥用这个工具,让自己变得异常困难。谢谢。这是有道理的。但一定要让你的问题保持开放。我很想听听NDepend先生(patrick)对此有什么看法。Richard,正如Steven所说,NDepend专注于静态分析,还不能很好地与DI框架配合。但是,我们在全新的用户语音页面上有一个开放的建议,建议将来使用此功能,请随意投票:
[Test]
[TestCaseSource("TestCases")]
public void CanBeResolved(
IComponentRegistration componentRegistration,
TypedService typedService)
{
using (var scope = Container.Value.BeginLifetimeScope())
scope.Resolve(typedService.ServiceType);
}