用不同的数据库进行小巧的查询

时间:2014-07-02 10:22:35

标签: design-patterns dapper

我们在两个不同的数据库上有相同的数据库模式,一个使用Sql Server,另一个使用Oracle。

我正在使用dapper访问数据库并将其映射到强类型对象。

我想要实现的是,我不想使用指定的sql查询,因为它们不同,但返回结果遵循相同的接口/契约。

//example, in sql, select * from table where id = @id
//         in oracle, select * from table where id = :id

    public class AdvertisementRepository : IAdvertisementRepository
    {
        public IEnumerable<Advertisement> GetAll()
        {
            using (IDbConnection conn = DbConn.GetConnection()) // I can change connection easily if one day they want to point to sql server
            {
                string query = @"select "; // I want to make the query very easy to change

                return conn.Query<Advertisement>(query).ToList();
            }
        }
     }

如果有一天他们想要更改,我怎样才能轻松指向另一个数据库。

我目前的想法是注入不同的存储库,然后我会有

public class OracleAdvertisementRepository : IAdvertisementRepository
public class SqlAdvertisementRepository : IAdvertisementRepository

数据库访问的两个实现,但它表示“Dapper没有特定于DB的实现细节”,因此有更简单的方法来实现这一点。

0 个答案:

没有答案