Search code examples
asp.netvb.netormdata-access-layerpetapoco

Correct way to use PetaPoco in DAL layer (ASP.NET Web Forms VB.NET)


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 which, 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 which 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 which opens the connection and does all 2-3 operations? Since opening the connection in the BLL should not be the case.


Solution

  • 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);
            }
        }
    }