Dependency not injected into class by Castle Windsor

4.3k Views Asked by At

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. 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

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:

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.

3

There are 3 best solutions below

0
On

In my case it was because I didn't have default constructor in the class that implements the Interface

0
On

Based off your exception in your "UPDATE" you need to register your AuthenticationContext class so Windsor knows how to create it.

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:

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

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