Skip to content

Instantly share code, notes, and snippets.

@ursenzler
Created July 13, 2012 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ursenzler/3103750 to your computer and use it in GitHub Desktop.
Save ursenzler/3103750 to your computer and use it in GitHub Desktop.
Prototype of Appccelerate feature management
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FreakyFeatureManagement
{
using Appccelerate.EvaluationEngine;
class Program
{
// Button enable/disable
// Plugin load
// application start allowed
static void Main(string[] args)
{
var s = new Switchable();
var engine = new EvaluationEngine();
engine.Solve<IsFeaturePresent, bool, string>()
.AggregateWithSingleExpressionAggregator()
.ByEvaluating((q, f) => (f == "A") || (f == "B"));
var features = new FeatureTokenBuilder(engine);
var token = features
.Enabled(() => { s.Switched = true; })
.Disabled(() => { s.Switched = false; })
.ForFeatures("A", "B");
//.OnEnabled(this, () => { })
//.OnDisable(this, () => { })
//.ForFeatures(descriptor);
token.Execute();
Console.WriteLine(s.Switched);
Console.ReadLine();
}
public class FeatureTokenBuilder
{
private readonly EvaluationEngine engine;
private Definition definition = new Definition();
public FeatureTokenBuilder(EvaluationEngine engine)
{
this.engine = engine;
}
public FeatureTokenBuilder Enabled(Action action)
{
definition.EnableActions.Add(action);
return this;
}
public FeatureTokenBuilder Disabled(Action action)
{
definition.DisableActions.Add(action);
return this;
}
public FeatureToken ForFeatures(params string[] features)
{
this.definition.Features.AddRange(features);
var featureToken = new FeatureToken(this.definition, this.engine);
this.definition = new Definition();
return featureToken;
}
}
public class Definition
{
public Definition()
{
this.EnableActions = new List<Action>();
this.DisableActions = new List<Action>();
this.Features = new List<string>();
}
public List<Action> EnableActions { get; private set; }
public List<Action> DisableActions { get; private set; }
public List<string> Features { get; private set; }
}
public class FeatureToken
{
readonly Definition definition;
readonly DslAbarbeiter doer;
public FeatureToken(Definition definition, EvaluationEngine engine)
{
this.definition = definition;
this.doer = new DslAbarbeiter(engine);
}
public void Execute()
{
bool shouldWeReallyDoIt = doer.Evaluate(definition.Features);
if (shouldWeReallyDoIt)
{
foreach (var enableAction in definition.EnableActions)
{
enableAction();
}
}
else
{
foreach (var disableAction in definition.DisableActions)
{
disableAction();
}
}
}
}
public class DslAbarbeiter
{
public DslAbarbeiter(EvaluationEngine e)
{
this.e = e;
}
EvaluationEngine e;
public bool Evaluate(IEnumerable<string> features)
{
return features.Select(f => e.Answer(new IsFeaturePresent(), f)).All(x => x);
}
}
public class IsFeaturePresent : Question<bool, string>
{
}
//public class Descriptor
//{
// private IFancyFeatureDSL GiveMeDaFeatures
// {
// get
// {
// return "A or B";
// }
// }
//}
public class Switchable
{
public bool Switched { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment