更新时间:2022-12-11 11:42:42
您需要使用 DependencyResolver
类在ASP.NET MVC。如果您正确连接您的容器 DependencyResolver.Current
将使用您的容器来解决的依赖关系。
You need to use DependencyResolver
class in ASP.NET MVC. If you wire your container correctly DependencyResolver.Current
will use your container to resolve the dependencies.
public class MustBeValidWarehouse : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value is string)
{
string warehouse = value.ToString();
IInventService inventserv = DependencyResolver.Current.GetService<IInventService>();
return (inventserv.GetWarehouses().Where(m => m.WarehouseId == warehouse).Count() != 0);
}
return false;
}
}
在你的类的测试,你可以为 DepedencyResolver.Current
这样的模拟:
In your class tests you can provide a mock for DepedencyResolver.Current
like this:
DependencyResolver.SetResolver(resolverMock);