示例#1
0
        /// <summary>
        /// Executes the <see cref="DataDeleteRequest{TEntity}" />.
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override Message Execute(DataRequestParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (Entities == null || !Entities.Any())
            {
                throw new ArgumentException("Could not execute request because no entities have been set.");
            }

            DataServiceContext serviceContext = Resolve(parameters);

            foreach (TEntity entity in Entities)
            {
                if (!serviceContext.Entities.Contains(serviceContext.GetEntityDescriptor(entity)))
                {
                    serviceContext.AttachTo(parameters.EntitySet, entity);
                }
                serviceContext.DeleteObject(entity);
            }
            serviceContext.SaveChanges();
            return(null);
        }
示例#2
0
        /// <summary>
        /// Resolves a <see cref="DataServiceContext" /> based on the specified <see cref="DataRequestParameters" />.
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected DataServiceContext Resolve(DataRequestParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (string.IsNullOrEmpty(parameters.BaseUrl))
            {
                throw new ArgumentException("BaseUrl has not been set.");
            }

            if (string.IsNullOrEmpty(parameters.EntitySet))
            {
                throw new ArgumentException("EntitySet has not been set.");
            }

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri(parameters.Url),
                                                                           DataServiceProtocolVersion.V3)
            {
                Credentials = CredentialCache.DefaultNetworkCredentials,
                IgnoreResourceNotFoundException = true,
                IgnoreMissingProperties         = true,
                ResolveEntitySet = x => new Uri(parameters.Url),
                //ResolveName = x => entitySet,
                SaveChangesDefaultOptions = SaveChangesOptions.ContinueOnError
            };

            IDictionary <string, string> headers = parameters.Headers;

            if (headers != null && headers.Any())
            {
                dataServiceContext.SendingRequest2 += (sender, args) =>
                {
                    foreach (string key in headers.Keys)
                    {
                        args.RequestMessage.SetHeader(key, headers[key]);
                    }
                };
            }

            return(dataServiceContext);
        }
示例#3
0
        /// <summary>
        /// Executes the <see cref="DataGetRequest{TEntity}" />.
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override Message Execute(DataRequestParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (Query == null)
            {
                throw new ArgumentException("Could not execute request because no query has been set.");
            }

            DataServiceContext         serviceContext   = Resolve(parameters);
            DataServiceQuery <TEntity> query            = serviceContext.CreateQuery <TEntity>(parameters.EntitySet);
            IEnumerable <TEntity>      result           = Query(query);
            DataServiceQuery <TEntity> dataServiceQuery = result as DataServiceQuery <TEntity>;
            IEnumerable <TEntity>      entities         = dataServiceQuery != null?dataServiceQuery.Execute() : result;

            return(new DataResponse <IEnumerable <TEntity> > {
                Result = entities
            });
        }
示例#4
0
 /// <summary>
 /// Executes the <see cref="DataRequest" /> using the specified <see cref="DataRequestParameters" />.
 /// </summary>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public abstract Message Execute(DataRequestParameters parameters);