patternMinor
UnitOfWork, an excuse for multiple repositories
Viewed 0 times
repositoriesformultipleunitofworkexcuse
Problem
I needed a way of attaching multiple repositories to a presenter. Given I already had an
The UoW opens a connection upon being created and, since it's a unit of work, initiates a transaction. Because I wanted the client code to be able to commit multiple transactions using the same UoW instance, I'm initiating a new transaction every time I
So here it is:
IUnitOfWork class module
UnitOfWork class module
```
Option Explicit
Private Const CONNECTION_STRING As String = "DRIVER={MySQL ODBC 5.1 Driver};UID=;PWD=;SERVER=;DATABASE=;PORT=;"
Private repositories As New Dictionary
Private adoConnection As New ADODB.Connection
Private cmd As New SqlCommand
Implements IUnitOfWork
Private Sub Class_Initialize()
adoConnection.ConnectionString = CONNECTION_STRING
adoConnection.Open
adoConnection.BeginTrans
End Sub
Private Sub Class_Terminate()
On Error GoTo ErrHandler
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
Exit Sub
ErrHandler:
Err.Raise Err.number, Err.source, Err.description, Err.HelpFile, Err.HelpContext
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()
On Error GoTo ErrHandler
adoConnection.CommitTrans
ad
IRepository abstraction, coming up with an IUnitOfWork seemed the next logical step.The UoW opens a connection upon being created and, since it's a unit of work, initiates a transaction. Because I wanted the client code to be able to commit multiple transactions using the same UoW instance, I'm initiating a new transaction every time I
Commit or Rollback.So here it is:
IUnitOfWork class module
Option Explicit
Public Sub AddRepository(repo As IRepository)
End Sub
Public Property Get Repository(ByVal key As String) As IRepository
End Property
Public Sub Commit()
End Sub
Public Sub Rollback()
End SubUnitOfWork class module
```
Option Explicit
Private Const CONNECTION_STRING As String = "DRIVER={MySQL ODBC 5.1 Driver};UID=;PWD=;SERVER=;DATABASE=;PORT=;"
Private repositories As New Dictionary
Private adoConnection As New ADODB.Connection
Private cmd As New SqlCommand
Implements IUnitOfWork
Private Sub Class_Initialize()
adoConnection.ConnectionString = CONNECTION_STRING
adoConnection.Open
adoConnection.BeginTrans
End Sub
Private Sub Class_Terminate()
On Error GoTo ErrHandler
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
Exit Sub
ErrHandler:
Err.Raise Err.number, Err.source, Err.description, Err.HelpFile, Err.HelpContext
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()
On Error GoTo ErrHandler
adoConnection.CommitTrans
ad
Solution
First, to address your question, cleaning up should be a simple fix. Unfortunately,
I don't see a need for the error handler in
Actually, I take that back. Now, I'm not 100% on this, but if
I don't think there's much more to say. Except maybe that I find the readability to be top notch.
ExcludedOrdersPresenter isn't shown. However, that's where the change needs to be made. It should be responsible for setting Me.UnitOfWork = Nothing when it terminates. Honestly though, I can't quite figure out why it isn't doing that implicitly already. Perhaps because of the reference to adodb?I don't see a need for the error handler in
UnitOfWork.Class_Terminate. All it does is re-raise the error exactly as it is. Unless you intend on adding information to the description or source, just remove it and let the error bubble up. The same can be said for Commit and Rollback. Actually, I take that back. Now, I'm not 100% on this, but if
Commit errors, should it try to Rollback? I'm not sure. Consider it. I don't think there's much more to say. Except maybe that I find the readability to be top notch.
Context
StackExchange Code Review Q#60820, answer score: 2
Revisions (0)
No revisions yet.