Dapper语法-2016 [英] Dapper Syntax - 2016

查看:158
本文介绍了Dapper语法-2016的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Dapper(和Dapper.Contrib)而不是EF来开始我的第一个MVC项目.我试图确保使用时使用正确的语法.搜索网络时,我可以看到随着Dapper的发展,它的一些要求和语法也随之发展.是否有最新的文档显示在2016年使用Dapper的最佳实践?

I'm just beginning my first MVC project using Dapper (and Dapper.Contrib) instead of EF. I'm trying to make sure I'm using the proper syntax when using it. Searching the web, I can see that as Dapper has evolved, so has some of its requirements and syntax. Is there an up to date document that shows best practices for using Dapper in 2016?

具体来说,我的问题是:

Specifically, my questions are:

  • 我需要打开和关闭连接吗?看起来Dapper曾经需要它,但可能不再需要.

  • Do I need to open and close the connection? It looks like Dapper used to require it but may not any longer.

使用Dapper.Contrib,我是否需要将代码封装在使用"调用中,还是Dapper现在会负责自动处理自身?

Using Dapper.Contrib, do I need to encapsulate my code in a 'using' call or will Dapper take care of disposing itself automatically now?

推荐哪种方式?

private string dbconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public Company Add(Company company)
    {

        using (SqlConnection cn = new SqlConnection(dbconn))
        {
            cn.Open();
            var id = cn.Insert(company);
            cn.Close();

            company.id = Convert.ToInt16(id);
        }

        return company;
    }

OR

private IDbConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

public Company Add(Company company)
    {
       var id = cn.Insert(company);
       company.id = Convert.ToInt16(id);

       return company;
    }

推荐答案

使用Dapper,有两种方法来管理连接:

With Dapper, there are two ways to manage connection:

  • 完全管理自己:在这里,您完全负责打开和关闭连接.就像使用ADO.NET时对待连接的方式一样.在这种情况下,应确保使用using块或显式调用Dispose正确处理连接.
  • 允许Dapper对其进行管理:Dapper会自动为您打开连接(如果未打开)并关闭(如果Dapper打开了).这类似于DataAdapter.Fill()方法.
  • Fully manage yourself: Here, you are fully responsible for opening and closing connection. This is just like how you treat connection while working with ADO.NET. In this case, you should make sure you are disposing connection properly using using block or calling Dispose explicitly.
  • Allow Dapper to manage it: Dapper automatically opens the connection (if it was not opened) and closes it (if it was opened by Dapper) for you. This is similar to DataAdapter.Fill() method.

2016年,您应该避免将连接直接暴露给您的应用程序代码.您应该使用UnitOfWorkRepository等模式来实现数据访问层(最好使用已经学习的Dapper等出色的ORM).UnitaWork + Dapper的代码示例可以找到

In 2016, you should avoid exposing your connection directly to your application code. You should implement Data Access Layer (preferably using some good ORM like Dapper that you are already learning) using patterns like UnitOfWork, Repository etc. Code sample for UnitOfWork + Dapper could be found here. Please go through it. I am sure it will help you.

如果您不想实现UnitOfWork,则我建议:

If you do not want to implement UnitOfWork, following is what I propose:

  • 完全管理自己:

以下是示例:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    connection.Execute(......);
    //OR
    var data = connection.Query(......);
    //OR whatever
}

如果您想在更高的范围内共享连接,因此不能使用using块,则只需确保正确连接Dispose.

If you want to share connection over higher scope and hence could not use using block, then just make sure you Dispose connection properly.

  • 允许Dapper对其进行管理:我个人不希望这样做.
  • Allow Dapper to manage it: I personally do not prefer this.

以下是示例:

SqlConnection connection = new SqlConnection(connectionString);
//Dapper will open connection on next line.
var data = connection.Query(......);
//Dapper will close connection in previous call.

这篇关于Dapper语法-2016的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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