1 out of 2 rated this helpful - Rate this topic

LinqToEntitiesDomainService<TContext> Class

WCF RIA Services

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

Provides a base class for domain services operating on LINQ to Entities data models.

System.Object
  System.ServiceModel.DomainServices.Server.DomainService
    System.ServiceModel.DomainServices.EntityFramework.LinqToEntitiesDomainService<TContext>

Namespace:  System.ServiceModel.DomainServices.EntityFramework
Assembly:  System.ServiceModel.DomainServices.EntityFramework (in System.ServiceModel.DomainServices.EntityFramework.dll)
[LinqToEntitiesDomainServiceDescriptionProviderAttribute]
public abstract class LinqToEntitiesDomainService<TContext> : DomainService
where TContext : new(), ObjectContext

Type Parameters

TContext

The type of the LINQ to Entities ObjectContext.

The LinqToEntitiesDomainService<TContext> type exposes the following members.

  Name Description
Protected method LinqToEntitiesDomainService<TContext> Initializes a new instance of the LinqToEntitiesDomainService<TContext> class.
Top
  Name Description
Protected property AuthorizationContext Gets or sets the optional template AuthorizationContext to use for IsAuthorized. (Inherited from DomainService.)
Protected property ChangeSet Gets the current ChangeSet. (Inherited from DomainService.)
Protected property ObjectContext Gets the ObjectContext for this LinqToEntitiesDomainService<TContext>.
Protected property ServiceContext Gets the active DomainServiceContext for this DomainService. (Inherited from DomainService.)
Protected property ServiceDescription Gets the DomainServiceDescription for this DomainService. (Inherited from DomainService.)
Protected property ValidationContext Gets or sets the optional ValidationContext to use for all validation operations invoked by the DomainService. (Inherited from DomainService.)
Top
  Name Description
Protected method AuthorizeChangeSet Returns a value that indicates the whether the user is authorized to submit the specified ChangeSet. (Inherited from DomainService.)
Protected method Count<T>(IQueryable<T>) Returns the number of rows in the specified IQueryable object. (Overrides DomainService.Count<T>(IQueryable<T>).)
Protected method Count<T>(IQueryable<T>) Returns the number of rows in an IQueryable. (Inherited from DomainService.)
Protected method CreateObjectContext Creates and returns the ObjectContext instance that will be used by this provider.
Public method Dispose() Releases all resources used by the current instance of the DomainService class. (Inherited from DomainService.)
Protected method Dispose(Boolean) Releases all resources used by the LinqToEntitiesDomainService<TContext>. (Overrides DomainService.Dispose(Boolean).)
Public method Equals (Inherited from Object.)
Protected method ExecuteChangeSet Invokes the DomainOperationEntry for each operation in the ChangeSet. (Inherited from DomainService.)
Protected method Finalize (Inherited from Object.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method Initialize Initializes the domain service. (Overrides DomainService.Initialize(DomainServiceContext).)
Public method Invoke Invokes the specified operation. (Inherited from DomainService.)
Public method IsAuthorized Requests authorization for the specified DomainOperationEntry. (Inherited from DomainService.)
Protected method MemberwiseClone (Inherited from Object.)
Protected method OnError Called whenever an unrecoverable error occurs during the processing of a DomainService operation. (Inherited from DomainService.)
Protected method PersistChangeSet Finalizes changes after all the operations in the ChangeSet have been invoked. (Overrides DomainService.PersistChangeSet().)
Public method Query Performs the query operation indicated by the specified QueryDescription. (Inherited from DomainService.)
Protected method ResolveConflicts Resolves the conflicts when the submit fails due to optimistic concurrency conflicts.
Public method Submit Performs the operations indicated by the specified ChangeSet by invoking each of the corresponding domain operations. (Inherited from DomainService.)
Public method ToString (Inherited from Object.)
Protected method ValidateChangeSet Validates the whole ChangeSet before calling ExecuteChangeSet. (Inherited from DomainService.)
Top

You create domain service classes that derive from the LinqToEntitiesDomainService<TContext> class when you expose LINQ to Entities types. When you use the Add New Domain Service Class dialog box to create a domain service that exposes LINQ to Entities types, the dialog box automatically creates a class that derives from LinqToEntitiesDomainService<TContext>. The class is created with methods that provide a starting point for implementing business logic in your application. A query method is included in the class. If you select the Enable Edit check box, insert, update, and delete methods are included. You should add methods or customize the existing methods to meet the requirement of your application.

You use the ObjectContext property to access the object that facilitates interacting with entity data objects.

The following example shows a domain service that derives from LinqToEntitiesDomainService<TContext>. It contains methods for querying, updating, and deleting data.

[EnableClientAccess()]
public class OrderDomainService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
{
    public IQueryable<SalesOrderHeader> GetSalesOrders()
    {
        return this.ObjectContext.SalesOrderHeaders.Include("SalesOrderDetails");
    }

    public void UpdateSalesOrder(SalesOrderHeader currentSalesOrderHeader)
    {
        SalesOrderHeader originalOrder = this.ChangeSet.GetOriginal(currentSalesOrderHeader);

        if ((currentSalesOrderHeader.EntityState == EntityState.Detached))
        {
            if (originalOrder != null)
            {
                this.ObjectContext.AttachAsModified(currentSalesOrderHeader, this.ChangeSet.GetOriginal(currentSalesOrderHeader));
            }
            else
            {
                this.ObjectContext.Attach(currentSalesOrderHeader);
            }
        }

        foreach (SalesOrderDetail detail in this.ChangeSet.GetAssociatedChanges(currentSalesOrderHeader, o => o.SalesOrderDetails))
        {
            ChangeOperation op = this.ChangeSet.GetChangeOperation(detail);
            switch (op)
            {
                case ChangeOperation.Insert:
                    if ((detail.EntityState != EntityState.Added))
                    {
                        if ((detail.EntityState != EntityState.Detached))
                        {
                            this.ObjectContext.ObjectStateManager.ChangeObjectState(detail, EntityState.Added);
                        }
                        else
                        {
                            this.ObjectContext.AddToSalesOrderDetails(detail);
                        }
                    }
                    break;
                case ChangeOperation.Update:
                    this.ObjectContext.AttachAsModified(detail, this.ChangeSet.GetOriginal(detail));
                    break;
                case ChangeOperation.Delete:
                    if (detail.EntityState == EntityState.Detached)
                    {
                        this.ObjectContext.Attach(detail);
                    }
                    this.ObjectContext.DeleteObject(detail);
                    break;
                case ChangeOperation.None:
                    break;
                default:
                    break;
            }
        }
    }

    public void DeleteSalesOrder(SalesOrderHeader salesOrderHeader)
    {
        if ((salesOrderHeader.EntityState == EntityState.Detached))
        {
            this.ObjectContext.Attach(salesOrderHeader);
        }

        switch (salesOrderHeader.Status)
        {
            case 1: // in process
                this.ObjectContext.DeleteObject(salesOrderHeader);
                break;
            case 2: // approved
            case 3: // backordered
            case 4: // rejected
                salesOrderHeader.Status = 6;
                break;
            case 5: // shipped
                throw new ValidationException("The order has been shipped and cannot be deleted.");
            default:
                break;
        }

    }
}


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)