Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A cleaner way to automatically call one method after another?

Tags:

c#

winforms

Is it possible to design a method in such a fashion, that it knows it must automatically call a next method in succession upon exiting?

In the following example, I must call Refresh() to cause my form to repaint after this event takes place. The problem is that, it's ugly to call Refresh() after, for example, 20 different events which must make the form refresh. e.g

private void PriorityLine_Click(object sender, EventArgs e)
{
   _showPriorityLine = (_showPriorityLine) ? false : true;
  Refresh(); // Must call refresh for changes to take effect.
}

I suppose what I'm looking for is some kind of signature I can apply to the method to cause it to automatically chain to the next method, regardless from where its called. e.g

(I know this isn't syntactically correct.)

private void PriorityLine_Click(object sender, EventArgs e).Refresh()
{
   _showPriorityLine = (_showPriorityLine) ? false : true;
}

I want to seperate the interface of the method, from the logic contained within the method. I understand it would be the exact amount of effort, if not more. For example, if I were to edit the method and accidently removed Refresh, it would cause my application to break. Whereas, if the Refresh method was outside of the actual logic of the method, I could do anything within the method without worrying about removing the next chain of logic.

like image 363
George Johnston Avatar asked Jan 28 '11 15:01

George Johnston


2 Answers

Sounds like what you want is Aspect Oriented Programming, there are a number of different frameworks to enable you to have stuff "magically" happen after some set of methods have run, have a look here AOP programming in .Net?

like image 193
AndreasKnudsen Avatar answered Oct 14 '22 13:10

AndreasKnudsen


I'm not aware of any really clean way. One method would be to use PostSharp.

like image 38
troutinator Avatar answered Oct 14 '22 15:10

troutinator