2013-01-04 29 views
1

我正在嘗試使用PetaPoco爲ASP.NET Web窗體項目生成我的DAL層。在DAL層中使用PetaPoco的正確方法(ASP.NET Web Forms VB.NET)

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 

特別是我對如何打開DB感興趣。 在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在DAL層中使用它?

另外我讀了一個PetaPoco的方法來保持連接打開並重用它,我想這對於BLL/DAL體系結構來說是不可行的,例如,當您從BLL訪問數據庫時有2-3個操作?或者如果是,那麼應該如何正確處理?創建一個DAL方法來打開連接並執行所有2-3個操作?由於在BLL中打開連接不應該如此。

在此先感謝

回答

5

UPDATE:這是你可以使用一個共享的連接:

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

非常非常好,謝謝你的隊友! – Manight

+0

我還是不明白,爲什麼PetaPoco T4創建一個繼承自PetaPoco數據庫類的派生類,並添加了複製基類方法的Record類。 我的意思是......我們不能僅僅實例化基礎PetaPoco數據庫類嗎?什麼是嵌套的Record類用於派生類? 對不起noob問題我真的很陌生OOP – Manight

1

我總是創建一個包裝實例吧:

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

我不會保持數據庫連接打開不再只爲您正在執行的直接操作,但不會離開它在開放狀態下重複使用。使用多個圖層,您可以設想構建應用程序以跨業務組件(如存儲庫模式)共享連接。我習慣於在每個存儲庫方法中打開連接,或者將引用傳遞給存儲庫的構造函數。

+0

我不能只返回新EvaDb沒有連接字符串? 鑑於PetaPoco T4創造的東西? 因此,你最好在每個請求上打開一個DB,而不是在每次調用時創建和銷燬。我想那麼使用httpcontext項來保存當前請求以隔離它與應用程序中的其他併發http請求會更安全? – Manight

0

繼布萊恩建議,並在這裏閱讀其他用戶的建議#2,我創建了一個靜態輔助功能至極使用當前的HttpContext創建(或返回現有的)每個請求的單個Db資金。通過這種方式(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 

然後在我的達爾對象我只是

Private db As New Eva.Dal.Core.EvaDb = DbHelper.GetEvaDbIstance() 
相關問題