mercredi 2 janvier 2019

Defining a Message Passing domain with very many message types

Most F# Message Passing examples I've seen so far are working with 2-4 message types, and are able to utilize pattern matching to direct each message to its proper handler function.

For my application, I need hundreds of unique message types due to the different nature of their handling and required parameters. So far, each message type is its own record type with a marker interface attached, because including hundreds of types in a single discriminated union would not be very pretty - and neither would the pattern matching of these be. As a result, I'm currently using reflection to find the correct handler functions of messages.

Is there a better, and more functional way of doing this? Perhaps even a smarter way to define such a domain? I'd like to enforce as much correctness as possible at compile time, but currently I'm finding the handler functions based on a custom attribute, as well as checking their signature at run time. As far as I know, I cannot enforce a function's signature with a .NET custom attribute, and since there are too many types to realistically pattern match, I can't (to my knowledge) use a single generic message handler function either.

I have thought about the possibility of attaching the handler functions to their respective record type as a member, which would circumvent the need for reflection and enforce some additional correctness at compile time. However, it doesn't make much sense to have all those functions present client side.





Aucun commentaire:

Enregistrer un commentaire