Skip to content

Instantly share code, notes, and snippets.

@luisrudge
Last active December 18, 2015 12:59
Show Gist options
  • Save luisrudge/5786786 to your computer and use it in GitHub Desktop.
Save luisrudge/5786786 to your computer and use it in GitHub Desktop.
scriptcs idea
//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);
};
}
}
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