How to manage large amounts of delegates and usercallbacks in C# async http library
Posted
by
Tyler
on Stack Overflow
See other posts from Stack Overflow
or by Tyler
Published on 2011-01-02T10:08:35Z
Indexed on
2011/01/02
11:54 UTC
Read the original article
Hit count: 194
I'm coding a .NET library in C# for communicating with XBMC via its JSON RPC interface using HTTP.
I coded and released a preliminary version but everything is done synchronously. I then recoded the library to be asynchronous for my own purposes as I was/am building an XBMC remote for WP7.
I now want to release the new async library but want to make sure it's nice and tidy before I do.
Due to the async nature a user initiates a request, supplies a callback method that matches my delegate and then handles the response once it's been received.
The problem I have is that within the library I track a RequestState object for the lifetime of the request, it contains the http request/response as well as the user callback etc. as member variables, this would be fine if only one type of object was coming back but depending on what the user calls they may be returned a list of songs or a list of movies etc.
My implementation at the moment uses a single delegate ResponseDataRecieved which has a single parameter which is a simple Object - As this has only be used by me I know which methods return what and when I handle the response I cast said object to the type I know it really is - List, List etc.
A third party shouldn't have to do this though - The delegate signature should contain the correct type of object. So then I need a delegate for every type of response data that can be returned to the third party - The specific problem is, how do I handle this gracefully internally - Do I have a bunch of different RequestState objects that each have a different member variable for the different delegates? That doesn't "feel" right. I just don't know how to do this gracefully and cleanly.
© Stack Overflow or respective owner