CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: VB.NET

EF Repo in VB.NET

391 Views
Copy Code Show/Hide Line Numbers
   1:  '------------------------------------------------------------------------------
   2:  ' <auto-generated>
   3:  '     This code was generated from a template.
   4:  '
   5:  '     Changes to this file may cause incorrect behavior and will be lost if
   6:  '     the code is regenerated.
   7:  ' </auto-generated>
   8:  '------------------------------------------------------------------------------
   9:   
  10:  Option Compare Binary
  11:  Option Infer On
  12:  Option Strict On
  13:  Option Explicit On
  14:   
  15:  Imports System.ComponentModel
  16:  Imports System.Web
  17:  Imports System.Data
  18:  Imports System.Data.Common
  19:  Imports System.Data.Objects
  20:  Imports System.Data.EntityClient
  21:   
  22:  Public Interface IEntityRepository(Of T)
  23:      Sub Add(ByVal entity As T)
  24:      Sub Delete(ByVal entity As T)
  25:      Sub DeleteById(ByVal id As Integer)
  26:      Function GetById(ByVal id As Integer) As T
  27:      Function List() As IQueryable(Of T)
  28:      Function SaveChanges() As Integer
  29:  End Interface
  30:   
  31:   
  32:      ''' <summary>
  33:      ''' Object context for use with asp.net, or Windows/WPF.
  34:      ''' </summary>
  35:      ''' <remarks></remarks>
  36:      Public NotInheritable Class EntityContext
  37:      
  38:          Private Sub New()
  39:          End Sub
  40:   
  41:          <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", _
  42:              MessageId:="System.Int32.ToString(System.String)")> _
  43:          Public Shared ReadOnly Property Context() As CmsServer.CmsAppEntities
  44:              Get
  45:                  If Not httpContext.Current is nothing then
  46:                      Dim objectContextKey As String = "Entity" + HttpContext.Current.GetHashCode().ToString("x")
  47:                      If Not HttpContext.Current.Items.Contains(objectContextKey) Then
  48:                          HttpContext.Current.Items.Add(objectContextKey, New CmsServer.CmsAppEntities())
  49:                      End If
  50:                      Return TryCast(HttpContext.Current.Items(objectContextKey), CmsServer.CmsAppEntities)
  51:                  Else
  52:                      ' Creates a Thread Scoped DataContext object that can be reused. 
  53:                      ' The DataContext is stored in Thread local storage.
  54:                      ' See here http://code.msdn.microsoft.com/multitierlinqtosql/Thread/View.aspx?ThreadId=361
  55:                      'for a discussion of this code.
  56:                      Dim DataContextKey as String = "CmsAppEntities"
  57:                      Dim threadData As LocalDataStoreSlot =System.Threading.Thread.GetNamedDataSlot(DataContextKey)
  58:                      Dim dataContext As Object = Nothing
  59:                      
  60:                      If Not threadData is Nothing Then
  61:                          dataContext = System.Threading.Thread.GetData(threadData)
  62:                      End If
  63:                      
  64:                      If dataContext is Nothing Then
  65:                          dataContext = New CmsServer.CmsAppEntities()
  66:                          If threadData is Nothing Then
  67:                              threadData = System.Threading.Thread.AllocateNamedDataSlot(DataContextKey)
  68:                          End If
  69:                      
  70:                          System.Threading.Thread.SetData(threadData, dataContext)
  71:                      End If
  72:                      Return DirectCast(dataContext, CmsServer.CmsAppEntities)
  73:   
  74:                  End If                    
  75:              End Get
  76:          End Property
  77:      
  78:      End Class
  79:   
  80:      ''' <summary>
  81:      ''' Main repository for the Entities
  82:      ''' </summary>
  83:      ''' <remarks></remarks>
  84:      Partial Public NotInheritable Class EntityRepo
  85:   
  86:          Private Sub New()
  87:          End Sub
  88:   
  89:  #Region "Members"
  90:   
  91:      Private Shared _Article As ArticleRepo
  92:      Public Shared ReadOnly Property Article As ArticleRepo
  93:          Get
  94:              If _Article Is Nothing Then _Article = New ArticleRepo
  95:              Return _Article
  96:          End Get
  97:      End Property
  98:   
  99:      Private Shared _ArticleType As ArticleTypeRepo
 100:      Public Shared ReadOnly Property ArticleType As ArticleTypeRepo
 101:          Get
 102:              If _ArticleType Is Nothing Then _ArticleType = New ArticleTypeRepo
 103:              Return _ArticleType
 104:          End Get
 105:      End Property
 106:   
 107:  #End Region
 108:   
 109:      End Class
 110:   
 111:  Partial Public Class ArticleRepo
 112:      Implements IEntityRepository(Of Article)
 113:   
 114:      Public Sub New()
 115:      End Sub
 116:   
 117:  #Region "Members"
 118:   
 119:      Public Sub Add(ByVal entity As Article) Implements IEntityRepository(Of Article).Add
 120:          EntityContext.Context.Article.AddObject(entity)
 121:          SaveChanges()
 122:      End Sub
 123:   
 124:      Public Sub Delete(ByVal entity As Article) Implements IEntityRepository(Of Article).Delete
 125:          EntityContext.Context.Article.DeleteObject(entity)
 126:          SaveChanges()
 127:      End Sub
 128:   
 129:      Public Sub DeleteById(ByVal id As Integer) Implements IEntityRepository(Of Article).DeleteById
 130:          Dim entity As Article = GetById(id)
 131:          If Not entity Is Nothing Then Delete(entity)
 132:      End Sub
 133:   
 134:      Public Function GetById(ByVal id As Integer) As Article Implements IEntityRepository(Of Article).GetById
 135:          Return (From g In EntityContext.Context.Article Where g.ArticleId = id Select g).SingleOrDefault()
 136:      End Function
 137:   
 138:      Public Function List() As System.Linq.IQueryable(Of Article) Implements IEntityRepository(Of Article).List
 139:          Return EntityContext.Context.Article
 140:      End Function
 141:   
 142:      Public Overridable Function SaveChanges() As Integer Implements IEntityRepository(Of Article).SaveChanges
 143:          Dim RetVal As Integer = 0
 144:          RetVal = EntityContext.Context.SaveChanges()
 145:          Return RetVal
 146:      End Function
 147:   
 148:  #End Region
 149:   
 150:  End Class
 151:   
 152:  Partial Public Class ArticleTypeRepo
 153:      Implements IEntityRepository(Of ArticleType)
 154:   
 155:      Public Sub New()
 156:      End Sub
 157:   
 158:  #Region "Members"
 159:   
 160:      Public Sub Add(ByVal entity As ArticleType) Implements IEntityRepository(Of ArticleType).Add
 161:          EntityContext.Context.ArticleType.AddObject(entity)
 162:          SaveChanges()
 163:      End Sub
 164:   
 165:      Public Sub Delete(ByVal entity As ArticleType) Implements IEntityRepository(Of ArticleType).Delete
 166:          EntityContext.Context.ArticleType.DeleteObject(entity)
 167:          SaveChanges()
 168:      End Sub
 169:   
 170:      Public Sub DeleteById(ByVal id As Integer) Implements IEntityRepository(Of ArticleType).DeleteById
 171:          Dim entity As ArticleType = GetById(id)
 172:          If Not entity Is Nothing Then Delete(entity)
 173:      End Sub
 174:   
 175:      Public Function GetById(ByVal id As Integer) As ArticleType Implements IEntityRepository(Of ArticleType).GetById
 176:          Return (From g In EntityContext.Context.ArticleType Where g.ArticleTypeId = id Select g).SingleOrDefault()
 177:      End Function
 178:   
 179:      Public Function List() As System.Linq.IQueryable(Of ArticleType) Implements IEntityRepository(Of ArticleType).List
 180:          Return EntityContext.Context.ArticleType
 181:      End Function
 182:   
 183:      Public Overridable Function SaveChanges() As Integer Implements IEntityRepository(Of ArticleType).SaveChanges
 184:          Dim RetVal As Integer = 0
 185:          RetVal = EntityContext.Context.SaveChanges()
 186:          Return RetVal
 187:      End Function
 188:   
 189:  #End Region
 190:   
 191:  End Class
 192:   
by rickrat
  June 13, 2010 @ 4:51pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate