私は以下のインターフェースと基本クラスを持っています。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の:私は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);
}
}
は、今では_userRepositoryがnullです。 リポジトリを正しく注入するために設定する必要があることを考えてください。
私は、コードをインストールするには、次のしている:私はデフォルトを削除すると
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のコンストラクタrvice私は次のエラーを取得:
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