-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUnitOfWork.cls
81 lines (65 loc) · 1.94 KB
/
UnitOfWork.cls
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
73
74
75
76
77
78
79
80
81
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "UnitOfWork"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Const CONNECTION_STRING As String = ""
Private repositories As New Dictionary
Private adoConnection As New ADODB.connection
Private disposed As Boolean
Implements IUnitOfWork
Implements IDisposable
Private Sub Class_Initialize()
adoConnection.ConnectionString = CONNECTION_STRING
adoConnection.Open
adoConnection.BeginTrans
End Sub
Private Sub Class_Terminate()
If Not disposed Then Dispose
End Sub
Private Sub Dispose()
Set repositories = Nothing
If Not adoConnection Is Nothing Then
If adoConnection.State = adStateOpen Then
adoConnection.RollbackTrans 'rollback any uncommitted changes
adoConnection.Close
End If
Set adoConnection = Nothing
End If
disposed = True
End Sub
Private Sub IDisposable_Dispose()
If Not disposed Then Dispose
End Sub
Public Sub AddRepository(ByVal key As String, ByRef repo As IRepository)
repo.SetConnection adoConnection
repositories.Add key, repo
End Sub
Public Property Get Repository(ByVal key As String) As IRepository
Set Repository = repositories(key)
End Property
Public Sub Commit()
adoConnection.CommitTrans
adoConnection.BeginTrans
End Sub
Public Sub Rollback()
adoConnection.RollbackTrans
adoConnection.BeginTrans
End Sub
Private Sub IUnitOfWork_AddRepository(ByVal key As String, ByRef repo As IRepository)
AddRepository key, repo
End Sub
Private Sub IUnitOfWork_Commit()
Commit
End Sub
Private Property Get IUnitOfWork_Repository(ByVal key As String) As IRepository
Set IUnitOfWork_Repository = Repository(key)
End Property
Private Sub IUnitOfWork_Rollback()
Rollback
End Sub