patternMinor
Handling of open database connection and calling shared function from WebAPI
Viewed 0 times
fromhandlingsharedopenwebapifunctiondatabaseandcallingconnection
Problem
I'm working on an ASP.NET project using VB.NET that uses Dapper and the code as implemented so far runs fine with just me testing. In the example below, Dapper calls a stored proc.
But I am wondering primarily right now if the method of generating an open connection (see
Also, can there be pitfalls to calling a
AutomoblieDomain project
Web UI Project
But I am wondering primarily right now if the method of generating an open connection (see
dbConnFactory below) implemented in the Domain project is not good practice.Also, can there be pitfalls to calling a
Shared function from .NET's WebAPI, whether it the function is for GET, PUT, POST, or Delete?AutomoblieDomain project
Imports System.Data.Common
Imports Dapper
Namespace DAL
Public Class DomainSettings
Public Shared Property CustomConnectionString As String
End Class
Public Class dbConnFactory
Public Shared Function GetOpenConnection() As DbConnection
Dim connection = New SqlClient.SqlConnection(CustomConnectionString)
connection.Open()
Return connection
End Function
End Class
Public Class CarTypes
Public Property CarTypeID As Integer
Public Property CarTypeText As String
Public Shared Function GetList() As IEnumerable(Of CarTypes)
Using conn = dbConnFactory.GetOpenConnection()
Dim _list = conn.Query(Of CarTypes)("dbo.CarTypes_GetList", CommandType.StoredProcedure).ToList()
Return _list
End Using
End Function
End Class
End Namespace
Web UI Project
Imports System.Net
Imports System.Web.Http
Imports AutomobileDomain
Public Class CarTypesController
Inherits ApiController
Public Function GetList() As IEnumerable(Of DAL.CarTypes)
Return DAL.CarTypes.GetList()
End Function
End Class
Solution
According to MSDN:
"We recommend that you always close the connection when you are finished using it in order for the connection to be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside of a using statement in C#, or a Using statement in Visual Basic."
http://msdn.microsoft.com/en-us/library/8xx3tyca(v=VS.80).aspx - Section: "Adding Connections"
The way your code is written, (connection being delivered via a factory method) does look a little risky. However, the way MSDN describes it, at the end of the Using block, the connection object will be automatically closed and disposed.
"We recommend that you always close the connection when you are finished using it in order for the connection to be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside of a using statement in C#, or a Using statement in Visual Basic."
http://msdn.microsoft.com/en-us/library/8xx3tyca(v=VS.80).aspx - Section: "Adding Connections"
The way your code is written, (connection being delivered via a factory method) does look a little risky. However, the way MSDN describes it, at the end of the Using block, the connection object will be automatically closed and disposed.
Context
StackExchange Code Review Q#24542, answer score: 2
Revisions (0)
No revisions yet.