T>没有对IDbSet&LT FindAsync()方法; [英] No FindAsync() method on IDbSet<T>

查看:301
本文介绍了T>没有对IDbSet&LT FindAsync()方法;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个原因, IDbSet<省略FindAsync()方法; T> 界面? 查找是接口的一部分,它似乎很奇怪异步版本不可用。我需要投以 DbSet< T> 来访问它,这是一个有点麻烦:

 用户的用户=等待((DbSet<使用者>)db.Users)
.FindAsync(de7d5d4a-9d0f-48ff-9478-d240cd5eb035);


解决方案

如果你拥有的消费者 IDbSet< T> ,我认为你这样做,因为你要访问 FindAsync()从消费者,在那么一个简单的解决方案是创建你自己的界面,包括IDbSet并包含您要使用的任何 FindAsync()方法:

 公共接口IAsyncDbSet< T> :IDbSet< T> 
,其中T:类
{
任务< T> FindAsync(params对象[]键值);
}

这解决了不必转换为DbSet的问题 - 其中,由这样,吹走合同编码的抽象利益。但是,这也介绍了其自身的问题。



有一个更好的解决方案(IMO),需要更多的工作是定义一个只包含所需的成员的接口到的另有你们会DbSet对象使用的,子类DbSet而实现接口,然后使用该界面在你的代码:

 公共接口IMyAsyncDbSet< TEntity> 
,其中TEntity:类
{
TEntity加入(TEntity实体);
TEntity删除(TEntity实体);从IDbSet<

//复制等方法; T>如所须。

任务<对象> FindAsync(params对象[]键值);
}

公共类MyDbSet< T> :DbSet<吨>中IMyAsyncDbSet< T>
,其中T:类
{
}

这是一个适配器模式,真的。它能够消除你的代码从实体框架提供的接口预期的接口。现在,它们是相同的 - 这就是为什么什么都不做,除了继承 DbSet< T> 。但后来他们可能会出现分歧。在这一点上,你仍然可以使用最新DbSet不会破坏你的代码。


Is there a reason that the FindAsync() method is omitted from the IDbSet<T> interface? Find is part of the interface, it seems odd the async version isn't available. I'm needing to cast to DbSet<T> to access it, which is a bit cumbersome:

User user = await ((DbSet<User>)db.Users)
    .FindAsync("de7d5d4a-9d0f-48ff-9478-d240cd5eb035");

解决方案

If you own the consumer of IDbSet<T>, which I assume that you do because you want to have access to FindAsync() from within the consumer, then a simple solution is to create your own interface that includes IDbSet and contains whichever FindAsync() method that you want to use:

public interface IAsyncDbSet<T> : IDbSet<T>
    where T : class
{
    Task<T> FindAsync(params Object[] keyValues);
}

This solves the problem of not having to cast to DbSet - which, by the way, blows away the abstractness benefit of contract coding. But this also introduces its own set of problems.

A better solution (imo) that requires a bit more work is to define an interface that contains only the members that you want to use in what would otherwise be your DbSet object, subclass DbSet while implementing the interface, then use that interface in your code:

public interface IMyAsyncDbSet<TEntity>
    where TEntity : class
{
    TEntity Add(TEntity entity);
    TEntity Remove(TEntity entity);

    // Copy other methods from IDbSet<T> as needed.

    Task<Object> FindAsync(params Object[] keyValues);
}

public class MyDbSet<T> : DbSet<T>, IMyAsyncDbSet<T>
    where T : class
{
}

This is an Adapter pattern, really. It decouples the interface that your code expects from the interface that Entity Framework provides. Right now, they are identical - which is why the implementation does nothing except inherit DbSet<T>. But later on they might diverge. At that point you will still be able to use the latest DbSet without breaking your code.

这篇关于T&GT;没有对IDbSet&LT FindAsync()方法;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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