x


Parametrize generic event system (DRY it)

Hey guys, been checking out some messaging systems lately. I came across the CSharpMessengerExtended - Kinda nice, but not very DRY - very repetitive, lot's of copy-pasting.

See my question here, as nobody seems to have answered it.

(Nice sad humor in the comments as well =))

Thanks for any help.

more ▼

asked Nov 06, 2013 at 09:55 PM

vexe gravatar image

vexe
19.4k 90 463 218

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I don't have any quick idea how to solve your problem the way you'd like it, but maybe an option is to always send data using a class derived from some base class? Just like in standard .NET event system, where EventArgs (or derived class) is used.

If you want to use a message with no parameters, then you just use EventArgs.Empty (or eventually pass null). If you want any parameters, then create a class derived from EventArgs with a properties of your desired types.

This method requires creating new classes, but in my opinion this is much better than having unnamed generic parameters. With such generic parameters, browsing through code after a few weeks will be more painful, as you'll scratch your head trying to find out what the hell this int, long and string actually mean. Dedicated class with properties having descriptive names is a much better option. That's why I never use classes from Tuple family (not supported in Unity yet).

more ▼

answered Nov 07, 2013 at 11:35 PM

ArkaneX gravatar image

ArkaneX
11.8k 6 11 46

Thanks for your answer. This is what I actually ended up doing. As I found [this system here][1] - just as what you describe.

One thing I don't quite like about it, is the fact that you have to new up stuff, each time an event fires. Imagine someone subscribing to a OnBulletFire where the bullets are coming out of a minigun - at a very high rate.... newnewnewnewnewn.....

But it is a lot better, than generics. Because:

  1. it's a lot more object-oriented
  2. it follows the rules (the O in SOLID - the open for extension, and close for modification principle). So if you want to add a new event you just create one, extending what you have, while if you wanted to add another generic arg, you'd have to modify the source.
  3. The main problem with generic args, is that you never know the users needs, what if they wanted 5 args, 10 args? you never know - which leads you to considering the worst case, and making a silly amount of overloads, like 16 (just like the Action and Func) - then you'd fall into YAGNI (ya ain't gonna need it) - and DRY (don't repeat yourself) by copying and pasting... so not very clean.

    [1]: http://www.willrmiller.com/?p=87

Nov 08, 2013 at 08:08 AM vexe

Speaking of solving the original problem, I don't think there is a way - if there was, there wouldn't be 16 overloads to Func and Action.

Nov 08, 2013 at 08:19 AM vexe
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9793
x302
x108
x81
x75

asked: Nov 06, 2013 at 09:55 PM

Seen: 416 times

Last Updated: Nov 08, 2013 at 08:19 AM