c# - Designing set of rules that use dependencies -
c# - Designing set of rules that use dependencies -
i have service reads emails received in specific mailbox. issue based on email contains have 1 or many actions. have mess below. i've shortened quite bit. actual application has far more corner cases.
var email = new email { body = "some random email body text..." }; if(email.body.startswith("a")) { // requires dependency on inotifier console.writeline("notifying administrator"); } if (email.body.startswith("b")) { // requires dependency on iparser , irepository console.writeline("parsing email , adding database"); } if (email.body.endswith("c")) { // requires dependency on isender , inotifier console.writeline("forwarding email , notifying administrator"); } if (email.body.endswith("c")) { // requires dependency on inotifier console.writeline("ignoring email"); }
essentially, if criteria met associating action must executed using 1 or more dependencies. these dependencies i'd inject in constructor.
i've thought of creating this:
public class administratornotififercriteria : icriteria { private readonly inotifier _notifier; public administratornotififercriteria(inotifier notifier) { _notifier = notifier; } private void execute() { _notifier.notify(); } public void checksatisfaction(email email) { if(email.body.startswith("a")) execute(); } }
the bottom line wish create composable commands. when add together criteria downwards line have inherit icriteria
(or whatever) , allow application figure out.
is there name of this?
i have consumer resembles this.
public class emailconsumer { private readonly ienumerable<icriteria> _criterias; // criterias found , injected windsor classes inheriting icriteria interface public emailconsumer(ilist<icriteria> criterias) { _criterias = criterias; } public void consume(ilist<email> emails) { foreach(var criteria in _criterias) { foreach(var email in emails) { criteria.checksatisfaction(email); } } } }
edit thanks replies, iabstract , djna. understand strategy , cor pattern , thinking more appropriate proving don't understand plenty of current problem.
the way understand cor greedy , whoever can take responsibility execute , proceed next object consumed. on other hand, there doesn't seem stopping looping between strategies says "hey! can consume request don't worry it".
this seems variation of chain of responsibility perchance logic dealing non-exclusive cases - intend should happen if body starts "a" , ends "c"?
one thought in chain of resposibility there's no need huge dispatching if/else chain. instead offer email icriteria implementatio, , have responsibility either process or pass on next candidate.
c# oop design
Comments
Post a Comment