From many days i try to find the way to create Database context that is disposable,i googling much read lots of posts that describing unit of work but that all are looked difficult , so i want some easy way ,i mix some of of my knowledge and make that possible to dispose the context after its work done.
I just Implement IDisposable Interface to my Repository classes and that give me one overridavble method Dispose that is going to dispose ObjectContext. see the below code to see how it works.
Public Interface ITagRepository
Inherits IDisposable
Sub Add_Update_Tag(ByVal tgList As List(Of Tag))
Sub DeleteTag(ByVal tg As Tag)
Function GetTagByName(ByVal tagName As String) As Tag
Function GetTagByTagID(ByVal tagid As Int32) As Tag
Function GetTagsByIDs(ByVal ids() As Integer) As List(Of Tag)
End Interface
<Pluggable("default")> _
Public Class TagRepository
Implements ITagRepository
Private con As Connection
Private context As CallBackDotComEntities
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
Public Function GetTagByName(ByVal tagName As String) As Tag Implements ITagRepository.GetTagByName
Return context.Tags.Where(Function(t) String.Compare(t.TagLabel, tagName, True) = 0).FirstOrDefault
End Function
Public Function GetTagByTagID(ByVal tagid As Integer) As Tag Implements ITagRepository.GetTagByTagID
Return context.Tags.Where(Function(t) t.TagID = tagid).FirstOrDefault
End Function
Public Function GetTagsByIDs(ByVal ids() As Integer) As System.Collections.Generic.List(Of Tag) Implements ITagRepository.GetTagsByIDs
Dim tags As New List(Of Tag)
If IsArray(ids) Then
tags = (From t In context.Tags Join i In ids On t.TagID Equals i Select t).ToList
End If
Return tags
End Function
#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
Here i am going to use that repository with Using code block so it force Repository to dispose and Repository will Dispose the ObjectContext.
Function GetTags() as ViewResult
Using _qr As IQuestionRepository = _of.GetInstance(Of IQuestionRepository)()
'do something here...
End Using
End Function
This works at all. if this post help you even bit then please write your review..
