Example #1
0
 /// <summary>
 /// Get an existing business object.
 /// </summary>
 /// <param name="request">The request parameter object.</param>
 public byte[] Fetch(byte[] req)
 {
   var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
   Csla.Server.Hosts.WcfBfChannel.FetchRequest request;
   using (var buffer = new System.IO.MemoryStream(req))
   {
     request = (Csla.Server.Hosts.WcfBfChannel.FetchRequest)formatter.Deserialize(buffer);
   }
   Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
   object result;
   try
   {
     result = portal.Fetch(request.ObjectType, request.Criteria, request.Context);
   }
   catch (Exception ex)
   {
     result = ex;
   }
   var response = new WcfResponse(result);
   using (var buffer = new System.IO.MemoryStream())
   {
     formatter.Serialize(buffer, response);
     return buffer.ToArray();
   }
 }
Example #2
0
 public WcfResponse Update(UpdateRequest request)
 {
   Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
   object result;
   try
   {
     result = portal.Update(request.Object, request.Context, true).Result;
   }
   catch (Exception ex)
   {
     result = ex;
   }
   return new WcfResponse(result);
 }
Example #3
0
 public WcfResponse Fetch(FetchRequest request)
 {
   Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
   object result;
   try
   {
     result = portal.Fetch(request.ObjectType, request.Criteria, request.Context, true).Result;
   }
   catch (Exception ex)
   {
     result = ex;
   }
   return new WcfResponse(result);
 }
        public async Task<WcfResponse> Create(CreateRequest request)
        {
            Csla.Server.DataPortal portal = new Csla.Server.DataPortal();

            object result;

            try
            {
                result = await portal.Create(request.ObjectType, request.Criteria, request.Context, true);
            }
            catch (Exception ex)
            {
                result = ex;
            }

            return new WcfResponse(result);
        }
Example #5
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;
    }
Example #6
0
    public async Task<MobileResponse> Create(MobileCriteriaRequest request)
#endif
    {
      var serverDataPortal = new Csla.Server.DataPortal();
      var result = new MobileResponse();
      Type businessObjectType = null;
      object criteria = null;
      try
      {
        criteria = request.Criteria;
        // load type for business object
        businessObjectType = Type.GetType(request.TypeName);
        if (businessObjectType == null)
          throw new InvalidOperationException(
            string.Format(Csla.Properties.Resources.ObjectTypeCouldNotBeLoaded, request.TypeName));

        SetContext(request);

        serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create });
        serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, criteria, DataPortalOperations.Create));

        object newObject = null;
        var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
        if (factoryInfo == null)
        {
#if NET40
          if (criteria != null)
            newObject = Csla.DataPortal.Create(businessObjectType, criteria);
          else
            newObject = Csla.DataPortal.Create(businessObjectType, new EmptyCriteria());
#else
          if (criteria != null)
            newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "CreateAsync", new Type[] { businessObjectType }, true, criteria).ConfigureAwait(false);
          else
            newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "CreateAsync", new Type[] { businessObjectType }, false, null).ConfigureAwait(false);
#endif
        }
        else
        {
          if (string.IsNullOrEmpty(factoryInfo.CreateMethodName))
            throw new InvalidOperationException(Resources.CreateMethodNameNotSpecified);

          object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
#if NET40
          if (criteria != null)
            newObject = Csla.Reflection.MethodCaller.CallMethod(f, factoryInfo.CreateMethodName, criteria);
          else
            newObject = Csla.Reflection.MethodCaller.CallMethod(f, factoryInfo.CreateMethodName);
#else
          if (criteria != null)
            newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.CreateMethodName, criteria).ConfigureAwait(false);
          else
            newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.CreateMethodName).ConfigureAwait(false);
#endif
        }
        result.Object = newObject;
        result.GlobalContext = ApplicationContext.GlobalContext;
      }
      catch (Csla.Reflection.CallMethodException ex)
      {
        var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Create", ex);
        result.Error = inspected.InnerException;
      }
      catch (Exception ex)
      {
        var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Create", ex);
        result.Error = inspected;
      }
      finally
      {
        if (result.Error != null)
          serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
        else
          serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create, Result = new DataPortalResult(result.Object, result.GlobalContext) });
        ClearContext();
      }
      return result;
    }
Example #7
0
    public async Task<MobileResponse> Update(MobileUpdateRequest request)
#endif
    {
      var serverDataPortal = new Csla.Server.DataPortal();
      var result = new MobileResponse();
      Type businessObjectType = null;
      object obj = null;
      var operation = DataPortalOperations.Update;
      try
      {
        // unpack object
        obj = request.ObjectToUpdate;

        if (obj is Csla.Core.ICommandObject)
          operation = DataPortalOperations.Execute;

        // load type for business object
        businessObjectType = obj.GetType();

        SetContext(request);

        serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation });
        serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, obj, operation));

        object newObject = null;
        var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
        if (factoryInfo == null)
        {
#if NET40
          newObject = Csla.DataPortal.Update(obj);
#else
          newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "UpdateAsync", new Type[] { businessObjectType }, true, obj).ConfigureAwait(false);
#endif
        }
        else
        {
          if (string.IsNullOrEmpty(factoryInfo.UpdateMethodName))
            throw new InvalidOperationException(Resources.UpdateMethodNameNotSpecified);

          object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
#if NET40
          newObject = Csla.Reflection.MethodCaller.CallMethod(f, factoryInfo.UpdateMethodName, obj);
#else
          newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.UpdateMethodName, obj).ConfigureAwait(false);
#endif
        }
        result.Object = newObject;
        result.GlobalContext = ApplicationContext.GlobalContext;
      }
      catch (Csla.Reflection.CallMethodException ex)
      {
        var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, obj, "DataPortal.Update", ex);
        result.Error = inspected.InnerException;
      }
      catch (Exception ex)
      {
        var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, obj, "DataPortal.Update", ex);
        result.Error = inspected;
      }
      finally
      {
        if (result.Error != null)
          serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
        else
          serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation, Result = new DataPortalResult(result.Object, result.GlobalContext) });
        ClearContext();
      }
      return result;
    }
Example #8
0
    /// <summary>
    /// Update a business object.
    /// </summary>
    /// <param name="request">The request parameter object.</param>
#pragma warning disable 1998
    public async Task<HttpResponse> Update(UpdateRequest request)
#pragma warning restore 1998
    {
      var result = new HttpResponse();
      try
      {
        request = ConvertRequest(request);
        // unpack object
        object obj = GetCriteria(request.ObjectData);

        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.Update(obj, 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);
      }
      return ConvertResponse(result);
    }