Silverlight 异步API回调设计问题

Silverlight 异步API回调设计问题,silverlight,asynchronous,callback,api-design,Silverlight,Asynchronous,Callback,Api Design,我将Silverlight与RIA服务一起使用,这些服务本质上是异步的。不过,这个问题并不特定于这种技术选择 我想包装一个异步调用,例如一个web服务,并提供我自己的简化且松散耦合的API(例如:DomainClient上的repository) 到目前为止,我的接口中有以下类型的异步方法: public void DoAsyncWork(Action<AsyncWorkResult<someResultType>> callback); public-void-DoA

我将Silverlight与RIA服务一起使用,这些服务本质上是异步的。不过,这个问题并不特定于这种技术选择

我想包装一个异步调用,例如一个web服务,并提供我自己的简化且松散耦合的API(例如:DomainClient上的repository)

到目前为止,我的接口中有以下类型的异步方法:

public void DoAsyncWork(Action<AsyncWorkResult<someResultType>> callback);
public-void-DoAsyncWork(操作回调);
我正在考虑添加以下重载:

public void DoAsyncWork(Action<someResultType> onSuccess, 
                        Action<Exception> onException);
public void doacyncwork(成功时的操作),
行动一例外);

public void doacyncwork(成功时的操作),
行动一例外,
行动(最后);
只有异步调用成功完成时,
onSuccess
操作才会执行,如果调用报告了错误,
onException
将在调用结束时执行,
最终
将在任何一种情况下执行

我的问题是,在实现了第一种风格,即“一般风格”和最常用的风格(据我所知)之后,我是否应该实现另外两种风格?它们值得在开发和维护方面投资吗

这个问题是关于设计方面的,与任何需求无关


提前感谢:)

我想说,如果可能的话,请使用一个真正好的、一致的API

最好模仿.NET中已经存在的异步API,它允许您使用内置的异常处理语法:

public IAsyncResult BeginAsyncWork(AsyncCallback callback, object state);
public SomeResultType EndAsyncWork(IAsyncResult res);
它的用法如下:

BeginAsyncWork(res =>
{
    // BeginAsyncWork calls this once it completes, even on error.

    // res is IAsyncResult -- the same one BeginAsyncWork returns.
    // res.AsyncState is whatever the users passed in the 'state' parameter.

    try
    {
        // To get the result, and possibly an exception, EndAsyncWork is called.
        SomeResultType r = EndAsyncWork(res);
    }
    catch(Exception ex)
    {
        // EndAsyncWork throws the exception.
    }
    finally
    {
        //
    }
}, null);

@Cory:我不是故意在这里添加“Silverlight”标记的,因为正如我所说的,我只关注API设计方面。无论如何,谢谢:)根据您所使用的语言/框架,最好的API设计看起来可能会大不相同。我会说这是重要的信息,但也许不是。@Cory:好吧,很公平:)但你的答案是什么?我没有太多地使用你提到的模式,我将进一步了解它。然后我发现你的答案不见了:当我意识到它没有完全回答你的问题时,我把它删除了。我想我会取消删除:)
BeginAsyncWork(res =>
{
    // BeginAsyncWork calls this once it completes, even on error.

    // res is IAsyncResult -- the same one BeginAsyncWork returns.
    // res.AsyncState is whatever the users passed in the 'state' parameter.

    try
    {
        // To get the result, and possibly an exception, EndAsyncWork is called.
        SomeResultType r = EndAsyncWork(res);
    }
    catch(Exception ex)
    {
        // EndAsyncWork throws the exception.
    }
    finally
    {
        //
    }
}, null);