.net体系结构:通常创建一个IRepository <T>

Mor*_*Man 4 .net architecture generics repository-pattern

我使用通用存储库接口,允许我添加,查找,更新和删除不同类型的对象.然后我实现了两个具体的存储库,可以在不更改应用程序的情况下切换它们.一切都奇妙地断开了.但我遇到了障碍.我的存储库方法中的代码感觉不对,我无法弄清楚如何做得更好.这是我的代码片段:

public class OracleRepository<T> : IRepository<T> where T : new()
{
    public IQueryable<T> GetAll()
    {
        if (typeof(T) == typeof(Object1))
        {
            return (IQueryable<T>)DataAccess.GetAllObject1().AsQueryable();

        }

        if (typeof(T) == typeof(Object2))
        {
            return (IQueryable<T>)DataAccess.GetAllObject2().AsQueryable();
        }

        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我的DataAccess(从datareader创建对象)不是通用的,并且对于它支持的每种类型的对象都有特定的方法.

我可以重写上面的内容,以便我使用泛型或其他方法避免使用if-then-elseif列表吗?

Jon*_*eet 11

一种常见的模式是使用从类型到相关类型的函数的字典.例如:

private static readonly IDictionary<Type, Func<object>> Fetchers =
    new Dictionary<Type, Func<object>>();
    {
        (typeof(Object1), () => DataAccess.GetAllObject1().AsQueryable()),
        (typeof(Object2), () => DataAccess.GetAllObject2().AsQueryable()),
        // etc
    };

public IQueryable<T> GetAll()
{
    Func<object> func = Fetchers[typeof(T)];
    if (func == null)
    {
        throw new NotImplementedException();
    }
    return (IQueryable<T>) func();
}
Run Code Online (Sandbox Code Playgroud)

演员是因为你无法真正表达字典中的类型关系,但至少它更容易添加新类型等.

您可以根据返回的内容将呼叫转移AsQueryable()到.GetAll()GetAllObject1()