Imports System.Linq
Imports Telerik.OpenAccess
Imports Telerik.OpenAccess.Query

Public Class ContainerManager
    ' return a ChangeSet loaded with objects populated from the database
    Public Shared Function LoadChangeSet(Of T)() _
 As ObjectContainer.ChangeSet
        Dim changeSet As ObjectContainer.ChangeSet = Nothing

        ' get a new object scope for data access in this method
        Using scope As IObjectScope = _
   DataAccessScopeProvider.GetNewObjectScope()
            ' extract data for <T> and place in an IQueryable
            Dim queryable As IQueryable(Of T) = _
               From q In scope.Extent(Of T)() _
               Select q

            ' create a container to hold the data
            Dim container As New ObjectContainer()

            ' define the FetchGroup as default for the copy (fetches all fields)
            Dim collector As IObjectCollector = _
      New FetchGroupCollector(FetchGroupCollector.DefaultFetchGroup)

            ' copy everything from the IQueryable, and name the list as the 
            ' <T> type name
            container.CopyFrom(scope, GetType(T).Name, queryable, collector)

            ' save the current container content in a ChangeSet
            changeSet = container.GetContent()
        End Using

        Return changeSet
    End Function

    Public Shared Function SaveChangeSet( _
 ByVal changeSet As ObjectContainer.ChangeSet) As ObjectContainer.ChangeSet
        Dim result As ObjectContainer.ChangeSet = Nothing

        Using scope As IObjectScope = _
   DataAccessScopeProvider.GetNewObjectScope()
            ' commit the changeset from the client and return the updated set 
            result = ObjectContainer.CommitChanges( _
      changeSet, ObjectContainer.Verify.All, scope, True, True)
        End Using
        Return result
    End Function
End Class
