开发者

Making sure a method is run only if another method has completed successfully

I'm using a web service to update some of my users' info on a third party db.

This info usually needs to be updated on my db as well (I know that doesn't sound good, but nothing I can do about that..).

Recently we started having all kind of discrepancy problems between the web service's db and our local db, so now I'm looking for a way to make sure my local db is only updated if the web service call returned success.

A major problem doing that is that the web service may return success in many ways (e.g. bool, enum etc.), and of course each method has a different signature.

For example:

I have web service methods like:

public bool ChangeDetails(User user)

And:

public SuccessStatus RemoveUser(long userId)  

Which only if are completed successfully should trigger a call to:

public bool UpdateUserDetails(long userId string userName) 

And:

public bool RemoveUser(long userId)

(I just intended to demonstrate the divergence of the methods..)

What I want to do is a mechanism that will "tie" each of the WS's method to a db method, by using a single method(but maybe there's a better way of making sure the db's method will execute only if the WS's method completed successfully).

My best way of solving this is by writing a method, call it UpdateWSandDB which receives the WS method's delegate, it's success type (e.g. bool/enum), it's success value, the db method name, the parameters for each method.... As you can see tha开发者_C百科t is a very awkward way of doing that.

Is there a neater and more elegant way?

Thanks.


Create a helper class of your web service like this:

public class ChangeDetailsServiceHelper
{    
    /// <summary>
    /// ChangeDetails
    /// </summary>
    /// <param name="callback"></param>
    public void ChangeDetails(Action<User, Exception> callback)
    {
        var proxy = new ChangeDetailsServiceClient();

        try
        {
            proxy.ChangeDetailsCompleted += (sender, eventargs) =>
            {
                var userCallback = eventargs.UserState as Action<User, Exception>;
                if (userCallback == null)
                    return;

                if (eventargs.Error != null)
                {
                    userCallback(null, eventargs.Error);
                    return;
                }
                userCallback(eventargs.Result, null);
            };
            proxy.ChangeDetailsAsync(callback);
        }
        catch (Exception ex)
        {
            proxy.Abort();
        }
        finally
        {
            if (proxy.State != CommunicationState.Closed)
            {
                proxy.CloseAsync();
            }
        }
    }
}

Then in your code you call your web service:

var changeDetails= new ChangeDetailsServiceHelper();
changeDetails.ChangeDetails((result, error) =>
{
    if (result != null && error == null)
    {
        //everything ok, call next webservice here
        //changeDetails.NEXT WEB SERVICE METHOD HERE...
    }
    else 
    {
        //error occured!
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜