Last active
December 18, 2015 12:59
-
-
Save luisrudge/5786786 to your computer and use it in GitHub Desktop.
scriptcs idea
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
//Just for the sake of constraint | |
public interface INakeTaskConfiguration { } | |
//NakeTask base class | |
public abstract class NakeTask | |
{ | |
protected readonly IDictionary<string, Action> Do = new Dictionary<string, Action>(); | |
public virtual void Initialize() | |
{ | |
SetMeUp(); | |
} | |
/// <summary> | |
/// What should this task do? | |
/// </summary> | |
protected abstract void SetMeUp(); | |
public void Run() | |
{ | |
foreach (var d in Do) | |
{ | |
Console.WriteLine("Running [{0}]", d.Key); | |
d.Value(); | |
} | |
} | |
} | |
//NakeTask base class (with configuration) | |
public abstract class NakeTask<T> where T : INakeTaskConfiguration, new() | |
{ | |
protected readonly IDictionary<string, Action<T>> Do = new Dictionary<string, Action<T>>(); | |
protected readonly T Config = new T(); | |
public void Initialize(Action<T> action) | |
{ | |
action(Config); | |
SetMeUp(); | |
} | |
protected abstract void SetMeUp(); | |
public void Run() | |
{ | |
foreach (var d in Do) | |
{ | |
Console.WriteLine("Running [{0}]", d.Key); | |
d.Value(Config); | |
} | |
} | |
} | |
public class MySampleConfiguration : INakeTaskConfiguration | |
{ | |
public string Prop1 { get; set; } | |
public string Prop2 { get; set; } | |
public int Prop3 { get; set; } | |
} | |
//Creating a new Task | |
public class MySampleNakeTask : NakeTask | |
{ | |
public override void Initialize() | |
{ | |
base.Initialize(); | |
Console.WriteLine("Do some custom stuff before this task runs"); | |
} | |
protected override void SetMeUp() | |
{ | |
Do["this"] = () => Console.WriteLine("Do stuff"); | |
} | |
} | |
//Creating a new Task (with configuration) | |
public class MySampleNakeTaskWithConfiguration : NakeTask<MySampleConfiguration> | |
{ | |
protected override void SetMeUp() | |
{ | |
Do["this"] = c => | |
{ | |
Console.WriteLine("Do stuff with Prop1: {0}", c.Prop1); | |
Console.WriteLine("Do stuff with Prop2: {0}", c.Prop2); | |
Console.WriteLine("Do stuff with Prop3: {0}", c.Prop3); | |
}; | |
} | |
} |
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
var withConfig = new MySampleNakeTaskWithConfiguration(); | |
withConfig.Initialize(c => | |
{ | |
c.Prop1 = "1"; | |
c.Prop2 = "2"; | |
c.Prop3 = 3; | |
}); | |
withConfig.Run(); | |
/* | |
PRINTS THIS: | |
Running [this] | |
Do stuff with Prop1: 1 | |
Do stuff with Prop2: 2 | |
Do stuff with Prop3: 3 | |
*/ | |
var task = new MySampleNakeTask(); | |
task.Initialize(); | |
task.Run(); | |
/* | |
PRINTS THIS: | |
Do some custom stuff before this task runs | |
Running [this] | |
Do stuff | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment