开发者

Generic callback mechanism overloads success handler as a means for flow control - code smell?

I'm fairly new on a project and ran across an interesting design paradigm for some asynchronous calls we make to the database (variables and function name altered):

private void OnLogin(object selectedInitialState,
                     AsyncEventCompletedCallback<EmptyAsyncEventArgs> userCallback,
                     object userState)

Example usages:

OnLogin(
    null,
    args =>
    {
        if (args.IsSuccess)
            DetermineNextStep(); //When done, continue to this step
        else
            //NOTE: This probably means we couldn't connect to the DB
            // Handle this case
    },
    null);

OnLogin(
    newInitialState,
    args =>
    {
         ReLoginUser(); //Was logged in; re-loggin开发者_如何学Pythong in user with different initial state
    },
    null);

The example usages show two different calls to this function for two different cases - an initial login and a re-login (not technically a re-login, but a restart of the application for the currently logged in user with a different initial state).

What bothers me is that the callback function in these two cases is different. I'm used to seeing a function take a callback to allow users of the function to provide custom implementations within the purview of the function being called.

In the above case, though, the callback function changes the control flow. Depending on which callback function is provided, the subsequent calling functions after the async call returns are different. Is this a code smell or just an inventive use for callbacks?


I'm interpreting the OnLogin function as starting an operation that will invoke the callback exactly once upon completion.

In this case, the code is actually quite normal. This is not uncommon when writing a program asynchronously. Considering that the alternative is keeping a "state" with a (small) state machine in the callback function, I think different callbacks is actually the more elegant solution. It handles the asynchronous "state" implicitly instead of explicitly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜