4
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$
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));

I think you should learn how to use async-await properly. Instead of using async void (which is a bad practice in general) and taking a delegate, your method should return something like Task<List<Track>>.

This way, your method would look like this:

public Task<List<Track>> GetTracksAsync()
{
    return ApiCaller.ApiAction<List<Track>>(ApiCall.UserTracks, Id);
}

You wouldn't need all the boilerplate around it and you would also handle exceptions properly.

To use GetTracksAsync(), you would then use await:

List<Track> tracks = await GetTracksAsync();
\$\endgroup\$
  • \$\begingroup\$ Ah yes, I didn't fully grasp the working of async. I didn't know that I could do a return as in the example you gave, kept adding the async - await there too but can change that now. And your link learnt me something new, thank you! \$\endgroup\$ – Alex_na Dec 19 '14 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.