Should I stop breaking .NET event rules?

Again and again, I find myself implementing events in C # in accordance with the recommendations, and then return to a simpler implementation, using only Action, when the client code does not need any advantages added in the manual.

Perhaps I will add that to exclude the sender object , if it is not really required, is a good thing , because it helps to decouple triggers and handlers. All in all, I think it's a good habit to avoid writing code that isn't needed.

I do not mean that the guidelines should always be fired, there are advantages to following them (see links below). My question is whether I should always follow him.

The following example shows how concise the “naive event patterns” are compared to the rule pattern.

// This is neat class NaiveEvents { public event Action<string> OnAlert; public void TriggerOnAlert(string message) { OnAlert(message); } } // Is this bloated? class ProperEvents { public event EventHandler<OnAlertEventArgs> OnAlertEvent; public void TriggerOnAlert(string message) { OnAlertEvent(this, new OnAlertEventArgs(message)); } public class OnAlertEventArgs : EventArgs { private readonly string _message; public OnAlertEventArgs(string message) { _message = message; } public string Message { get { return _message; } } } } class EventsDemo { public static void DemoNaiveEvents() { var ev = new NaiveEvents(); ev.OnAlert += (msg) => { Console.WriteLine(msg); }; ev.TriggerOnAlert("Hello World"); } public static void DemoProperEvents() { var ev = new ProperEvents(); ev.OnAlertEvent += (sender, args) => Console.WriteLine(args.Message); ev.TriggerOnAlert("Hello World"); } } 

See the manual: http://msdn.microsoft.com/en-us/library/w369ty8x.aspx

Benefits: What are the benefits of having Net compliant events?

Thanks!

+6
source share
3 answers

Recommendations or conventions are arbitrary, and not all of them can correspond to all situations, environments, decisions, or points of view, but the reason for creating these documents is standardization .

Sometimes some recommendations seem to be useless, but my opinion that my opinion will never follow the recommendations and / or convention will be worse than not doing it.

Maintaining health, reusing code, predictability, readability and lack of discussion every day, because each developer has his own opinion about everything in detail, it is more valuable to me than anything.

By focusing on your current issue and based on what I said above, even if you have no performance or design quality advantages, I would suggest and recommend that global guidelines and conventions be followed if some software designs are intended for professional purposes. And if this is for open-source audiencie, they must also be respected.

As an enthusiast and professional developer, I like to write, read and maintain code that follows de facto coding standards, because I know that everyone will understand my work - obviously, the documentation is a plus! -.

+4
source

My design in this (and similar situations) is usually different for application code and library.

For reusable libraries, I often follow the recommendations, as this gives the best version semantics. And changing the public library api is pretty annoying.

In the application code, I fell to compromise more. Refactoring such an event handler, which is used in only one solution, does not work so much. So using a simplified template is OK IMO.

+1
source

From my point of view, the biggest advantage of following a pattern is that you can have interchangeable event handlers. For example, if you defined

 public void OnClick(object sender, EventArgs e) 

which handles the click event somewhere, it would be trivial to bind it to the OnAlert event, if necessary, without having to wrap the signature. This, however, is trivial to do in C #> 3.0 using lambdas so that the gain can be small. The day before anonymous delegates, it was harder to do, so it makes sense to follow the recommendations

+1
source

Source: https://habr.com/ru/post/889611/


All Articles