DAL与dapper和C# [英] DAL with dapper and C#

查看:207
本文介绍了DAL与dapper和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个利用Dapper的数据访问层,但我不禁觉得它可能更加优雅。 DAL只是根据模型的命名响应传递参数并映射模型,以便至少可以直接理解该部分,但我讨厌看起来重复的代码。

I have a data access layer that utilises Dapper but can't help feeling that it could be far more elegant. The DAL is just passing in parameters and mapping the model as per the named responses of the model so that part is straight forward at least, but I hate code that looks duplicated.

这里是一个示例

 public IEnumerable<Product> ProductSearch(int? userId, DateTime?      modifiedAfter, DateTime? modifiedBefore, Guid? productId)
    {
        IList<Product> products;

        using (var connection = _connection.OpenConnection())
        {
            const string sproc = "dbo.stp_Product_Search";

            products = connection.Query<JobProduct>(sproc, new
            {
                User_ID = userId,
                Modified_After = modifiedAfter,
                Modified_Before = modifiedBefore,
                Product_ID = productId
            }, commandType: CommandType.StoredProcedure)
            .ToList();
        }
        return products;
    }

我有很多类似这样的代码,但是使用了不同的参数和实体。有人有很好的例子吗?

I have lots of code that is like this but with different parameters and entities used. Has anyone got any good examples?

推荐答案

感谢您的建议。这是我最后使用的方法,这意味着我不必每次都在使类减少代码行的情况下使用打开连接的语句来编写代码:

Thanks to the suggestions. This is what I've used in the end and means I don't have to write using statements opening connections each time making my classes less lines of code:

public class Repository<T> where T : class
{
    protected readonly IComplianceConnection Connection;

    public Repository(IComplianceConnection connection)
    {
        Connection = connection;
    }

    public IEnumerable<T> Get(string query, object arguments)
    {
        IList<T> entities;

        using (var connection = Connection.OpenConnection())
        {
            entities = connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).ToList();
        }

        return entities;
    }

    public T GetSingleOrDefault(string query, object arguments)
    {
        T entity;

        using (var connection = Connection.OpenConnection())
        {
            entity =
                connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).SingleOrDefault();
        }

        return entity;
    }

    public void Update(string query, object arguments)
    {
        using (var connection = Connection.OpenConnection())
        {
            connection.Execute(query, arguments, commandType: CommandType.StoredProcedure);
        }
    }

    public int ExecuteScalar(string query, object arguments)
    {
        var id = 0;
        using (var connection = Connection.OpenConnection())
        {
            id = connection.ExecuteScalar<int>(query, arguments, commandType: CommandType.StoredProcedure);
        }
        return id;
    }
}

这篇关于DAL与dapper和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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