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.
Answer by ArkaneX
Nov 07, 2013 at 11:35 PM
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).
Thanks for your answer. This is what I actually ended up doing. As I found this system here - 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:
it's a lot more object-oriented
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.
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.
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
18 People are following this question.
Event to change level
Login form: Do I have to set custom delegates to null in OnDestroy?
Score system using events to pass data?
Delegate an event
custom event delegate - convertible error