城堡温莎没有注入课堂的依赖性

时间:2016-04-30 18:56:38

标签: c# castle-windsor windsor-3.0

我有以下接口和基类。

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为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&#39>

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; }
}

更新

当我删除UserService中的默认构造函数时,我收到以下错误:

  

Castle.MicroKernel.Handlers.HandlerException:无法创建组件&#39; DataAccess.Repositories.UserRepository&#39;因为它有依赖性来满足。 &#39; DataAccess.Repositories.UserRepository&#39;正在等待以下依赖项: - Service&#39; DataAccess.AuthenticationContext&#39;没有注册。

3 个答案:

答案 0 :(得分:1)

在我的情况下,这是因为我在实现Interface的类中没有默认构造函数

答案 1 :(得分:0)

在你的&#34; UPDATE&#34;中基于您的例外情况您需要注册您的AuthenticationContext类,以便Windsor知道如何创建它。

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

但是,根据UserRepository.cs代码,它依赖于接口IAuthenticationContext(而不是AuthenticationContext),因此您将指定接口的实现:

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

答案 2 :(得分:0)

对于那些稍后查看此问题的人,还请确保实现类的名称以接口名称开头。

例如:

class FooBarImpl : IFooBar

不是

class Foo : ISomething

相关问题