我有以下接口和基類。Castle Windsor沒有注入類的依賴關係
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);
}
}
現在,當我注入UserService它的_userRepository爲空。 任何想法我需要配置讓它正確注入存儲庫。
我有以下安裝代碼:
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()));
}
}
我怎麼會去註冊的具體的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
當我刪除默認UserSe中的構造函數起動轉矩大,我得到以下錯誤:
Castle.MicroKernel.Handlers.HandlerException: Can't create component 'DataAccess.Repositories.UserRepository' as it has dependencies to be satisfied. 'DataAccess.Repositories.UserRepository' is waiting for the following dependencies: - Service 'DataAccess.AuthenticationContext' which was not registered.
你有沒有嘗試刪除默認的構造函數來自'UserService'?擁有多個構造函數[是一種反模式](https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=97)。 – Steven
如果我刪除構造函數,UserService將不會注入到我的控制器中。 – Wilds
那麼當你刪除默認的構造函數時你得到的錯誤是什麼? – Steven