如何强制异步孩子将覆盖在C#5.0 [英] How to force async child overrides in C# 5.0

查看:98
本文介绍了如何强制异步孩子将覆盖在C#5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,其中多个客户端对象,预计通过接口来实现特定功能的系统上,我想这个函数与延续异步运行(我期待的实现是I / O密集​​型并希望确保所有的客户端一旦对象完成此功能越好)。我使用Visual Studio异步CTP刷新了SP1,用C#5.0。

什么是强制执行我的抽象类的子对象异步行为(见下文)建议的做法?我不能(显然)强制使用使用虚拟方式方法异步的方法。我只需要一个任务返回类型。这是否意味着我不应该要求异步行为在所有子对象?在这种情况下,应该返回类型是简单地无效?

public接口是系统设计的一个不幸后果,现在,但是这是一个单独的问题。很显然,我不能约束任何人都可以是异步谁绕过BaseFoo',只是实现了'的IFoo接口。

下面是code:

 公共接口IFoo的
{
    酒吧无效(); //注意:不能使用异步的方法,而没有尸体。
}公共抽象类BaseFoo:IFoo的
{
    公共异步空栏()
    {
        等待的OnBar(); //问:什么是正确的异步代表团的格局?
    }    受保护的虚拟异步任务的OnBar()
    {
        等待TaskEx.Yield();
    }
}公共类RealFoo:// BaseFoo注意:可能由第三方来实施
{
    保护覆盖异步任务的OnBar()
    {
        //客户:做的工作,可能等待异步调用        等待TaskEx.Yield(); //次生问题:是否有办法避免这一点,如果没有等待着在客户端的工作?
    }
}


解决方案

无论方法是使用实​​施异步 / 等待与否是一个的实施的细节。如何使用该方法应该的的行为的是一个合同的细节,应该以正常的方式来指定。

请注意,如果你使该方法返回一个工作任务< T> ,它更明显,它的意思是异步的,可能会是难以实现的没有的是异步的,

在另一方面,如果有一个实现(例如,用于测试目的),其中的await 前pressions会的从不的是不完整的,你为什么会想强迫别人来写,没有等待反正调用异步方法?你的期望的实施是IO的限制,但说不定会有特殊情况下,实现要使用硬codeD数据等。

基本上,你已经得到了该方法的文档中来处理这一点 - 如果你不能信任实施者读取,你有没有机会呢:(

I'm working on a system in which multiple client objects are expected to implement a particular function via an interface, and I want that function to run asynchronously with continuations (I'm expecting the implementations to be I/O-bound and want to ensure that all the client objects complete this function as soon as possible). I'm using the Visual Studio Async CTP Refresh for SP1, with C# "5.0".

What is the recommended practice for enforcing asynchronous behavior in child objects of my abstract class (see below)? I can't (apparently) enforce use of 'async' methods using the virtual method approach. I can only require a 'Task' return type. Does this mean I should not try to require asynchronous behavior at all in child objects? In that case, should the return type be simply 'void'?

The public interface is an unfortunate consequence of the system design right now, but that's a separate issue. Obviously, I couldn't constrain anyone to be asynchronous who bypasses 'BaseFoo' and just implements the 'IFoo' interface.

Here is the code:

public interface IFoo
{
    void Bar(); //NOTE: Cannot use 'async' on methods without bodies.
}

public abstract class BaseFoo : IFoo
{
    public async void Bar()
    {
        await OnBar(); //QUESTION: What is the right "async delegation" pattern?
    }

    protected virtual async Task OnBar()
    {
        await TaskEx.Yield();
    }
}

public class RealFoo : BaseFoo //NOTE: May be implemented by 3rd party
{
    protected override async Task OnBar()
    {
        //CLIENT: Do work, potentially awaiting async calls

        await TaskEx.Yield(); //SECONDARY QUESTION: Is there a way to avoid this if there are no 'awaits' in the client's work?
    }
}

解决方案

Whether a method is implemented using async/await or not is an implementation detail. How the method should behave is a contract detail, which should be specified in the normal way.

Note that if you make the method return a Task or a Task<T>, it's more obvious that it's meant to be asynchronous, and will probably be hard to implement without being asynchronous.

On the other hand, if there's an implementation (e.g. for test purposes) where the await expressions would never be incomplete, why would you want to force someone to write an async method with no await calls in anyway? You're expecting implementations to be IO-bound, but maybe there will be special cases where implementations want to use hard-coded data etc.

Basically you've got to handle this in the documentation for the method - if you can't trust implementers to read that, you've got no chance anyway :(

这篇关于如何强制异步孩子将覆盖在C#5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆