Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the classes that you present in your example code are accurate (i.e. there is not a bunch more methods and properties). Then the simplest possible solution might work, and you can get rid of a number of classes / dependencies on classes.</p> <p>A simple solution, that does not rely on a framework/container would be:</p> <pre><code>public static class FormsProcessing { private static ConcurrentDictionary&lt;string, Func&lt;FormProcessor&gt;&gt; _registeredProcessors = new ConcurrentDictionary&lt;string, Func&lt;FormProcessor&gt;&gt;(); public delegate bool FormProcessor(XmlDocument form); public static void RegisterProcessor(string formKey, Func&lt;FormProcessor&gt; formsProcessorFactory) { _registeredProcessors.AddOrUpdate(formKey, formsProcessorFactory, (k, current) =&gt; formsProcessorFactory); } public static FormProcessor GetProcessorFor(string formKey) { Func&lt;FormProcessor&gt; processorFactory; if (_registeredProcessors.TryGetValue(formKey, out processorFactory); return processorFactory(); return null; } public static bool Process(string formKey, XmlDocument form) { var processor = GetProcessorFor(formKey); if (null == processor) throw new Exception(string.Format("No processor for '{0}' forms available", formKey)); return processor(form); } } </code></pre> <p>Usage: </p> <pre><code>namespace Web.Services { public class MyServiceClass { public bool AddForm(XmlDocument form, string formName) { return FormsProcessing.Process(formName, form); } } } </code></pre> <p>It is simple and explicit, and does not need or expose any dependency on some structure of <code>IPFormDataContext</code> and <code>IPFormDataStrategy</code> classes. The only explicit dependency you have is on a delegate that has the <code>FormProcessor</code> signature.</p> <p>Similar to a container, you will need to perform the registrations somewhere:</p> <pre><code>FormsProcessing.RegisterProcessor("FormA", () =&gt; new FormAStrategy().DoWork); FormsProcessing.RegisterProcessor("FormB", () =&gt; new FormBStrategy().DoWork); </code></pre> <p>Alternatively it would be easy to add some form of (convention based) auto registration by scanning assemblies for the convention (e.g. an interface signature).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload