在DAL层使用PetaPoco正确的方法(ASP.NET Web窗体VB.NET) [英] Correct way to use PetaPoco in DAL layer (ASP.NET Web Forms VB.NET)

查看:1286
本文介绍了在DAL层使用PetaPoco正确的方法(ASP.NET Web窗体VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成使用PetaPoco ASP.NET Web窗体项目我DAL层。

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

在特定的我很感兴趣,如何打开数据库。
在PetaPoco现场还有就是例子

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

但随后在随PetaPoco的T4发生器有一个很好的部分至极,与所有的DTO一起,产生类似

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 

所以至极是创建我的数据库对象,并与PetaPoco一个DAL层?

So wich is the correct way to create my DB object and use it in a DAL layer with PetaPoco?

此外,我读有一个与PetaPoco一种方法来保持连接打开,并重新使用它,我想这不会是一个BLL / DAL架构可行比如当你已经从BLL访问DB 2-3操作?或者,如果是的话,应如何正确处理?创建DAL方法至极打开连接,并完成所有操作2-3?自开业以来在BLL的连接不应该是这样的。

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.

在此先感谢

推荐答案

更新:这是你如何使用共享连接:

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

这篇关于在DAL层使用PetaPoco正确的方法(ASP.NET Web窗体VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆