Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:37:51+00:00 2026-06-16T21:37:51+00:00

I’m trying to generate my DAL layer for ASP.NET web forms project using PetaPoco.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:37:53+00:00Added an answer on June 16, 2026 at 9:37 pm

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to render a haml file in a javascript response like so:
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.