IDbSet <T>上没有FindAsync()方法

Mis*_*pic 21 c# entity-framework asp.net-identity

是否有理由FindAsync()IDbSet<T>界面中省略该方法?Find是界面的一部分,异步版本不可用似乎很奇怪.我需要转换DbSet<T>才能访问它,这有点麻烦:

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

Kei*_*yne 14

如果您拥有IDbSet<T>我的消费者,我认为您这样做是因为您希望FindAsync()从消费者中获得访问权限,那么一个简单的解决方案是创建您自己的包含IDbSet的接口,并包含FindAsync()您想要使用的任何方法:

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

这解决了不必转换为DbSet的问题 - 顺便说一下,它破坏了合同编码的抽象性好处.但这也引入了一系列问题.

需要更多工作的更好的解决方案(imo)是定义一个接口,该接口仅包含您要在DbSet对象中使用的成员,在实现接口时子类DbSet,然后在代码中使用该接口:

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
{
}
Run Code Online (Sandbox Code Playgroud)

这是一个适配器模式,真的.它将您的代码所期望的接口与Entity Framework提供的接口分离.现在,它们是相同的 - 这就是为什么实现除了继承之外什么也不做DbSet<T>.但后来他们可能会分歧.此时,您仍然可以使用最新的DbSet而不会破坏您的代码.


Ron*_*ers 14

以下是我在其中一个项目中解决这个问题的方法:

using System.Threading.Tasks;

namespace System.Data.Entity
{
    public static class IDbSetExtensions
    {
        /// <summary>
        /// If possible asynchronously finds an entity with the given primary key values 
        /// otherwise finds the entity synchronously.  
        /// If an entity with the given primary key values exists in the context, then it is
        /// returned immediately without making a request to the store. Otherwise, a
        /// request is made to the store for an entity with the given primary key values
        /// and this entity, if found, is attached to the context and returned. If no
        /// entity is found in the context or the store, then null is returned.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="this"></param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>A task that represents the asynchronous find operation. The task result contains 
        /// the entity found, or null.</returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static async Task<TEntity> FindAsync<TEntity>(this IDbSet<TEntity> @this, params object[] keyValues)
        where TEntity : class
        {
            DbSet<TEntity> thisDbSet = @this as DbSet<TEntity>;
            if (thisDbSet != null)
            {
                return await thisDbSet.FindAsync(keyValues);
            }
            else
            {
                return @this.Find(keyValues);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

可以考虑将Find方法包装在异步同步模式中,该模式将提供卸载(并且不像真正的异步方法那样具有可伸缩性).但是,调用者必须知道这一点,以确保在调用可能会干扰的FindAsync方法后,他们不会在上下文中调用方法.让调用者意识到特定的实现并不是一个非常好的设计,但是因为它很容易导致问题.对于OP,IDbSet是DbSet,因此调用将是异步的.