2016-04-30 136 views
0

我有以下接口和基類。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.

+0

你有沒有嘗試刪除默認的構造函數來自'UserService'?擁有多個構造函數[是一種反模式](https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=97)。 – Steven

+0

如果我刪除構造函數,UserService將不會注入到我的控制器中。 – Wilds

+0

那麼當你刪除默認的構造函數時你得到的錯誤是什麼? – Steven

回答

0

基於關閉您的例外在你的「UPDATE」你需要這樣溫莎知道如何創建它註冊AuthenticationContext類。

container.Register(
    Component.For<AuthenticationContext>() 
     .ImplementedBy<AuthenticationContext>()); 

然而,根據關閉它取決於接口IAuthenticationContext(而不是AuthenticationContext)上,所以你會指定接口的實現UserRepository.cs代碼:

container.Register(
    Component.For<IAuthenticationContext>() 
     .ImplementedBy<AuthenticationContext>()); 
相關問題