Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Created August 31, 2011 15:28
Show Gist options
  • Save jakejscott/1183828 to your computer and use it in GitHub Desktop.
Save jakejscott/1183828 to your computer and use it in GitHub Desktop.
TinyIoc Nube
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void InitialiseInternal(TinyIoCContainer container)
{
base.InitialiseInternal(container);
FormsAuthentication.Enable(this, new FormsAuthenticationConfiguration()
{
RedirectUrl = "~/login",
UsernameMapper = container.Resolve<IUsernameMapper>()
});
this.EnableBasicAuthentication(new BasicAuthenticationConfiguration(
container.Resolve<IUserValidator>(), "LootRealm"));
container.AutoRegister(true);
container.Register<TestCommandHandler>();
var commandInvoker = container.Resolve<ICommandInvoker>();
commandInvoker.Execute(new TestCommand() { Username = "foo", Password = "bar" });
}
}
public interface ICommandInvoker
{
void Execute<T>(T command);
}
public interface ICommandHandler<T>
{
void Handle(T command);
}
public class CommandInvoker : ICommandInvoker
{
public void Execute<T>(T command)
{
var handlers = TinyIoCContainer.Current.ResolveAll<ICommandHandler<T>>();
if (!handlers.Any())
throw new Exception("EEEK");
foreach (var handler in handlers)
{
handler.Handle(command);
}
}
}
public class TestCommand
{
public string Username { get; set; }
public string Password { get; set; }
}
public class TestCommandHandler : ICommandHandler<TestCommand>
{
public void Handle(TestCommand e)
{
Trace.WriteLine("Recieved a test command " + e.Username + " " + e.Password);
Debug.WriteLine("Recieved a test command " + e.Username + " " + e.Password);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment