' NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
Imports Model
Imports Telerik.OpenAccess

Public Class Service1
    Implements IService1

    Public Function IService1_GetContacts() As ObjectContainer.ChangeSet Implements IService1.GetContacts
        Dim changeSet As ObjectContainer.ChangeSet = Nothing
        Using scope As IObjectScope = ServiceScopeProvider.GetNewObjectScope()
            Dim queryable = _
               From q In scope.Extent(Of Contact)() _
               Select q

            changeSet = GetChangeSet("Contacts", scope, queryable)

        End Using
        Return changeSet
    End Function

    Public Function IService1_GetContactGroups() As ObjectContainer.ChangeSet Implements IService1.GetContactGroups
        Dim changeSet As ObjectContainer.ChangeSet = Nothing
        Using scope As IObjectScope = ServiceScopeProvider.GetNewObjectScope()
            Dim queryable = _
               From q In scope.Extent(Of ContactGroup)() _
               Select q

            changeSet = GetChangeSet("ContactGroups", scope, queryable)
        End Using
        Return changeSet
    End Function

    Public Function IService1_SaveContacts(ByVal changeset As ObjectContainer.ChangeSet) As ObjectContainer.ChangeSet Implements IService1.SaveContacts
        Dim result As ObjectContainer.ChangeSet = Nothing

        Using scope As IObjectScope = _
      ServiceScopeProvider.GetNewObjectScope()
            ' commit the changeset from the client and return the updated set 
            ' commits entire transaction?
            result = ObjectContainer.CommitChanges(changeset, _
         ObjectContainer.Verify.All, scope, True, True)
        End Using
        Return result
    End Function

    Private Shared Function GetChangeSet(ByVal listName As String, _
         ByVal scope As IObjectScope, _
         ByVal queryable As Object) As ObjectContainer.ChangeSet

        Dim container As New ObjectContainer()

        ' define the fetch groups used in the copy, by default all columns
        Dim collector As IObjectCollector = _
     New FetchGroupCollector(FetchGroupCollector.DefaultFetchGroup)

        container.CopyFrom(scope, queryable.[GetType]().Name, _
     queryable, collector)

        ' get everything in the container as a changeset
        Dim changeSet As ObjectContainer.ChangeSet = container.GetContent()

        Return changeSet
    End Function

End Class
