Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by vexe · Nov 06, 2013 at 09:55 PM · c#eventdelegategenericsmessage

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Nov 08, 2013 at 08:08 AM 0
Share

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 co$$anonymous$$g out of a $$anonymous$$igun - 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.

avatar image vexe · Nov 08, 2013 at 08:19 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# event setup, no result 0 Answers

Sharing delegate types across scripts 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Event to change level 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges