[英]Dependency not injected into class by Castle Windsor
I have the following interfaces and base class.我有以下接口和基类。
UserRespository:用户存储库:
public class UserRepository : Repository<User>, IUserRepository
{
public IAuthenticationContext authenticationContext;
public UserRepository(IAuthenticationContext authenticationContext)
:base(authenticationContext as DbContext) { }
public User GetByUsername(string username)
{
return authenticationContext.Users.SingleOrDefault(u => u.Username == username);
}
}
UserService:用户服务:
public class UserService : IUserService
{
private IUserRepository _userRepository;
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public IEnumerable<User> GetAll()
{
return _userRepository.GetAll();
}
public User GetByUsername(string username)
{
return _userRepository.GetByUsername(username);
}
}
Now when I inject the UserService it's _userRepository is null.现在,当我注入 UserService 时,它的 _userRepository 为空。 Any idea what I need to configure to get it to inject the repository correctly.
知道我需要配置什么才能正确注入存储库。
I have the following install code:我有以下安装代码:
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Types.FromAssemblyNamed("DataAccess")
.Where(type => type.Name.EndsWith("Repository") && !type.IsInterface)
.WithServiceAllInterfaces()
.Configure(c =>c.LifestylePerWebRequest()));
//AuthenticationContext authenticationContext = new AuthenticationContext();
}
}
public class ServicesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Types.FromAssemblyNamed("Services")
.Where(type => type.Name.EndsWith("Service") && !type.IsInterface)
.WithServiceAllInterfaces()
.Configure(c => c.LifestylePerWebRequest()));
}
}
How would I go about registering the concrete DbContext's我将如何注册具体的 DbContext
public class AuthenticationContext : DbContext
{
public AuthenticationContext() : base("name=Authentication")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
}
UPDATE更新
When I remove the default constructor in UserService I get the following error:当我删除 UserService 中的默认构造函数时,出现以下错误:
Castle.MicroKernel.Handlers.HandlerException: Can't create component 'DataAccess.Repositories.UserRepository' as it has dependencies to be satisfied.
Castle.MicroKernel.Handlers.HandlerException:无法创建组件“DataAccess.Repositories.UserRepository”,因为它具有要满足的依赖项。 'DataAccess.Repositories.UserRepository' is waiting for the following dependencies: - Service 'DataAccess.AuthenticationContext' which was not registered.
“DataAccess.Repositories.UserRepository”正在等待以下依赖项: - 未注册的服务“DataAccess.AuthenticationContext”。
就我而言,这是因为我在实现接口的类中没有默认构造函数
Based off your exception in your "UPDATE" you need to register your AuthenticationContext class so Windsor knows how to create it.根据“更新”中的异常,您需要注册 AuthenticationContext 类,以便 Windsor 知道如何创建它。
container.Register(
Component.For<AuthenticationContext>()
.ImplementedBy<AuthenticationContext>());
However, based off the UserRepository.cs code it depends on the interface IAuthenticationContext (and not AuthenticationContext) so you would specify the implementation of the interface:但是,基于 UserRepository.cs 代码,它依赖于接口 IAuthenticationContext(而不是 AuthenticationContext),因此您可以指定接口的实现:
container.Register(
Component.For<IAuthenticationContext>()
.ImplementedBy<AuthenticationContext>());
For those looking at this question later, also please make sure that the name of the implementing class starts with the interface name.对于稍后查看此问题的人,还请确保实现类的名称以接口名称开头。
ex:例如:
class FooBarImpl : IFooBar
NOT不是
class Foo : ISomething
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.