接口和异步方法 [英] Interfaces and async methods

查看:109
本文介绍了接口和异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序。该应用程序使用的一个接口来访问数据库。此接口可以由许多类来实现。例如,一种使用EF 4.4,但其它类可以使用EF5是更有效的。在未来也许是因为它使用异步方法,我将使用EF6。在这个例子中所有的方法都使用EF,但也许其他选项可以使用其他的方式。

I have an application. This application uses an interface to access the database. This interface can be implemented by many classes. For example, one uses EF 4.4, but other classes can use EF5 that is more efficient. In the future perhaps I will use EF6 because it uses async methods. In this example all the methods use EF, but perhaps other options can be use other ways.

该应用程序是codeD一次,期运用界面,并根据配置文件中,使用一个实现或其他的,所以我只需要修改code在一个地方,构造,加在被分配给该接口的类的实例化的新的选择。

The application is coded once, useing the interface, and according to the config file, use one implementation or the other, so I only need to modify the code in one place, the constructor, to add the new option in the instantiation of the class that is assigned to the interface.

目前的类的所有方法都没有异步,但在未来,如果我用EF6我想用异步方法,所以我不知道这是否是可能的使用EF6和实现接口可以使用异步方法的类。

At the moment all the methods of the classes are not async, but in the future if I use EF6 I would like to use the async methods, so I don't know if it is possible that the class that use EF6 and implements the interface can use the async methods.

有关EF6的异步方法,我会用异步/ awiat模式,所以在我的类的方法,我需要使用异步属性。这让我使用等待关键字时,我打电话给EF6的异步方法。

For the async methods of EF6, I would use the async/awiat pattern, so in the method of my class I need to use the async attribute. This lets me use the await keyword when I call to the async method of EF6.

但这个类可以实现该接口,在第一时间同步方法呢?

But this class can implement the interface that in a first time is for synchronous methods?

是否有某种方式,在主应用程序,我可以使用许多实现,而不需要修改code吗?一些实现将使用异步方法,而有些则是同步的。

Is there some way that in the main application I can use many implementations without the need to modify the code? Some implementations will use async methods while others will be synchronous.

推荐答案

异步不是签名的一部分,所以你实际上并不需要关注以实现接口的方法是否异步或没有,你只需要同类型的属性,返回类型,方法的名称有关,而可访问性。

async isn't a part of the signature, so you don't actually need to be concerned with whether the method implementing the interface is async or not, you only need to be concerned with the types of the properties, the return type, the name of the method, and the accessibility.

真正的区别是,你的异步方法需要返回一个工作任务< T> ,而非异步方法最有可能目前正在返回无效或某种类型, T 直接

The real difference is that your async methods will need to return a Task or a Task<T>, whereas the non-async methods are most likely currently returning void or some type, T directly.

如果你想面向未来您的应用程序一个选择是确保所有的接口都返回工作任务&LT; T&GT; 并为您EF4 / EF5实现您在包装即使它们同步执行已完成任务的结果。

If you want to "future proof" your application one option is to ensure that all of your interfaces return Task or Task<T> and that for your EF4/EF5 implementations you wrap your results in a completed task even though they're executed synchronously.

Task.FromResult 添加在.NET 4.5,但如果你没有它,你可以写你自己足够容易:

Task.FromResult was added in .NET 4.5, but if you don't have it you can write your own easily enough:

public static Task<T> FromResult<T>(T result)
{
    var tcs = new TaskCompletionSource<T>();
    tcs.SetResult(result);
    return tcs.Task;
}

您也可以编写一个简单的返回一个已经完成了任务 CompletedTask 方法:(它缓存出于效率的考虑单个任务)

You can also write a CompletedTask method that simply returns a task that has already completed: (It caches a single task for efficiency reasons.)

private static Task _completedTask;
public static Task CompletedTask()
{
    return _completedTask ?? initCompletedTask();
}

private static Task initCompletedTask()
{
    var tcs = new TaskCompletionSource<object>();
    tcs.SetResult(null);
    _completedTask = tcs.Task;
    return _completedTask;
}

这两种方法将简化其所有的方法返回某种类型的任务,虽然这样做会让你的codeA有点混乱,直到你'的过程重新能够使用C#5.0才能够等待的结果。

Those two methods will simplify the process of having all of your methods returning some type of Task, although doing this will make your code a bit messier until you're able to use C# 5.0 to be able to await the result.

这篇关于接口和异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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