Read through you tutorials and great work! Have a question for you though, that I can't seem to find an answer in google. On decorators. How do you provide state dependencies that are unknown at configuration time? So I ask for an IRepository and I have configuration setup to wrap it in a IValidator and maybe an ISecurity. However, ISecurity has a dependency on a runtime determined role (say from multiple sources, possibly including a state value on IWidget) and the user id (for argument sake isn't available on the context). I want to call T Get() on IRepository. How do you get it all setup? Many Thanks!
Read through you tutorials and great work!
Have a question for you though, that I can't seem to find an answer in google. On decorators. How do you provide state dependencies that are unknown at configuration time?
So I ask for an IRepository and I have configuration setup to wrap it in a IValidator and maybe an ISecurity. However, ISecurity has a dependency on a runtime determined role (say from multiple sources, possibly including a state value on IWidget) and the user id (for argument sake isn't available on the context).
I want to call T Get() on IRepository. How do you get it all setup?
Many Thanks!
public interface IRepository<T>
where T : class, new()
{
T Get(int id);
}
public class StubRepository<T> : IRepository<T>
public T Get(int id)
return new T();
public class SecurityDecorator<T> : IRepository<T>
private readonly IRepository<T> _inner;
public SecurityDecorator(IRepository<T> inner)
_inner = inner;
return _inner.Get(id);
public class Widget
public class ContainerTests
private readonly IWindsorContainer container;
public ContainerTests()
container = new WindsorContainer();
container.Register(Component.For(typeof (IRepository<>)).ImplementedBy(typeof (SecurityDecorator<>)));
container.Register(Component.For(typeof (IRepository<>)).ImplementedBy(typeof (StubRepository<>)));
[Fact]
public void GetRepository_ForWidget_ReturnsSecurityDecoratorOfTypeWidget()
var widgetRepository = container.Resolve<IRepository<Widget>>();
Assert.True(widgetRepository is SecurityDecorator<Widget>);
public interface IUser
public IUser User { get; set; } // <-- the current user
private readonly IUser user = MockRepository.GenerateStub<IUser>();
public void GetRepository_ForWidget_WhenSupplyingUserInjectsUserIntoSecurityRepository()
var securityDecoratorForWidget = (SecurityDecorator<Widget>) container.Resolve<IRepository<Widget>>(new {User = user});
Assert.Same(user, securityDecoratorForWidget.User);
public interface IRole
bool IsMember(IUser user);
CheckPermission();
public IUser User { get; set; }
public IList<IRole> Roles { get; set; } // <-- the roles to check
private void CheckPermission()
if (User == null || Roles == null) return;
if (Roles.Any(role => !role.IsMember(User)))
throw new Exception("You do not have permission");
private readonly IRole failingRole = MockRepository.GenerateStub<IRole>();
private readonly IRole passingRole = MockRepository.GenerateStub<IRole>();
passingRole.Stub(stub => stub.IsMember(user)).Return(true);
public void Get_ForWidgetWhenUserMatchesAllRoles_ReturnsWidget()
var securityDecoratorForWidget =
(SecurityDecorator<Widget>) container.Resolve<IRepository<Widget>>(new {User = user, Roles = new List<IRole> {passingRole}});
Assert.NotNull(securityDecoratorForWidget.Get(1));
public void Get_ForWidgetWhenUserDoesNotMatchAllRoles_ThrowsException()
(SecurityDecorator<Widget>) container.Resolve<IRepository<Widget>>(new {User = user, Roles = new List<IRole> {passingRole, failingRole}});
var exception = Assert.Throws<Exception>(() => securityDecoratorForWidget.Get(1));
Assert.Equal("You do not have permission", exception.Message);