当类继承自同一基类时,使用温莎城堡注入依赖项
我有两个继承自同一个基类的类。
public class UserDetailValidator : BaseValidator<UserDetail>{
public UserDetailValidator(IRepository<Person, Guid> userRepository, AddressValidator addressValidator)
{
RuleFor(x => x.FirstName).Length(1, 10);
}
}
public class AddressValidator : BaseValidator<Address>
当我尝试从 WindsorServiceLocator 获取 UserDetailValidator 时,出现错误“
缺少依赖项”。 组件 UserDetailValidator 依赖于 AddressValidator,无法解析。 确保依赖项在容器中正确注册为服务,或作为内联参数提供。我在我的 ValidationInstaller 中使用以下内容。
container.Register(
AllTypes.FromAssemblyNamed("Validation")
.IncludeNonPublicTypes()
.BasedOn(typeof(IValidator<>))
.WithService.AllInterfaces()
.LifestyleTransient()
IRepository 组件的注入没有任何问题。只是AddressValidator没有进来。我哪里做得不对?我正在使用温莎城堡3.0
I have two classes that inherit from the same base class.
public class UserDetailValidator : BaseValidator<UserDetail>{
public UserDetailValidator(IRepository<Person, Guid> userRepository, AddressValidator addressValidator)
{
RuleFor(x => x.FirstName).Length(1, 10);
}
}
public class AddressValidator : BaseValidator<Address>
When I try and get the UserDetailValidator from WindsorServiceLocator I get the error
Missing dependency.
Component UserDetailValidator has a dependency on AddressValidator, which could not be resolved.
Make sure the dependency is correctly registered in the container as a service, or provided as inline argument. I'm using the following in my ValidationInstaller.
container.Register(
AllTypes.FromAssemblyNamed("Validation")
.IncludeNonPublicTypes()
.BasedOn(typeof(IValidator<>))
.WithService.AllInterfaces()
.LifestyleTransient()
The IRepository component is being injected with no problems. It's only the AddressValidator that does not come in. What am I not doing properly? I'm using Castle Windsor 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更改了传递的参数的类型
从 AddressValidator 到 IValidator 到 UserDetailValidator。
我仍然必须使用 AllInterfaces (至少在我到目前为止的测试中)但是
现在可以了。
I changed the type of the Argument being passed
to UserDetailValidator from AddressValidator to IValidator.
I still have to use AllInterfaces (at least in my testing so far) but
it now works.