/
NonPersistentObjectSpaceHelper.vb
52 lines (48 loc) · 2.09 KB
/
NonPersistentObjectSpaceHelper.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Namespace NonPersistentObjectsDemo.Module
Public Class NonPersistentObjectSpaceHelper
Implements IDisposable
Private application As XafApplication
Private basePersistentTypes() As Type
Public ReadOnly Property AdapterCreators() As List(Of Action(Of NonPersistentObjectSpace))
Public Sub New(ByVal application As XafApplication, ParamArray ByVal basePersistentTypes() As Type)
Me.application = application
Me.basePersistentTypes = basePersistentTypes
Me.AdapterCreators = New List(Of Action(Of NonPersistentObjectSpace))()
AddHandler application.ObjectSpaceCreated, AddressOf Application_ObjectSpaceCreated
NonPersistentObjectSpace.UseKeyComparisonToDetermineIdentity = True
NonPersistentObjectSpace.AutoSetModifiedOnObjectChangeByDefault = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
RemoveHandler application.ObjectSpaceCreated, AddressOf Application_ObjectSpaceCreated
End Sub
Private Sub Application_ObjectSpaceCreated(ByVal sender As Object, ByVal e As ObjectSpaceCreatedEventArgs)
If TypeOf e.ObjectSpace Is NonPersistentObjectSpace Then
Dim npos As NonPersistentObjectSpace = CType(e.ObjectSpace, NonPersistentObjectSpace)
If basePersistentTypes IsNot Nothing Then
For Each type In basePersistentTypes
EnsureObjectSpaceForType(npos, type)
Next type
End If
npos.AutoDisposeAdditionalObjectSpaces = True
For Each adapterCreator In AdapterCreators
adapterCreator.Invoke(npos)
Next adapterCreator
End If
End Sub
Private Sub EnsureObjectSpaceForType(ByVal npos As NonPersistentObjectSpace, ByVal type As Type)
If Not npos.IsKnownType(type) Then
If Not npos.AdditionalObjectSpaces.Any(Function(os) os.IsKnownType(type)) Then
Dim persistentObjectSpace As IObjectSpace = application.CreateObjectSpace(type)
npos.AdditionalObjectSpaces.Add(persistentObjectSpace)
End If
End If
End Sub
End Class
End Namespace