I feel like there is a cleaner/simpler way of doing the following. Is there an existing software pattern that I should be using?
I have an Assembler
class that I use to assemble/process content rules MS-Word documents.
There are many different types of Controls that are processed:
ClearCellIf
DeleteColumnIf
DeleteLineIf
DeleteRowIf
RepeatingControl
The controls fall into two categories:
- Generic controls (used over and over)
- Custom controls (only used in a specific solution)
Each control extends a base PrecedentControl
class and has a Test
and a Process
method:
class DeleteRowIf : PrecedentControl
{
public DeleteRowIf(Wd.ContentControl control) : base(control) { }
public static bool Test(Wd.ContentControl control, PrecedentInstruction Instruction)
{
return Instruction != null && Instruction.Command == "DeleteRowIf";
}
public override void Process<T>(T data, ref bool Cancel)
{
var result = PrecedentExpression1.Resolve(control.GetPrecedentInstruction().Expression, data);
if (result)
control.DeleteRow();
}
}
The bit that I hate, and would like to do better, is the logic that determines which type of control is in use:
public static PrecedentControl AsPrecedentControl(this Wd.ContentControl control)
{
var instruction = control.GetPrecedentInstruction();
if (FormatVariable.Test(control, instruction))
return new FormatVariable(control);
else if (Optional.Test(control, instruction))
return new Optional(control);
else if (DeleteControlIf.Test(control, instruction))
return new DeleteControlIf(control);
else if (DeleteControlIf_AndPreviousSpace.Test(control, instruction))
return new DeleteControlIf_AndPreviousSpace(control);
else if (DeleteControlIf_AndSurroundingPunctuation.Test(control, instruction))
return new DeleteControlIf_AndSurroundingPunctuation(control);
else if (DeleteLineIf.Test(control, instruction))
return new DeleteLineIf(control);
else if (DeleteLineIf_OrRemoveControl.Test(control, instruction))
return new DeleteLineIf_OrRemoveControl(control);
else if (DeleteLineIf_AndRepositionBulletEnding.Test(control, instruction))
return new DeleteLineIf_AndRepositionBulletEnding(control);
else if (DeleteRowIf.Test(control, instruction))
return new DeleteRowIf(control);
else if (DeleteRowIf_OrRemoveControl.Test(control, instruction))
return new DeleteRowIf_OrRemoveControl(control);
else if (DeleteColumnIf.Test(control, instruction))
return new DeleteColumnIf(control);
else if (ClearCellIf.Test(control, instruction))
return new ClearCellIf(control);
else if (RepeatingControl.Test(control, instruction))
return new RepeatingControl(control);
else if (RepeatingControl_RemoveLastAnd.Test(control, instruction))
return new RepeatingControl_RemoveLastAnd(control);
else if (RepeatingControl_SecondLastHasAnd.Test(control, instruction))
return new RepeatingControl_SecondLastHasAnd(control);
else if (RunMacro.Test(control, instruction))
return new RunMacro(control);
else if (ContactDelivery.Test(control, instruction))
return new ContactDelivery(control);
else if (ContactDelivery_DeleteControlAndPrecedingComma.Test(control, instruction))
return new ContactDelivery_DeleteControlAndPrecedingComma(control);
else if (ContactDelivery_MultilineAddress.Test(control, instruction))
return new ContactDelivery_MultilineAddress(control);
else
return null;
}