示例#1
0
        /// <summary>
        /// Create and initialize an existing business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
#pragma warning disable 1998
        public async Task <HttpResponse> Create(CriteriaRequest request)
#pragma warning restore 1998
        {
            var result = new HttpResponse();

            try
            {
                request = ConvertRequest(request);

                // unpack criteria data into object
                object criteria = GetCriteria(request.CriteriaData);
                if (criteria is Csla.DataPortalClient.PrimitiveCriteria)
                {
                    criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
                }

                var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
                var context    = new DataPortalContext(
                    (IPrincipal)MobileFormatter.Deserialize(request.Principal),
                    true,
                    request.ClientCulture,
                    request.ClientUICulture,
                    (ContextDictionary)MobileFormatter.Deserialize(request.ClientContext),
                    (ContextDictionary)MobileFormatter.Deserialize(request.GlobalContext));

                var prtl = new Csla.Server.DataPortal();
                var dpr  = await prtl.Create(objectType, criteria, context, true);

                if (dpr.Error != null)
                {
                    result.ErrorData = new HttpErrorInfo(dpr.Error);
                }
                result.GlobalContext = MobileFormatter.Serialize(dpr.GlobalContext);
                result.ObjectData    = MobileFormatter.Serialize(dpr.ReturnObject);
            }
            catch (Exception ex)
            {
                result.ErrorData = new HttpErrorInfo(ex);
                throw;
            }
            finally
            {
                result = ConvertResponse(result);
            }
            return(result);
        }
示例#2
0
        /// <summary>
        /// Called by <see cref="DataPortal" /> to create a
        /// new business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
        public async virtual Task <DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            DataPortalResult result;

            try
            {
                var request = GetBaseCriteriaRequest();
                request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
                if (!(criteria is IMobileObject))
                {
                    criteria = new PrimitiveCriteria(criteria);
                }
                request.CriteriaData = SerializationFormatterFactory.GetFormatter().Serialize(criteria);
                request = ConvertRequest(request);
                var serialized = SerializationFormatterFactory.GetFormatter().Serialize(request);
                serialized = await CallDataPortalServer(serialized, "create", GetRoutingToken(objectType), isSync);

                var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter().Deserialize(serialized);
                response = ConvertResponse(response);

                var globalContext = (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(response.GlobalContext);
                if (response.ErrorData == null)
                {
                    var obj = SerializationFormatterFactory.GetFormatter().Deserialize(response.ObjectData);
                    result = new DataPortalResult(obj, null, globalContext);
                }
                else if (response.ErrorData != null)
                {
                    var ex = new DataPortalException(response.ErrorData);
                    result = new DataPortalResult(null, ex, globalContext);
                }
                else
                {
                    throw new DataPortalException("null response", null);
                }
            }
            catch (Exception ex)
            {
                result = new DataPortalResult(null, ex, null);
            }
            if (result.Error != null)
            {
                throw result.Error;
            }
            return(result);
        }
示例#3
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to delete a
    /// business object.
    /// </summary>
    /// <param name="objectType">Type of business object to create.</param>
    /// <param name="criteria">Criteria object describing business object.</param>
    /// <param name="context">
    /// <see cref="Server.DataPortalContext" /> object passed to the server.
    /// </param>
    /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
#pragma warning disable 1998
    public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
    {
      DataPortalResult result;
      try
      {

        var request = GetBaseCriteriaRequest();
        request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
        if (!(criteria is IMobileObject))
        {
          criteria = new PrimitiveCriteria(criteria);
        }
        request.CriteriaData = MobileFormatter.Serialize(criteria);
        request = ConvertRequest(request);

        var serialized = MobileFormatter.Serialize(request);

        serialized = await CallDataPortalServer(serialized, "delete", GetRoutingToken(objectType), isSync);

        var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)MobileFormatter.Deserialize(serialized);
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response != null && response.ErrorData == null)
        {
          result = new DataPortalResult(null, null, globalContext);
        }
        else if (response != null && response.ErrorData != null)
        {
          var ex = new DataPortalException(response.ErrorData);
          result = new DataPortalResult(null, ex, globalContext);
        }
        else
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
示例#4
0
        /// <summary>
        /// Get an existing business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
#pragma warning disable 1998
        public async Task <DataPortalResponse> Fetch(CriteriaRequest request)
#pragma warning restore 1998
        {
            var result = ApplicationContext.CreateInstanceDI <DataPortalResponse>();

            try
            {
                request = ConvertRequest(request);

                // unpack criteria data into object
                object criteria = GetCriteria(ApplicationContext, request.CriteriaData);
                if (criteria is Csla.DataPortalClient.PrimitiveCriteria)
                {
                    criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
                }

                var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
                var context    = new DataPortalContext(
                    ApplicationContext, (IPrincipal)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.Principal),
                    true,
                    request.ClientCulture,
                    request.ClientUICulture,
                    (ContextDictionary)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.ClientContext));

                var dpr = await dataPortalServer.Fetch(objectType, criteria, context, true);

                if (dpr.Error != null)
                {
                    result.ErrorData = ApplicationContext.CreateInstance <DataPortalErrorInfo>(ApplicationContext, dpr.Error);
                }
                result.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(dpr.ReturnObject);
            }
            catch (Exception ex)
            {
                result.ErrorData = ApplicationContext.CreateInstance <DataPortalErrorInfo>(ApplicationContext, ex);
                throw;
            }
            finally
            {
                result = ConvertResponse(result);
            }
            return(result);
        }
示例#5
0
        /// <summary>
        /// Called by <see cref="DataPortal" /> to delete a
        /// business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
#pragma warning disable 1998
        public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
        {
            DataPortalResult result = null;

            try
            {
                if (isSync)
                {
                    throw new NotSupportedException("isSync == true");
                }
                var client = GetClient();
                if (this.Timeout > 0)
                {
                    client.Timeout = TimeSpan.FromMilliseconds(this.Timeout);
                }
                var request = GetBaseCriteriaRequest();
                request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
                if (!(criteria is IMobileObject))
                {
                    criteria = new PrimitiveCriteria(criteria);
                }
                request.CriteriaData = MobileFormatter.Serialize(criteria);
                request = ConvertRequest(request);

                var serialized = MobileFormatter.Serialize(request);

                var httpRequest = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}?operation=delete", DataPortalUrl));
                httpRequest.Content = new ByteArrayContent(serialized);

                var httpResponse = await client.SendAsync(httpRequest);

                httpResponse.EnsureSuccessStatusCode();
                serialized = await httpResponse.Content.ReadAsByteArrayAsync();

                var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)MobileFormatter.Deserialize(serialized);
                response = ConvertResponse(response);
                var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
                if (response != null && response.ErrorData == null)
                {
                    result = new DataPortalResult(null, null, globalContext);
                }
                else if (response != null && response.ErrorData != null)
                {
                    var ex = new DataPortalException(response.ErrorData);
                    result = new DataPortalResult(null, ex, globalContext);
                }
                else
                {
                    throw new DataPortalException("null response", null);
                }
            }
            catch (Exception ex)
            {
                result = new DataPortalResult(null, ex, null);
            }
            if (result.Error != null)
            {
                throw result.Error;
            }
            return(result);
        }