c# - When and Why to Use Custom Event Handlers -


i have object (actually visual element , viewmodel element) i'd use in multiple places throughout project. function of object collect payment information. customer smith ordered widget company, he's late payment. calls me he'll pay amount in full on 1st. fill in payment amount , date, click button, , entry added database mr. smith's proposed payment info.

now, said, want use object in multiple places, , depending on i'm using it, may want more happen after clicking submit button. along widgets, sell whatsits , whoozits. after receiving payment info whatsit, want populate additional table send customer reminder. after entering whoozit payment info, want populate reminder table, sending customer sms confirmation.

my original idea add list<action> field original object, , depending on i'm accessing from, send additional actions want done after original database update complete.

something this:

public class moneygetter {     public list<action> afterevents = new list<action>();      public void savepaymentinfo()     {         //logic initial database entry          foreach(var in afterevents)         {             a.invoke();         }     } } 

and after creating object, depending on i'm using it, i'd add

var mg = new moneygetter(); mg.afterevents.add(() => { /* add situation specific action logic here */ }); 

after getting through of this, occurred me events (i think), , should maybe creating custom onsubmitted event moneygetter object, subscribing event whatever object using it, , raising event after initial db entry.

is case? , if so, why going through trouble of creating custom event handler better idea having list<action>?

is case?

probably, yes. if want able notify subscribers when happens in class, raising event common , approach.

and if so, why going through trouble of creating custom event handler better idea having list<action>?

well, adding action<t>s public list<t> kind of convoluted way implement events. guess work. consumer of class, expcect able subscribe event using += syntax though instead of having add/remove actions publicly exposed list<t>.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -