[英]Managing DbContext Lifetime in a Thick WPF Client
I know this question has been asked and discussed a lot (eg Here and Here and this Article ). 我知道这个问题已经提出并讨论了很多(例如Here and Here和这篇文章 )。 Nevertheless, i still feel confused about it.
尽管如此,我仍然对此感到困惑。 I know that
DbContext
s should not live for the duration of application lifetime, i know they should be used per Form (window) or per Presenter. 我知道
DbContext
不应在应用程序生存期内生存,我知道应在每个Form(窗口)或Presenter中使用它们。 The problem is that i don't have Forms or Presenters. 问题是我没有表格或演示者。 I have a single Form (Window) with many view models, some of them which live for the duration of the application and almost all of my view models depend on
DbContext
(LOB application, WPF, MVVM, Sql Server CE). 我有一个包含许多视图模型的窗体(窗口),其中一些在应用程序期间有效,并且几乎所有视图模型都依赖于
DbContext
(LOB应用程序,WPF,MVVM,Sql Server CE)。
My solution is to hide DbContext
behind a factory which is injected in all view models that need access to DbContext
and those view models create/dispose of the DbContext
when their corresponding view is loaded/unloaded. 我的解决方案是将
DbContext
隐藏在一个工厂后面,该工厂注入到需要访问DbContext
所有视图模型中,并且这些视图模型在加载/卸载其对应视图时创建/处理DbContext
。 I would like to know if this solution has any problems or if there is a better solution you could advise ? 我想知道此解决方案是否有问题,或者是否有更好的解决方案可以建议?
I tend to lay my projects out as follows; 我倾向于将我的项目安排如下:
1) Presentation Layer: 1)表示层:
Contains my Views and ViewModels
2) Business Layer: 2)业务层:
Contains my business logic
3) Data Layer: 3)数据层:
Contains my models
My Presentation layer calls into the business layer to populate local copies (held in the ViewModel) of the data I wish to use in my ViewModel / View. 我的Presentation层调用业务层,以填充要在ViewModel / View中使用的数据的本地副本(保存在ViewModel中)。
This is achieved with a Using statement, something like; 这可以通过using语句来实现,例如:
Using DBContext As Entities = ConnectToDatabase()
Dim clsApprovalTypes As New Repositories.clsApprovalTypesRepository(DBContext)
dbResults = clsApprovalTypes.GetRecords()
End Using
Return dbResults
Here I simply pass in the Context into Repository, once the data has been returned, the "End Using" will dispose of my context. 在这里,我只是将上下文传递到存储库中,一旦返回数据,“最终使用”将处理我的上下文。
To update the context with changes made in my ViewModel / View, I use an AddEdit routine, which accepts a record, and updates / Adds to the context as necessary, using a similar methodology to the above, something like; 要使用在ViewModel / View中所做的更改来更新上下文,我使用AddEdit例程,该例程接受一条记录,并使用与上述类似的方法,根据需要更新/添加到上下文中,例如:
Using DBContext As CriticalPathEntities = ConnectToDatabase()
Dim clsApprovalTypes As New Repositories.clsApprovalTypesRepository(DBContext)
clsApprovalTypes.AddEditRecord(ApprovalTypeToSave)
Try
clsApprovalTypes.SaveData()
Catch ex As Exception
Return ex
End Try
End Using
Where my AddEdit routine is something like; 我的AddEdit例程类似于
SavedRecord = New ApprovalType 'Store the Saved Record for use later
Dim query = From c In DBContext.ApprovalTypes
Where c.ApprovalType_ID = RecordToSave.ApprovalType_ID
Select c
If query.Count > 0 Then
SavedRecord = query.FirstOrDefault
End If
'
' Use Reflection here to copy all matching Properties between the Source Entity
' and the Entity to be Saved...
'
SavedRecord = Classes.clsHelpers.CopyProperties(RecordToSave, SavedRecord)
If query.Count = 0 Then
Try
DBContext.ApprovalTypes.Add(SavedRecord)
Catch ex As EntityException
Return ex
End Try
End If
I wrote a little more about some of this here; 我在这里写了一些有关它的内容。
https://stackoverflow.com/a/15014599/1305169 https://stackoverflow.com/a/15014599/1305169
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.