English 中文(简体)
处理亚特兰大的亚洲开发银行联系的最佳方式——创造或通过?
原标题:Best way to deal with DB Connection in the DAL - create or pass?

I want my Data Access Layer to be built very modular.
Therefore I have data retrieval methods that sometimes are called directly from the business layer and sometimes are called by other data retrieval methods to create object dependencies.

处理DAL数据库链接的最佳方式是什么?

a) Create a new connection in every method and dispose it afterwards.
Good: Easy to write and to work with.
Bad: Many connections are being opened and closed. (performance?)

b) Pass the connection as an (optional) argument.
Good: I could reuse an open connection for multiple commands.
Bad: I have to keep track of the ownership of the connection (who has to close it?) and cannot use the very neat "using" statements.

<>strong>c 还有一些问题? (Connection as oneton maybe?)

这是我首次撰写了一份真正的DAL文件,因此,我确实能够利用你经历的民间人士的一些帮助。

EDIT:由于情况看上去,它有一个伙伴关系。 网上网站项目。

最佳回答

如果你重新使用伙伴关系。 净额,选项A是你的朋友。

为每项请求建立新的联系,在请求完成时予以处置。 确保你使用<>同一连接座标。 连接(违约)将保持开放,并通过连接库提供。

见http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx“rel=”http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx, 关于连接集合的更多信息。

很有必要以网络服务器的方式这样做,这样,你们不会有任何共同的问题。 一切都需要看好(现在看不出有多少并行工人的胎面在你处执行)。

[编辑成文法]

例如,我认为这是执行储存程序的一种典型方法。 这一点来自一文撰写的《习俗法典》生成者——手写法可能看上去没有什么不同——但只需在以下几个方面达到点:

public int Exec(  int? @iPatientID )
{
  using ( SqlConnection  conn = new SqlConnection( this.ConnectString ) )
  using ( SqlCommand     cmd  = conn.CreateCommand() )
  using ( SqlDataAdapter sda  = new SqlDataAdapter( cmd ) )
  {
    cmd.CommandText = STORED_PROCEDURE_NAME ;
    cmd.CommandType = CommandType.StoredProcedure ;

    if ( this.TimeoutInSeconds.HasValue )
    {
      cmd.CommandTimeout = this.TimeoutInSeconds.Value ;
    }

    //
    // 1. @iPatientID
    //
    SqlParameter p1 = new SqlParameter( @"@iPatientID" , SqlDbType.Int ) ;
    if ( @iPatientID == null )
    {
      p1.Value = System.DBNull.Value ;
    }
    else
    {
      p1.Value = @iPatientID ;
    }
    cmd.Parameters.Add( p1 ) ;

    // add return code parameter
    SqlParameter pReturnCode = new SqlParameter() ;
    pReturnCode.SqlDbType    = System.Data.SqlDbType.Int ;
    pReturnCode.Direction    = System.Data.ParameterDirection.ReturnValue ;
    cmd.Parameters.Add( pReturnCode ) ;

    DataSet ds = new DataSet() ;

    conn.Open() ;
    sda.Fill( ds ) ;
    conn.Close() ;

    this.ResultSet  = ( ds.Tables.Count > 0 ? ds.Tables[0] : null ) ;
    this.ReturnCode = (int) pReturnCode.Value ;

  }

  return this.ReturnCode ;

}
问题回答

我们使用备选案文A。

我们实际上利用实体框架,以便我们能够利用LINQ,这样。 实体框架管理自己的联系,因此创造和删除环境便宜。 然后,我们利用依赖性注射来管理实际建立联系,例如:

public class MyDao 
{
    IFactory<MyDataContext> _contextFactory;
    public MyDao(IFactory<MyDataContext> contextFactory)
    {
        _contextFactory = contextFactory;
    }

    public Foo GetFooById(int fooId)
    {
        using (var context = _contextFactory.Get())
        {
            return context.Foos.Single(f => f.FooId == fooId);
        }
    }
}

这样,如果我们永远决定我们想要利用不同的联系来创造我们的环境,甚至更小的连接,那么我们就能够简单地改变一个地方的受抚养人注射约束,而不必找到每个电话<>新的MyDataContext()。

多数供应商在ODBC级或NET级都汇集了连接。 因此,备选案文A既安全又可能同样好业绩。

http://msdn.microsoft.com/en-us/library/ms254502.aspx”rel=“nofollow”http://msdn.microsoft.com/en-us/library/ms254502.aspx

www.un.org/Depts/DGACM/index_spanish.htm 使用三条:

书写联系作为任择参数。 如果没有(null)通过,就会产生一些(别).、可能)共享来源的联系,那么,你们所有的DAL课程都会产生与相同的确切连接线的连接(如“Nicholas Carey”所述)。 开放和关闭,只有你创建。

如果连接已经通过,假定已经开放,而且不会关闭。 在较高一级,当你称之为这种方法时,你可以使用声明处理关闭连接的问题。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签