/
XpoImportHelper.vb
72 lines (67 loc) · 2.15 KB
/
XpoImportHelper.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Imports DevExpress.Xpo
Imports DevExpress.Xpo.Helpers
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Namespace DevExpress.Sample
Public MustInherit Class XpoImportHelper
Private dataLayer As IDataLayer
Private commandChannel As ICommandChannel
Private batchSize As Integer
Public Sub New(ByVal dataLayer As IDataLayer, ByVal batchSize As Integer)
Me.dataLayer = dataLayer
Me.commandChannel = TryCast(dataLayer, ICommandChannel)
Me.batchSize = batchSize
End Sub
Protected MustOverride Function CreatePersistentObject(ByVal session As Session, ByVal sourceObject As Object) As Object
Public Sub Import(ByVal sourceObjects As IEnumerable)
If commandChannel IsNot Nothing Then
ImportInBatches(sourceObjects)
Else
ImportAtOnce(sourceObjects)
End If
End Sub
Private Sub ImportAtOnce(ByVal sourceObjects As IEnumerable)
Using uow As New UnitOfWork(dataLayer)
For Each current As Object In sourceObjects
Dim obj As Object = CreatePersistentObject(uow, current)
uow.Save(obj)
Next current
uow.CommitChanges()
End Using
End Sub
Private Sub ImportInBatches(ByVal sourceObjects As IEnumerable)
commandChannel.Do(CommandChannelHelper.Command_ExplicitBeginTransaction, Nothing)
Dim uow As UnitOfWork = Nothing
Try
Dim i As Integer = 0
Dim enumerator As IEnumerator = sourceObjects.GetEnumerator()
Dim more As Boolean = enumerator.MoveNext()
Do While more
If uow Is Nothing Then
uow = New UnitOfWork(dataLayer)
End If
Dim obj As Object = CreatePersistentObject(uow, enumerator.Current)
uow.Save(obj)
more = enumerator.MoveNext()
i += 1
If Not more OrElse i >= batchSize Then
uow.CommitChanges()
uow.Dispose()
uow = Nothing
i = 0
End If
Loop
commandChannel.Do(CommandChannelHelper.Command_ExplicitCommitTransaction, Nothing)
Catch ex As Exception
If uow IsNot Nothing Then
uow.Dispose()
End If
commandChannel.Do(CommandChannelHelper.Command_ExplicitRollbackTransaction, Nothing)
Throw ex
End Try
End Sub
End Class
End Namespace