我正在考虑使用 Entity Framework 5 和 Database First(连接到 SQL Server 2008 R2)创建一个 VB.NET 11 WPF MVVM 应用程序。
我选择了 Database First,因为我正在将现有解决方案迁移到 WPF MVVM,当然,其中的数据库已经存在。
我想开始使用依赖注入,这样我就可以尽可能多地对我的代码进行单元测试。
我似乎无法找到关于如何将依赖注入与 EF DB-First,尤其是 vb.net 一起使用的清晰简洁的演练。尽管我敢肯定,即使是 C# 示例也可以。
我真正想要的是一个简单的分步指南,解释如何设置解决方案,如何设置每个部分为依赖注入做好准备等,但这些似乎很难实现。
到目前为止,我已经创建了解决方案及其项目,如下所示;
- DBAccess - 它只包含我的 .edmx 文件和一个能够将 ConnectionString 提供给构造函数的小模块。
- DBControl - 这包含了我用来在我的 EDMX 和我的 ViewModel 之间提供一个层的各种类。具体来说,我在这里填充复杂类型(我使用设计器创建的)以通过 UI 显示“更友好”的数据,以及将这些“友好”复杂类型转换为映射实体以进行保存/更新。我的数据库中每个表都有一个类。每个都有两个“FetchFriendlyRecords”方法(一个接受过滤器)和一个“ AddUpdateFriendlyRecord ”方法。我为每个类创建了一个接口。每个类在其构造函数中接受一个 DbContext,我只是从 DBAccess 项目传递我的 DBContext。
- MainUI - 这包含我的 MVVM 层,并引用 DBControl 项目中的每个类以提供 DataBinding 等。
我已经看到建议,与其花时间编写一个能够使用 EF 进行单元测试的复杂解决方案,不如创建一个填充了测试数据的牢固模拟数据库,并将代码指向模拟数据库,而不是住一个。但是,我更希望能够创建一个完全无需访问 SQL Server 即可运行的内存解决方案。
任何帮助都会很棒,包括告诉我这一切是否都错了!!
更新:
我采用了下面 Paul Kirby 提供的解决方案,并创建了我相信的“某种”存储库模式。
我创建了一个界面;
Public Interface IFriendlyRepository(Of T)
ReadOnly Property FriendlyRecords As ObservableCollection(Of T)
Function GetFilteredFriendlyRecords(predicates As List(of Func(Of T, Boolean))) As ObservableCollection(Of T)
Function AddEditFriendlyRecord(ByVal RecordToSave As T) As EntityException
Sub SaveData()
End Interface
然后我逐个类地实现了这个接口;
Namespace Repositories
Public Class clsCurrenciesRepository
Implements Interfaces.IFriendlyRepository(Of CriticalPathDB.FriendlyCurrencies)
Private _DBContext As CriticalPathEntities 'The Data Context
Public Sub New(ByVal Context As DbContext)
_DBContext = Context
End Sub
Public ReadOnly Property FriendlyRecords As ObservableCollection(Of FriendlyCurrencies) Implements Interfaces.IFriendlyRepository(Of CriticalPathDB.FriendlyCurrencies).FriendlyRecords
Get
' We need to convert the results of a Linq to SQL stored procedure to a list,
' otherwise we get an error stating that the query cannot be enumerated twice!
Dim Query = (From Currencies In _DBContext.Currencies.ToList
Group Join CreationUsers In _DBContext.Users.ToList
On Currencies.CreationUserCode Equals CreationUsers.User_Code Into JoinedCreationUsers = Group
From CreationUsers In JoinedCreationUsers.DefaultIfEmpty
Group Join UpdateUsers In _DBContext.Users.ToList
On Currencies.LastUpdateUserCode Equals UpdateUsers.User_Code Into JoinedUpdateUsers = Group
From UpdateUsers In JoinedUpdateUsers.DefaultIfEmpty
Where (Currencies.Deleted = False Or Currencies.Deleted Is Nothing)
Order By Currencies.NAME
Select New FriendlyCurrencies With {.Currency_Code = Currencies.Currency_Code,
.NAME = Currencies.NAME,
.Rate = Currencies.Rate,
.CreatedBy = If(Currencies.CreationUserCode Is Nothing, "", CreationUsers.First_Name & " " & CreationUsers.Last_Name),
.CreationDate = Currencies.CreationDate,
.CreationUserCode = Currencies.CreationUserCode,
.Deleted = Currencies.Deleted,
.LastUpdateDate = Currencies.LastUpdateDate,
.LastUpdatedBy = If(Currencies.LastUpdateUserCode Is Nothing, "", UpdateUsers.First_Name & " " & UpdateUsers.Last_Name),
.LastUpdateUserCode = Currencies.LastUpdateUserCode}).ToList
Return New ObservableCollection(Of FriendlyCurrencies)(Query)
End Get
End Property
Public Function GetFilteredFriendlyRecords(predicates As List(of Func(Of FriendlyCurrencies, Boolean))) As ObservableCollection(Of FriendlyCurrencies) Implements Interfaces.IFriendlyRepository(Of CriticalPathDB.FriendlyCurrencies).GetFilteredFriendlyRecords
Dim ReturnQuery = FriendlyRecords.ToList
For Each Predicate As Func(Of FriendlyCurrencies, Boolean) In predicates
If Predicate IsNot Nothing Then
ReturnQuery = ReturnQuery.Where(Predicate).ToList
End If
Next
Return New ObservableCollection(Of FriendlyCurrencies)(ReturnQuery)
End Function
Public Function AddEditFriendlyRecord(ByVal RecordToSave As FriendlyCurrencies) As EntityException Implements Interfaces.IFriendlyRepository(Of CriticalPathDB.FriendlyCurrencies).AddEditFriendlyRecord
Dim dbCurrency As New Currency
' Check if this Staff Member Exists
Dim query = From c In _DBContext.Currencies
Where c.Currency_Code = RecordToSave.Currency_Code
Select c
' If Asset exists, then edit.
If query.Count > 0 Then
dbCurrency = query.FirstOrDefault
Else
'Do Nothing
End If
dbCurrency.Currency_Code = RecordToSave.Currency_Code
dbCurrency.NAME = RecordToSave.NAME
dbCurrency.CreationDate = RecordToSave.CreationDate
dbCurrency.CreationUserCode = RecordToSave.CreationUserCode
dbCurrency.LastUpdateDate = RecordToSave.LastUpdateDate
dbCurrency.LastUpdateUserCode = RecordToSave.LastUpdateUserCode
dbCurrency.Deleted = RecordToSave.Deleted
' Save Asset Object to Database
If query.Count > 0 Then
' If Asset exists, then edit.
Try
'_dbContext.SaveChanges 'We could save here but it's generally bad practice
Catch ex As EntityException
Return ex
End Try
Else
Try
_DBContext.Currencies.Add(dbCurrency)
'_dbContext.SaveChanges 'We could save here but it's generally bad practice
Catch ex As EntityException
Return ex
End Try
End If
Return Nothing
End Function
Public Sub SaveData() Implements Interfaces.IFriendlyRepository(Of CriticalPathDB.FriendlyCurrencies).SaveData
_DBContext.SaveChanges()
End Sub
End Class
End Namespace
我使用构造函数注入将 dbContext 插入到类中。
我曾希望能够使用我现有的上下文和“努力”单元测试工具来模拟一个假的 dbContext 。
但是,我似乎无法让它发挥作用。
在此期间,在我的单元测试项目中,我正在删除(如果它已经存在)并使用 SQLCMD 命令创建一个空的测试数据库,使用与我的实时数据库相同的模式。
然后我创建一个引用测试数据库的 dbContext,用测试数据填充它,并对此进行测试。
作为说明,我将重构我的“添加/编辑”方法以使用实际的基本实体,而不是我的“友好”复杂版本,这是当时最简单的方法。