Created
October 25, 2012 14:13
-
-
Save AnthonyMastrean/3952776 to your computer and use it in GitHub Desktop.
Request handler redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAmAnAction | |
{ | |
void Handle(dynamic data = null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrchestrationAction : IAmAnAction | |
{ | |
readonly IDoSomething _collaborator; | |
public OrchestratorOrchestrationAction(IDoSomething collaborator) | |
{ | |
_collaborator = collaborator; | |
} | |
public void Handle(dynamic data) | |
{ | |
_collaborator.execute(data.Number); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Q<T> | |
where T : IAmAnAction | |
{ | |
private readonly IContainer _container; | |
private readonly Queue<Action> _queue = new Queue<Action>(); | |
public Q(IContainer container) | |
{ | |
_container = container; | |
} | |
public void Enqueue<TCommand>(dynamic data) | |
where TCommand : T | |
{ | |
_queue.Enqueue(() => _container.GetInstance<TCommand>().Handle(data)); | |
} | |
public void Consume() | |
{ | |
_queue.Dequeue()(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Runner | |
{ | |
static Q<IAmAnAction> _enqueue; | |
public static void QueueAnEvent() | |
{ | |
dynamic data = new ExpandoObject(); | |
data.Number = 10; | |
_enqueue.Enqueue<OrchestrationAction>(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment