I'm making a WindowsPhone 8 app in C#. With this an API so I can convert JSON data into useful objects. I've found a method but am wondering if it is any good at all. The problem is it's been a while since I've really worked with C# and never for windows phone.
Because it's making use of web requests it has to be asynchronous. From somewhere User.getTracks() should be called and handled. To do this I've come up with a solution but I don't know if it's a good one.
I have an ApiCaller class which has a few different ApiAction methods, these methods receive a generic to know what type to eventually serialize to. The main method receives all the data, makes the web request and serializes the JSON to an object.
public static async void ApiAction<T>(Uri uri, CallExecuted cbDelegate, HttpMethod method, bool requireAuthentication)
Here it receives a CallExecuted
this is the delegate from the calling class. After serializing the JSON data it calls this delegate with the item as parameter cbDelegate(new CallbackArgs(item));
The calling class will have the following to process everything
public delegate void GetTracksExecuted(List<Track> tracks); // The delegate other classes use
public static event GetTracksExecuted tracksExecuted; // The local event that calls the above delegate
// This is called from elsewhere
// The parameter is the delegate from that class
public void GetTracks(GetTracksExecuted e)
{
tracksExecuted += e;
ApiCaller.ApiAction<List<Track>>(ApiCall.UserTracks, TracksDelegate, Id);
}
// The delegate given to ApiCall class
public delegate void CallExecuted(CallbackArgs e);
// The delegate that is called from the ApiCall class (CallExecuted)
private static void TracksDelegate(CallbackArgs args)
{
List<Track> tracks = (List<Track>) args.Item;
tracksExecuted(tracks);
}
Is there a better way to accomplish any of this? Was thinking about just using the event to do it, leaving the delegate out, but don't know if that's better. Your input is very welcome