1
votes

Looking for some docs/tips on how to use Property Injection in an MVC solution using Castle Windsor.

Implementing a custom membership provider and don't have access to constructor injection. Currently using a service locator to pull the component, but curious about how to set the property with Windsor. Current code:

public class CustomRoleProvider : System.Web.Security.RoleProvider
{
    public IRepository<User> UserRepository
    {
        get { return ServiceLocator.Current.GetInstance<IRepository<User>>(); }
    }

    public override string[] GetRolesForUser(string username)
    {
        var user = UserRepository
                            .Where(x => x.Username == username)
                            .FirstOrDefault();

        return (user==null || user.UserUserRoles==null) ? new string[] { } : user.UserUserRoles.Select(x => x.UserRole.Name).ToArray();
    }
    ...
}
1

1 Answers

1
votes

There is no way to use dependency injection with role providers because your DI framework is not responsible for their instantiate and lifetime management. Service locator is probably the simplest way to go with.

You might actually try passing the Roles.Provider instance to Castle Windsor in order to wire up property injection. You might also take a look at the following article.