0
想知道是否有人有这种情况与Unity(作为IoC容器)类有两个注入依赖项(接口),其中一个依赖项可以为空。例如:通过构造函数注入可空的依赖需要统一注册可空的依赖
public MyServiceDll(IRepository repository, ICanBeNullDependency canBeNullDependency = null)
{
_repository = repository;
_canBeNullDependency = canBeNullDependency;
}
ICanBeNullDependency来自另一个程序集。 MyServiceDll是另一个程序集。 MyServiceDll被web api引用,并将其接口注入到其中一个控制器中。 ICanBeNullDependency可以为空,所以我并不需要注册此接口的实现在unityconfig.cs但是当控制器被调用时,它将错误说:
The current type, ICanBeNullDependency, is an interface and cannot be constructed. Are you missing a type mapping?
我甚至会说“你***应该使用空对象模式。” – Steven