Workflow Foundation parameters done better!?

I think a lot of people don't like the way Workflow Foundation parameters work. At least I totally dislike it. I don't want to use an almost untyped (yes, we have object as type) dictionary to read values, and besides why do we need to cast, the types are available. Instead of using basic strings for the parameter naming, I have a different approach. I add an static inner class to the Workflow codebeside.

1 public static class Parameters 2 { 3 public const string BaseFolder = "BaseFolder"; 4 public const string AnalysisResult = "AnalysisResult"; 5 }

When inside the Parameters class I also have a ReSharper Live Template, stpar:

public const string $ParamName$ = "$ParamName$";

In this example I can use the workflow parameter names by using :

WorkflowName.Parameters.BaseFolder

You now have to add a real C# property with the same name as we have in the Parameters class. But much less work, and through this approach we won't make that many mistakes in the string-based key.

Besides this the use of a basic Dictionary isn't that readable too. So I created a somewhat fluent interface for use with Dictionary<TKey,TValue>. So we can create a dictonary more readable than the basic way.

1 FluentDictionary<string, object> parameters = new FluentDictionary<string, object>() 2 .Add(AdministrationAnalysis.Parameters.BaseFolder, @"C:\") 3 .Add(AdministrationAnalysis.Parameters.AnalysisResult, null);

So here''s the code for the FluentDictionary:

1 public class FluentDictionary<TKey, TValue> 2 { 3 private readonly Dictionary<TKey, TValue> internalDictionary = new Dictionary<TKey, TValue>(); 4 5 public FluentDictionary() 6 { 7 } 8 9 public FluentDictionary(Dictionary<TKey, TValue> internalDictionary) 10 { 11 this.internalDictionary = internalDictionary; 12 } 13 14 public Dictionary<TKey, TValue> Dictionary 15 { 16 get { return internalDictionary; } 17 } 18 19 public int Count 20 { 21 get { return internalDictionary.Count; } 22 } 23 24 public FluentDictionary<TKey, TValue> Add(TKey key, TValue value) 25 { 26 internalDictionary.Add(key, value); 27 return this; 28 } 29 30 public void Clear() 31 { 32 internalDictionary.Clear(); 33 } 34 35 public bool ContainsKey(TKey key) 36 { 37 return internalDictionary.ContainsKey(key); 38 } 39 40 public bool ContainsValue(TValue value) 41 { 42 return internalDictionary.ContainsValue(value); 43 } 44 45 public TTyped Get<TTyped>(TKey key) where TTyped : TValue 46 { 47 return (TTyped) internalDictionary[key]; 48 } 49 }

Let me know if you like this approach or if you have a better solution.

Gravatar