Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change Interceptor to handle commands instead of actions.

  • Loading branch information...
commit fefaf039952978ce4c682776b4f4ae32c69dd1b9 1 parent 898a2e8
@lanwin authored
Showing with 29 additions and 28 deletions.
  1. +12 −22 Source/Core/Interceptor.cs
  2. +17 −6 Source/Scratchpad.cs
View
34 Source/Core/Interceptor.cs
@@ -9,12 +9,12 @@ public static class Interceptor
{
static readonly Dictionary<Type, TypeHandler> Handlers = new Dictionary<Type, TypeHandler>();
- public static void RunBefore<TCommandType>(Action<IContext, ICommand> command)
+ public static void RunBefore<TCommandType>(ICommand command)
{
GetOrAddHandler(typeof(TCommandType)).Before.Add(command);
}
- public static void RunAfter<TCommandType>(Action<IContext, ICommand> command)
+ public static void RunAfter<TCommandType>(ICommand command)
{
GetOrAddHandler(typeof(TCommandType)).After.Add(command);
}
@@ -23,13 +23,11 @@ static TypeHandler GetOrAddHandler(Type commandType)
{
TypeHandler handler;
if(Handlers.TryGetValue(commandType, out handler) == false)
- {
Handlers.Add(commandType,
- handler = new TypeHandler
- {
- CommandType = commandType
- });
- }
+ handler = new TypeHandler
+ {
+ CommandType = commandType
+ });
return handler;
}
@@ -47,22 +45,14 @@ static void Execute(IDecoratorCommand command, IContext context)
Handlers.TryGetValue(decorated.GetType(), out handler);
if(handler != null)
- {
- foreach(var action in handler.Before)
- {
- action(context, decorated);
- }
- }
+ foreach(var interceptingCommand in handler.Before)
+ interceptingCommand.Execute(context);
command.Inner.Execute(context);
if(handler != null)
- {
- foreach(var action in handler.After)
- {
- action(context, decorated);
- }
- }
+ foreach(var interceptingCommand in handler.After)
+ interceptingCommand.Execute(context);
}
[DebuggerNonUserCode]
@@ -78,8 +68,8 @@ public void Execute(IContext context)
class TypeHandler
{
- public readonly List<Action<IContext, ICommand>> After = new List<Action<IContext, ICommand>>();
- public readonly List<Action<IContext, ICommand>> Before = new List<Action<IContext, ICommand>>();
+ public readonly List<ICommand> After = new List<ICommand>();
+ public readonly List<ICommand> Before = new List<ICommand>();
public Type CommandType { get; set; }
}
}
View
23 Source/Scratchpad.cs
@@ -423,19 +423,30 @@ public static void Run(this IContext context, ICommand command, Func<ICommand, I
}
}
+ class AddRequiredPackageIds : ICommand
+ {
+ public string[] PackageIds { get; set; }
+
+ public void Execute(IContext context)
+ {
+ var ids = new List<string>(context.Get(PackageKeys.RequiredPackageIds));
+ ids.AddRange(PackageIds);
+ context.Put(PackageKeys.RequiredPackageIds, ids.ToArray());
+ }
+ }
+
public class Runner
{
public void Run()
{
// an plugin would run this
- Interceptor.RunAfter<InitializeRequiredPackageIds>((context, command) =>
+ Interceptor.RunAfter<InitializeRequiredPackageIds>(new AddRequiredPackageIds
{
- var ids = new List<string>(context.Get(PackageKeys.RequiredPackageIds))
+ PackageIds = new[]
{
- "a1",
- "a2"
- };
- context.Put(PackageKeys.RequiredPackageIds, ids.ToArray());
+ "A1",
+ "A2",
+ }
});
using(var context = Context.Empty)
Please sign in to comment.
Something went wrong with that request. Please try again.