问题
I'm trying to generate my DAL layer for ASP.NET web forms project using PetaPoco.
Namespace Eva.Dal.Polls
Public Partial Class EVAMOD_PL_CategoryDb
Private db As Eva.Dal.Core.EvaDb
Public Function Insert(a As EVAMOD_PL_Category) As Object
Return db.Insert(a)
End Function
Public Sub New()
db = New Eva.Dal.Core.EvaDb
End Sub
End Class
Public Partial Class EVAMOD_PL_GL_CategoryDb
Private db As Eva.Dal.Core.EvaDb
Public Function Insert(a As EVAMOD_PL_GL_Category) As Object
Return db.Insert(a)
End Function
Public Sub New()
db = New Eva.Dal.Core.EvaDb
End Sub
End Class
End Namespace
In particular I'm interested in how to open the DB. In PetaPoco site there is the example
// Create a PetaPoco database
objectvar db=new PetaPoco.Database("connectionStringName");
// Show all articles
foreach (var a in db.Query<article>("SELECT * FROM articles")){
Console.WriteLine("{0} - {1}", a.article_id, a.title);
}
But then in the T4 generator shipped with PetaPoco there is a nice section wich, along with all DTOs, generate something like
Namespace Eva.Dal.Core
Public Partial Class EvaDb
Inherits Database
Public Sub New()
MyBase.New("ConnectionString")
CommonConstruct()
End Sub
Public Sub New(connectionStringName As String)
MyBase.New(connectionStringName)
CommonConstruct()
End Sub
Private Partial Sub CommonConstruct()
End Sub
Public Interface IFactory
Function GetInstance() As EvaDb
End Interface
Public Shared Property Factory() As IFactory
Get
Return mFactory
End Get
Set
mFactory = Value
End Set
End Property
Private Shared mFactory As IFactory
Public Shared Function GetInstance() As EvaDb
If istance IsNot Nothing Then
Return istance
End If
If Factory IsNot Nothing Then
Return Factory.GetInstance()
Else
Return New EvaDb
End If
End Function
<ThreadStatic> _
Shared istance As EvaDb
Public Overrides Sub OnBeginTransaction()
If istance Is Nothing Then
istance = Me
End If
End Sub
Public Overrides Sub OnEndTransaction()
If istance Is Me Then
istance = Nothing
End If
End Sub
Public Class Record(Of T As New)
Public Shared ReadOnly Property Repo() As EvaDb
Get
Return EvaDb.GetInstance()
End Get
End Property
Public Function IsNew() As Boolean
Return Repo.IsNew(Me)
End Function
.......
End Class
End Class
End Namespace
So wich is the correct way to create my DB object and use it in a DAL layer with PetaPoco?
Also I read there is a way with PetaPoco to keep a connection open and reuse it, I guess this would not be feasible with a BLL/DAL architecture when for example you have 2-3 operation from the BLL accessing the DB? Or if it is, then how should it be handled correctly? Creating a DAL method wich opens the connection and does all 2-3 operations? Since opening the connection in the BLL should not be the case.
Thanks in advance
回答1:
UPDATE: this is how you can use a shared connection:
public class SharedConnection : IDisposable
{
private Database _db;
public SharedConnection(Database db)
{
_db = db;
_db.OpenSharedConnection();
}
public void Dispose()
{
_db.CloseSharedConnection();
}
}
public class FooBarDao
{
private Database _db = new Database("conn_str_name");
public SharedConnection GetSharedConnection()
{
return new SharedConnection(_db);
}
public Foo GetFoo(string id)
{
return db.SingleOrDefault<Foo>(
"SELECT FooVal FROM FooTbl WHERE Id = @0", id);
}
public Bar GetBar(string id)
{
return db.SingleOrDefault<Bar>(
"SELECT BarVal FROM BarTbl WHERE Id = @0", id);
}
}
public class FooBarManager
{
private FooBarDao _dao = new FooBarDao;
public void GetFooAndBar(string fooId, string barId)
{
using (_dao.GetSharedConnection())
{
_dao.GetFoo(fooId);
_dao.GetBar(barId);
}
}
}
回答2:
I always create a wrapper to instantiate it:
public class DatabaseCreator
public shared function GetContext() As EvaDb
return new EvaDb(ConfigurationManager.ConnectionStrings("X").ConnectionString)
end sub
end class
I would not keep the database connection open longer only for the immediate operations you are performing, but would not leave it in an open state to reuse. With multiple layers, you could conceivably structure the application to share the connection across your business components (like a repository pattern). I've gotten in the habit of opening the connection in each repository method, or passing the reference into the constructor of the repository.
回答3:
Following Brian suggestion and reading another user suggestion here on Stackoverflow, I created a static helper function wich use the current httpcontext to create (or return existing) a single Db istance for each request. This way (vb.net):
Public NotInheritable Class DbHelper
Public Shared Function GetEvaDbIstance() As Eva.Dal.Core.EvaDb
If HttpContext.Current.Items("EvaDb") Is Nothing Then
Dim retval = New Eva.Dal.Core.EvaDb
HttpContext.Current.Items("EvaDb") = retval
Return retval
End If
Return DirectCast(HttpContext.Current.Items("EvaDb"), Eva.Dal.Core.EvaDb)
End Function
Private Sub New()
End Sub
End Class
Then in my Dal object I just
Private db As New Eva.Dal.Core.EvaDb = DbHelper.GetEvaDbIstance()
来源:https://stackoverflow.com/questions/14161176/correct-way-to-use-petapoco-in-dal-layer-asp-net-web-forms-vb-net