Check if chain of responsibility template is right for you (metacode):
public class ChainOfResponsibility { ChainOfResponsibility _next; Type _t1; Type _t2; Action _action; public ChainOfResponsibility(Type t1, Type t2, Action action) { _t1 = t1; _t2 = t2; _action = action; } public void Execute(object o1, object o2) { if(CriteriaMatches(o1, o2)) { _action(); return; } if(_next != null) _next.Execute(o1,o2); } public ChainOfResponsibility SetNext(Type t1, Type t2, Action action) { _next = new ChainOfResponsibility(t1,t2,action); return _next; } private bool CriteriaMatches(object o1, object o2) { return (o1 as t1) and (o2 as t2); } }
And the use will be:
public GetReportLogic(object someReport1, object someReport2) { var chain = new ChainOfResponsibility(typeof(Report1), typeof(Report1), DoSomething) .SetNext(typeof(Report1), typeof(Report2), DoSomethingElse) .SetNext(typeof(Report1), typeof(Report3), DoSomethingElseAgain); chain.Execute(someReport1, someReport2); }
I wrote a version of the template for the code that you provided. Feel free to adapt it to your real needs.
source share