
Option Compare Binary
Option Infer On
Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Data
Imports System.Linq
Imports System.ServiceModel.DomainServices.EntityFramework
Imports System.ServiceModel.DomainServices.Hosting
Imports System.ServiceModel.DomainServices.Server
Imports TransactionsSilverlight.Web


'Implements application logic using the NORTHWNDEntities context.
' TODO: Add your application logic to these methods or in additional methods.
' TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
' Also consider adding roles to restrict access as appropriate.
'<RequiresAuthentication> _
<EnableClientAccess()>  _
Public Class DomainService1
    Inherits LinqToEntitiesDomainService(Of NORTHWNDEntities)
    
    'TODO:
    ' Consider constraining the results of your query method.  If you need additional input you can
    ' add parameters to this method or create additional query methods with different names.
    'To support paging you will need to add ordering to the 'Customers' query.
    Public Function GetCustomers() As IQueryable(Of Customer)
        Return Me.ObjectContext.Customers.Include("Orders.Order_Details")
    End Function
    
    Public Sub InsertCustomer(ByVal customer As Customer)
        If ((customer.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added)
        Else
            Me.ObjectContext.Customers.AddObject(customer)
        End If
    End Sub
    
    Public Sub UpdateCustomer(ByVal currentCustomer As Customer)
        Me.ObjectContext.Customers.AttachAsModified(currentCustomer, Me.ChangeSet.GetOriginal(currentCustomer))
    End Sub
    
    Public Sub DeleteCustomer(ByVal customer As Customer)
        If ((customer.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Deleted)
        Else
            Me.ObjectContext.Customers.Attach(customer)
            Me.ObjectContext.Customers.DeleteObject(customer)
        End If
    End Sub
    
    'TODO:
    ' Consider constraining the results of your query method.  If you need additional input you can
    ' add parameters to this method or create additional query methods with different names.
    'To support paging you will need to add ordering to the 'Orders' query.
    Public Function GetOrders() As IQueryable(Of Order)
        Return Me.ObjectContext.Orders
    End Function
    
    Public Sub InsertOrder(ByVal order As Order)
        If ((order.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(order, EntityState.Added)
        Else
            Me.ObjectContext.Orders.AddObject(order)
        End If
    End Sub
    
    Public Sub UpdateOrder(ByVal currentOrder As Order)
        Me.ObjectContext.Orders.AttachAsModified(currentOrder, Me.ChangeSet.GetOriginal(currentOrder))
    End Sub
    
    Public Sub DeleteOrder(ByVal order As Order)
        If ((order.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(order, EntityState.Deleted)
        Else
            Me.ObjectContext.Orders.Attach(order)
            Me.ObjectContext.Orders.DeleteObject(order)
        End If
    End Sub
    
    'TODO:
    ' Consider constraining the results of your query method.  If you need additional input you can
    ' add parameters to this method or create additional query methods with different names.
    'To support paging you will need to add ordering to the 'Order_Details' query.
    Public Function GetOrder_Details() As IQueryable(Of Order_Detail)
        Return Me.ObjectContext.Order_Details
    End Function
    
    Public Sub InsertOrder_Detail(ByVal order_Detail As Order_Detail)
        If ((order_Detail.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(order_Detail, EntityState.Added)
        Else
            Me.ObjectContext.Order_Details.AddObject(order_Detail)
        End If
    End Sub
    
    Public Sub UpdateOrder_Detail(ByVal currentOrder_Detail As Order_Detail)
        Me.ObjectContext.Order_Details.AttachAsModified(currentOrder_Detail, Me.ChangeSet.GetOriginal(currentOrder_Detail))
    End Sub
    
    Public Sub DeleteOrder_Detail(ByVal order_Detail As Order_Detail)
        If ((order_Detail.EntityState = EntityState.Detached)  _
                    = false) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(order_Detail, EntityState.Deleted)
        Else
            Me.ObjectContext.Order_Details.Attach(order_Detail)
            Me.ObjectContext.Order_Details.DeleteObject(order_Detail)
        End If
    End Sub
End Class

