In this post i am going to show you how to setup Unit of work using transactionscope in easy way.
let's start with creating simple repositories.
Public Interface ITagRepository
Inherits IDisposable 'for dispose database context
Sub Add_Update_Tag(ByVal tgList As List(Of Tag))
Sub DeleteTag(ByVal tg As Tag)
End Interface
Public Interface IUserRepository
Inherits IDisposable
Function Add_Update_User(ByVal u As OpenIDUser) As OpenIDUser
Sub RemoveUser(ByVal u As OpenIDUser)
End Interface
' -------------------------------------------------------------------------
Public Class UserRepository
Implements IUserRepository
Private con As Connection
Private context As MyDBEntities
Sub New()
con = New Connection
context = con.GetContext
End Sub
Public Function Add_Update_User(ByVal u As OpenIDUser) As OpenIDUser Implements IUserRepository.Add_Update_User
If u.UserID > 0 Then
context.OpenIDUsers.Attach(New OpenIDUser With {.UserID = u.UserID})
context.OpenIDUsers.ApplyCurrentValues(u)
Else
context.OpenIDUsers.AddObject(u)
End If
context.SaveChanges()
Return u
End Function
Public Sub RemoveUser(ByVal u As OpenIDUser) Implements IUserRepository.RemoveUser
context.OpenIDUsers.Attach(u)
context.OpenIDUsers.DeleteObject(u)
context.SaveChanges()
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
context.Dispose()
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Public Class TagRepository
Implements ITagRepository
Private con As Connection
Private context As MyDBEntities
Sub New()
con = New Connection
context = con.GetContext
End Sub
Public Sub Add_Update_Tag(ByVal tgList As List(Of Tag)) Implements ITagRepository.Add_Update_Tag
For Each tg In tgList
If tg.TagID > 0 Then
context.Tags.Attach(New Tag With {.TagID = tg.TagID})
context.Tags.ApplyCurrentValues(tg)
Else
context.Tags.AddObject(tg)
End If
Next
context.SaveChanges()
End Sub
Public Sub DeleteTag(ByVal tg As Tag) Implements ITagRepository.DeleteTag
context.Tags.Attach(tg)
context.Tags.DeleteObject(tg)
context.SaveChanges()
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
context.Dispose()
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Function TestMethod(ByVal returnUrl As String) As ActionResult
Using _ur As IUserRepository = _of.GetInstance(Of IUserRepository)(),
_ts = New TransactionScope, _tr = _of.GetInstance(Of ITagRepository)()
'write your logic here....
_tr.Complete()
End Using
End Function
Above i create simple controller action that uses all above repository to for done there work it wrap with using code block because it dispose the repository at end and repository is implements from Idisposable so its called its dispose method that we use for dispose our database ObjectContext and also we use transactionScope class that will commit all the transaction done by repository when we call Complete() method of transactionscope ,if any exception generate in between that all transaction would be canceled.
This is the easy way for me,also there are lots of other resources in web to maintain Unit of work. Enjoy.....
