没有LINQ或其他ORM的存储库模式?[英] Repository Pattern without LINQ or other ORM?

本文是小编为大家收集整理的关于没有LINQ或其他ORM的存储库模式?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用 Repository 模式而不使用 LINQ 或其他一些 ORM 是否有意义?我正在 MONO 中编写一个应用程序并使用 MySQL,正在考虑使用 repositoy 模式,但无法处理 IQueryable.我正在考虑在存储库中公开更多方法,以明确过滤将在存储库调用的 db 端发生.如果这是对设计的有效使用或任何其他设计理念,有什么建议吗?

推荐答案

Repository 与 IQueryable 没有任何关系.您想到的是 Rob Conory .net 3.5 对存储库模式的看法,这实际上更像是一种数据代理模式.

存储库负责返回对象,并处理数据访问,以便应用程序的其余部分可以对其一无所知.

您可以在 Martin Fowlers 网站上看到非常高级的描述

其他推荐答案

完全有可能.但是您应该将查询移动到存储库站点并为每个类实现一个存储库.例如:

public abstract class GenericRepository : IRepository {
    public virtual T Get<T>(Identity id) where T : PersistentDocument {
        using (IDbConnection connection = GetConnection())
        using(IDbCommand command = CreateGetCommand(id, connection)) {
            using (IDataReader reader = command.ExecuteReader()) {
                var mapper = DaHelper.GetMapper<T>();
                return mapper.Map(reader);
            }
        }
    }

    protected virtual IDbCommand CreateGetCommand(Identity id, IDbConnection connection) {
        IDbCommand command = connection.CreateCommand();
        command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.id = ?", TableName);
        command.Parameters.Add(id.ToGuid());
        return command;
    }

    protected abstract string TableName { get; }
}

public class UserRepository: GenericRepository<User>, IUserRepository
{
    protected override string TableName { get { return "users"; } }

    public User GetByEmail(string email)
    {
        using (IDbConnection connection = GetConnection())
        using (IDbCommand command = connection.CreateCommand())
        {
            command.CommandText  = String.Format("SELECT * FROM {0} e WHERE e.email = ?", TableName);
            command.Parameters.Add(email);
            using (var reader = command.ExecuteReader())
                return DaHelper.GetMapper<T>().Map(reader);
        }
    }
}

其他推荐答案

好的.存储库只是 linq 使用的一种模式.您可以通过它提供任何类型的数据访问.我从事的一个项目使用处理强类型数据集的存储库.

本文地址:https://www.itbaoku.cn/post/627313.html

问题描述

Does it makes sense to use the Repository pattern without the use of LINQ or some other ORM? I am writing an application in MONO and using MySQL, was thinking of using the repositoy pattern but not going to be able to deal with IQueryable. I was thinking of just exposing more methods on the repository to make it obvious that filtering was going to happen on the db side with the repository call. Any suggestions if that is a valid use of the design or any other design ideas instead?

推荐答案

Repository has nothing at all to do with IQueryable. What you are thinking of is the Rob Conory .net 3.5 take on the repository pattern, which is actually more of a data broker pattern.

A repository is responsible for returning objects, and deals with data access so that the rest of your application can remain ignorant of it.

You can see a very high level description on Martin Fowlers site

其他推荐答案

It's absolutely possible. But you should move queries to repository site and implement one repository per class. For example:

public abstract class GenericRepository : IRepository {
    public virtual T Get<T>(Identity id) where T : PersistentDocument {
        using (IDbConnection connection = GetConnection())
        using(IDbCommand command = CreateGetCommand(id, connection)) {
            using (IDataReader reader = command.ExecuteReader()) {
                var mapper = DaHelper.GetMapper<T>();
                return mapper.Map(reader);
            }
        }
    }

    protected virtual IDbCommand CreateGetCommand(Identity id, IDbConnection connection) {
        IDbCommand command = connection.CreateCommand();
        command.CommandText = String.Format("SELECT * FROM {0} e WHERE e.id = ?", TableName);
        command.Parameters.Add(id.ToGuid());
        return command;
    }

    protected abstract string TableName { get; }
}

public class UserRepository: GenericRepository<User>, IUserRepository
{
    protected override string TableName { get { return "users"; } }

    public User GetByEmail(string email)
    {
        using (IDbConnection connection = GetConnection())
        using (IDbCommand command = connection.CreateCommand())
        {
            command.CommandText  = String.Format("SELECT * FROM {0} e WHERE e.email = ?", TableName);
            command.Parameters.Add(email);
            using (var reader = command.ExecuteReader())
                return DaHelper.GetMapper<T>().Map(reader);
        }
    }
}

其他推荐答案

Sure. The repository is simply a pattern used by linq. You can provide any sort of data access you want through it. A project I work on uses repositories that deal with strongly typed DataSets.