1

我正在尝试使用 PetaPoco 为 ASP.NET Web 表单项目生成我的 DAL 层。

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

特别是我对如何打开数据库感兴趣。在 PetaPoco 网站上有这个例子

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

但是在 PetaPoco 附带的 T4 生成器中,有一个不错的部分,它与所有 DTO 一起生成类似

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 

那么使用 PetaPoco 创建我的 DB 对象并在 DAL 层中使用它的正确方法是什么?

另外,我读到 PetaPoco 有一种方法可以保持连接打开并重用它,我想这对于 BLL/DAL 架构是不可行的,例如,当您从 BLL 进行 2-3 次操作访问数据库时?或者如果是,那么应该如何正确处理?创建一个打开连接并执行所有 2-3 操作的 DAL 方法?由于在 BLL 中打开连接不应该是这种情况。

4

3 回答 3

5

更新:这是您可以使用共享连接的方式:

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);
        }
    }
}
于 2013-01-04T18:00:34.383 回答
1

我总是创建一个包装器来实例化它:

public class DatabaseCreator
   public shared function GetContext() As EvaDb
       return new EvaDb(ConfigurationManager.ConnectionStrings("X").ConnectionString)
   end sub
end class

我不会只为您正在执行的即时操作保持数据库连接打开的时间更长,但不会让它处于打开状态以供重用。通过多层,您可以想象构建应用程序以在您的业务组件之间共享连接(如存储库模式)。我已经习惯在每个存储库方法中打开连接,或者将引用传递给存储库的构造函数。

于 2013-01-04T17:20:23.547 回答
0

根据 Brian 的建议并阅读 Stackoverflow 上的另一个用户建议,我创建了一个静态帮助函数,它使用当前的 httpcontext 为每个请求创建(或返回现有的)单个 Db istance。这样(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

然后在我的 Dal 对象中,我只是

    Private db As New Eva.Dal.Core.EvaDb = DbHelper.GetEvaDbIstance()
于 2013-01-04T21:29:18.013 回答