我有一個測試數據庫和位於不同服務器上的實時數據庫。我目前的設計設法與兩個工作,但我想改善訪問。我現在沒有想法,希望得到您的意見。VB.NET實體框架訪問多個數據庫服務器與幾個DbContext類
問題:
我該如何解決以下設計問題?
在程序啓動時,我根據構建模式定義了一個默認連接。
然後,我使用myDBType創建DBAccess對象與我的數據庫進行交互。從現在開始,它會自動連接到測試數據庫或活動數據庫。
Dim userAccess = new UserDBAccess(myDBtype)
userAccess.GetUser()
userAccess.Dispose()
Dim projectAccess = new ProjectDBAccess(myDBType)
projectAccess.DoWork()
projectAccess.Dispose()
我在YouTube上觀看https://www.youtube.com/watch?v=sA-Hp4aBWb4一個SW-架構的視頻,我修改我的需求後得到了這個想法。
因此,直到現在,這看起來像一個非常乾淨的方式,但我遇到了麻煩。
我的問題是,對於每個數據庫訪問,我必須複製/粘貼99%的代碼,具體取決於我想訪問的服務器。例如。
對於現場DB:下面
對於TESTDB ctxLive < @see代碼:ctxTest < @see代碼如下
我有一個基類,所有DBACCESS類派生。
Imports System.Data.Entity
Public MustInherit Class AbstractDBAccess
Implements IDisposable
#Region "Fields"
' Access live db via EF 6
Protected ctxLive As DBLiveEntities
' Access test db via EF 6
Protected ctxTest As DBTestEntities
' Remember DB to access
Protected myDBType As DatabaseType
#End Region
#Region "Enum"
''' <summary>
''' Add more data bases here.
''' </summary>
''' <remarks>Matthias Köhler</remarks>
Public Enum DatabaseType
Live = 0
Test = 1
End Enum
#End Region
#Region "Constructor"
Public Sub New(ByVal dbType As AbstractDBAccess.DatabaseType)
myDBType = dbType '
' Depending on what type we get from startup we grant access to test or live DB
Select Case dbType
Case DatabaseType.Live
Me.ctxLive = New DBLiveEntities
Case DatabaseType.Test
Me.ctxTest = New DBTestEntities
End Select
End Sub
#End Region
#Region "Methods"
Public Function GetDBAccess() As DbContext
' My Problem is i need to return two different types in this method.
' After creating an instance I save which access this object was intended for with "myDBType"
' Both classes derive from DbContext but if I implement it this way I can't see my tables. See screenshot below.
Select Case myDBType
Case DatabaseType.Live
Return Me.ctxLive
Case DatabaseType.Test
Return Me.ctxTest
End Select
Throw New Exception("No matching case for " & myDBType.ToString)
End Function
#End Region
的問題:
您看到選擇情況是99%相同。想象一下複雜的代碼和15個類。我只是不喜歡那個複製粘貼。我只需要更改「ctxLive」或「ctxTest」。
想象一下,在某些年份某人不得不添加另一個數據庫。他必須瀏覽整個代碼併爲每種方法添加一個案例。
難道還有更好的辦法嗎?
這裏這個截圖匹配碼。
Public Class UserDBAccess
Inherits AbstractDBAccess
Implements IDisposable
Public Sub New(ByVal dbType As AbstractDBAccess.DatabaseType)
MyBase.New(dbType)
End Sub
Public Sub GetUser()
' Currently I have to add a lot of select cases to seperate my live DB and test DB.
' They have different connection strings and are on different servers
Select Case Me.myDBType
Case DatabaseType.Live
Me.ctxLive.CCTUsers.Where(Function(u) u.UserName.Contains("StackOverflow"))
Case DatabaseType.Test
Me.ctxTest.CCTUsers.Where(Function(u) u.UserName.Contains("StackOverflow"))
End Select
' I have a lot of Copy Pasting which in my opinion is ugly.
' I want sth like this to save me all that select cases
' The difference here is the "GetDBAccess"
Me.GetDBAccess.CCTUsers.Where(Function(u) u.UserName.Contains("StackOverflow"))
End Sub
End Class
通常這是通過配置處理的。上下文從配置中設置的任何數據庫連接加載。使用的配置綁定到構建模式。那麼只需要一個數據庫上下文。 – dbugger
我在上面。你給了我一個好主意。如果我解決了這個問題,我會回答。 –
我會建議在app.config文件中使用自定義組。 – codeMonger123