/// <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> public DataPortalResult Create(Type objectType, object criteria, DataPortalContext context) { ChannelFactory<IWcfBfPortal> cf = GetChannelFactory(); IWcfBfPortal svr = GetProxy(cf); WcfResponse response = null; try { var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); byte[] outbound; byte[] inbound; using (var buffer = new System.IO.MemoryStream()) { formatter.Serialize(buffer, new CreateRequest(objectType, criteria, context)); outbound = buffer.ToArray(); } inbound = svr.Create(outbound); using (var buffer = new System.IO.MemoryStream(inbound)) { response = (WcfResponse)formatter.Deserialize(buffer); } if (cf != null) cf.Close(); } catch { cf.Abort(); throw; } object result = response.Result; if (result is Exception) throw (Exception)result; return (DataPortalResult)result; }
public WcfResponse Fetch(CriteriaRequest request) { Csla.Server.DataPortal portal = new Csla.Server.DataPortal(); Exception error = null; DataPortalResult result = null; WcfResponse response = null; ISerializationFormatter formatter = SerializationFormatterFactory.GetFormatter(); try { request = ConvertRequest(request); DataPortalContext context = new DataPortalContext( formatter.Deserialize(request.Principal) as IPrincipal, true, request.ClientCulture, request.ClientUICulture, formatter.Deserialize(request.ClientContext) as ContextDictionary, formatter.Deserialize(request.GlobalContext) as ContextDictionary); result = portal.Fetch(Type.GetType(request.TypeName), formatter.Deserialize(request.CriteriaData), context); response = new WcfResponse( formatter.Serialize(result.ReturnObject), formatter.Serialize(error), formatter.Serialize(Csla.ApplicationContext.GlobalContext)); } catch (Exception ex) { error = ex; response = new WcfResponse( null, formatter.Serialize(error), formatter.Serialize(Csla.ApplicationContext.GlobalContext)); } return ConvertResponse(response); }
/// <summary> /// Called by <see cref="DataPortal" /> to load an /// existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</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 Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { if (isSync || Csla.ApplicationContext.LogicalExecutionLocation == ApplicationContext.LogicalExecutionLocations.Server) { return await _portal.Fetch(objectType, criteria, context, isSync); } else { var tcs = new TaskCompletionSource<DataPortalResult>(); var bw = new Csla.Threading.BackgroundWorker(); bw.DoWork += (s, o) => { o.Result = _portal.Fetch(objectType, criteria, context, isSync).Result; }; bw.RunWorkerCompleted += (s, o) => { if (o.Error == null) tcs.TrySetResult((DataPortalResult)o.Result); else tcs.TrySetException(o.Error); }; bw.RunWorkerAsync(); return await tcs.Task; } }
public static void Delete(object criteria) { Server.DataPortalResult result; MethodInfo method = MethodCaller.GetMethod( MethodCaller.GetObjectType(criteria), "DataPortal_Delete", criteria); DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(RunLocal(method)); Server.DataPortalContext dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); OnDataPortalInvoke(new DataPortalEventArgs(dpContext)); try { result = proxy.Delete(criteria, dpContext); } catch (Server.DataPortalException ex) { result = ex.Result; if (proxy.IsServerRemote) { ApplicationContext.SetGlobalContext(result.GlobalContext); } throw new DataPortalException("DataPortal.Delete " + Resources.Failed, ex.InnerException, result.ReturnObject); } if (proxy.IsServerRemote) { ApplicationContext.SetGlobalContext(result.GlobalContext); } OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext)); }
public async Task<DataPortalResult> Create( Type objectType, object criteria, DataPortalContext context, bool isSync) { LateBoundObject obj = null; IDataPortalTarget target = null; var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Create); try { // create an instance of the business object. obj = new LateBoundObject(objectType); target = obj.Instance as IDataPortalTarget; if (target != null) { target.DataPortal_OnDataPortalInvoke(eventArgs); target.MarkNew(); } else { obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs); obj.CallMethodIfImplemented("MarkNew"); } // tell the business object to create its data if (criteria is EmptyCriteria) await obj.CallMethodTryAsync("DataPortal_Create"); else await obj.CallMethodTryAsync("DataPortal_Create", criteria); if (target != null) target.DataPortal_OnDataPortalInvokeComplete(eventArgs); else obj.CallMethodIfImplemented( "DataPortal_OnDataPortalInvokeComplete", eventArgs); // return the populated business object as a result return new DataPortalResult(obj.Instance); } catch (Exception ex) { try { if (target != null) target.DataPortal_OnDataPortalException(eventArgs, ex); else if (obj != null) obj.CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex); } catch { // ignore exceptions from the exception handler } object outval = null; if (obj != null) outval = obj.Instance; throw DataPortal.NewDataPortalException( "DataPortal.Create " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, outval, criteria, "DataPortal.Create", ex), outval); } }
/// <summary> /// Get an existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</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 Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { try { context.FactoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (context.FactoryInfo == null) { var dp = new SimpleDataPortal(); return await dp.Fetch(objectType, criteria, context, isSync); } else { var dp = new FactoryDataPortal(); return await dp.Fetch(objectType, criteria, context, isSync); } } catch (DataPortalException) { throw; } catch (Exception ex) { throw DataPortal.NewDataPortalException( "DataPortal.Fetch " + Resources.FailedOnServer, ex, null); } }
/// <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> public DataPortalResult Create(Type objectType, object criteria, DataPortalContext context) { ChannelFactory<IExtendableWcfPortalForDotNet> cf = GetChannelFactory(); IExtendableWcfPortalForDotNet svr = GetProxy(cf); WcfResponse response = null; ISerializationFormatter formatter = SerializationFormatterFactory.GetFormatter(); try { response = svr.Create(GetRequest(formatter, objectType, criteria, context)); if (cf != null) cf.Close(); } catch { cf.Abort(); throw; } DataPortalResult result = GetResult(formatter, response); Exception error = (Exception)formatter.Deserialize(response.Error); if (error != null) { throw error; } return result; }
private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, DataPortalContext context, bool isSync) { object factory = FactoryLoader.GetFactory(factoryTypeName); var eventArgs = new DataPortalEventArgs(context, objectType, null, operation); Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs); object result = null; try { Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, factory, methodName); result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName).ConfigureAwait(false); var error = result as Exception; if (error != null) throw error; var busy = result as Csla.Core.ITrackStatus; if (busy != null && busy.IsBusy) throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name)); Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs); } catch (Exception ex) { Csla.Reflection.MethodCaller.CallMethodIfImplemented( factory, "InvokeError", new DataPortalEventArgs(context, objectType, null, operation, ex)); throw; } return new DataPortalResult(result); }
public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { //Security.BusinessPrincipal //var temp = new DataPortalContext(new UnauthenticatedPrincipal(), context.IsRemotePortal); var task = new UpdateTask { Obj = obj, Context = context }; if (isSync) { DoUpdate(task); } else { await Task.Factory.StartNew(DoUpdate, task); } if (task.ResultException != null) { throw task.ResultException; } return(task.Result); }
/// <summary> /// Creates an instance of the type. /// </summary> /// <param name="dataPortalContext"> /// Data portal context object. /// </param> /// <param name="objectType"> /// Business object type. /// </param> /// <param name="obj"> /// Criteria or business object for request. /// </param> /// <param name="operation"> /// Data portal operation being performed. /// </param> public DataPortalEventArgs(Server.DataPortalContext dataPortalContext, Type objectType, object obj, DataPortalOperations operation) { _dataPortalContext = dataPortalContext; _operation = operation; _objectType = objectType; _object = obj; }
/// <summary> /// Creates an instance of the object. /// </summary> /// <param name="dataPortalContext"> /// Data portal context object. /// </param> /// <param name="objectType"> /// Business object type. /// </param> /// <param name="obj"> /// Criteria or business object for request. /// </param> /// <param name="operation"> /// Data portal operation being performed. /// </param> public DataPortalEventArgs(Server.DataPortalContext dataPortalContext, Type objectType, object obj, DataPortalOperations operation) { _dataPortalContext = dataPortalContext; _operation = operation; _objectType = objectType; _object = obj; }
internal async Task DoDeleteAsync(Type objectType, object criteria, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; try { DataPortal.OnDataPortalInitInvoke(null); if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, objectType, Server.DataPortal.GetCriteriaArray(criteria))) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } var method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod <DeleteAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria), false); var proxy = GetDataPortalProxy(objectType, method); dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete)); try { result = await proxy.Delete(objectType, criteria, dpContext, isSync); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { var dpe = ex.InnerExceptions[0] as Server.DataPortalException; if (dpe != null) { HandleDeleteDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Delete {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleDeleteDataPortalException(ex, isSync, proxy); } GlobalContext = result.GlobalContext; if (proxy.IsServerRemote && isSync) { ApplicationContext.ContextManager.SetGlobalContext(result.GlobalContext); } DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete)); } catch (Exception ex) { DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete, ex)); throw; } }
public async Task <DataPortalResult> Update(object obj, Csla.Server.DataPortalContext context, bool isSync) { //evict cache items based on ObjectCacheEvictionAttribute RemoveCacheItems(obj.GetType()); proxy = GetDataPortalProxy(); return(await proxy.Update(obj, context, isSync)); }
public Csla.Server.DataPortalResult Delete(Type objectType, object criteria, Csla.Server.DataPortalContext context) { //evict cache items based on ObjectCacheEvictionAttribute RemoveCacheItems(objectType); proxy = GetDataPortalProxy(); return(proxy.Delete(objectType, criteria, context)); }
public Csla.Server.DataPortalResult Update(object obj, Csla.Server.DataPortalContext context) { //evict cache items based on ObjectCacheEvictionAttribute RemoveCacheItems(obj.GetType()); proxy = GetDataPortalProxy(); return(proxy.Update(obj, context)); }
/// <summary> /// Called by a factory method in a business class to create /// a new object, which is loaded with default /// values from the database. /// </summary> /// <param name="objectType">Type of business object to create.</param> /// <param name="criteria">Object-specific criteria.</param> /// <returns>A new object, populated with default values.</returns> public static object Create(Type objectType, object criteria) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; try { OnDataPortalInitInvoke(null); if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, objectType)) { throw new System.Security.SecurityException(string.Format( Resources.UserNotAuthorizedException, "create", objectType.Name)); } var method = Server.DataPortalMethodCache.GetCreateMethod(objectType, criteria); DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(objectType, method.RunLocal); dpContext = new Csla.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create)); try { result = proxy.Create(objectType, criteria, dpContext); } catch (Server.DataPortalException ex) { result = ex.Result; if (proxy.IsServerRemote) { ApplicationContext.ContextManager.SetGlobalContext(result.GlobalContext); } throw new DataPortalException( string.Format("DataPortal.Create {0} ({1})", Resources.Failed, ex.InnerException.InnerException), ex.InnerException, result.ReturnObject); } if (proxy.IsServerRemote) { ApplicationContext.ContextManager.SetGlobalContext(result.GlobalContext); } OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create)); } catch (Exception ex) { OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, DataPortalOperations.Create, ex)); throw; } return(result.ReturnObject); }
private async Task <object> DoCreateAsync(Type objectType, object criteria, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; try { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.CreateObject, objectType, Server.DataPortal.GetCriteriaArray(criteria))) { throw new Csla.Security.SecurityException(string.Format( Resources.UserNotAuthorizedException, "create", objectType.Name)); } Reflection.ServiceProviderMethodInfo method; if (criteria is Server.EmptyCriteria) { method = ServiceProviderMethodCaller.FindDataPortalMethod <CreateAttribute>(objectType, null, false); } else { method = ServiceProviderMethodCaller.FindDataPortalMethod <CreateAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria), false); } var proxy = GetDataPortalProxy(method); dpContext = new Csla.Server.DataPortalContext(ApplicationContext, GetPrincipal(), proxy.IsServerRemote); try { result = await proxy.Create(objectType, criteria, dpContext, isSync); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { if (ex.InnerExceptions[0] is Server.DataPortalException dpe) { HandleCreateDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Create {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleCreateDataPortalException(ex, isSync, proxy); } } catch { throw; } return(result.ReturnObject); }
public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context) { Thread t = new Thread(DoFetch); FetchTask task = new FetchTask(); task.ObjectType = objectType; task.Criteria = criteria; task.Context = context; t.Start(task); t.Join(); return task.Result; }
/// <summary> /// Called by <see cref="DataPortal" /> to update a /// business object. /// </summary> /// <param name="obj">The business object to update.</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 Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { if (isSync || Csla.ApplicationContext.LogicalExecutionLocation == ApplicationContext.LogicalExecutionLocations.Server) { return await _portal.Update(obj, context, isSync); } else { return await await _taskFactory.StartNew(() => this._portal.Update(obj, context, isSync)); } }
/// <summary> /// Called by <see cref="DataPortal" /> to load an /// existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</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 Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { if (isSync || Csla.ApplicationContext.LogicalExecutionLocation == ApplicationContext.LogicalExecutionLocations.Server) { return await _portal.Fetch(objectType, criteria, context, isSync); } else { return await await _taskFactory.StartNew(() => this._portal.Fetch(objectType, criteria, context, isSync)); } }
/// <summary> /// Wraps a Create call in a TransactionScope /// </summary> /// <remarks> /// This method delegates to /// <see cref="SimpleDataPortal">SimpleDataPortal</see> /// but wraps that call within a /// <see cref="TransactionScope">TransactionScope</see> /// to provide transactional support via /// System.Transactions. /// </remarks> /// <param name="objectType">A <see cref="Type">Type</see> object /// indicating the type of business object to be created.</param> /// <param name="criteria">A custom criteria object providing any /// extra information that may be required to properly create /// the object.</param> /// <param name="context">Context data from the client.</param> /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param> /// <returns>A populated business object.</returns> public async Task<DataPortalResult> Create( Type objectType, object criteria, DataPortalContext context, bool isSync) { DataPortalResult result; using (TransactionScope tr = CreateTransactionScope()) { var portal = new DataPortalSelector(); result = await portal.Create(objectType, criteria, context, isSync); tr.Complete(); } return result; }
public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { var cachingAttribute = ObjectCacheAttribute.GetObjectCacheAttribute(objectType); var cacheProvider = CacheManager.GetCacheProvider(); if (cachingAttribute != null && cacheProvider != null) { //get scenario details var scope = cachingAttribute.Scope; var cacheByCriteria = cachingAttribute.CacheByCriteria; var expiration = cachingAttribute.Expiration; //get key var key = GetCacheItemKey(objectType, cachingAttribute); //include criteria hash if needed if (cachingAttribute.CacheByCriteria) key = string.Format("{0}::{1}", key, criteria.GetHashCode()); var data = cacheProvider.Get(key); if (data == null) { //cache miss proxy = GetDataPortalProxy(); var results = await proxy.Fetch(objectType, criteria, context, isSync); if (expiration > 0) cacheProvider.Put(key, results.ReturnObject, new TimeSpan(0, expiration, 0)); else cacheProvider.Put(key, results.ReturnObject); return results; } else { //cache hit #if !NET45 await TaskEx.Delay(0); #else await Task.Delay(0); #endif return new DataPortalResult(data); } } else { proxy = GetDataPortalProxy(); return await proxy.Fetch(objectType, criteria, context, isSync); } }
public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context) { var cachingAttribute = ObjectCacheAttribute.GetObjectCacheAttribute(objectType); var cacheProvider = CacheManager.GetCacheProvider(); if (cachingAttribute != null && cacheProvider != null) { //get scenario details var scope = cachingAttribute.Scope; var cacheByCriteria = cachingAttribute.CacheByCriteria; var expiration = cachingAttribute.Expiration; //get key var key = GetCacheItemKey(objectType, cachingAttribute); //include criteria hash if needed if (cachingAttribute.CacheByCriteria) key = string.Format("{0}::{1}", key, criteria.GetHashCode()); var data = cacheProvider.Get(key); if (data == null) { //cache miss proxy = GetDataPortalProxy(); var results = proxy.Fetch(objectType, criteria, context); if (expiration > 0) cacheProvider.Put(key, results, new TimeSpan(0, expiration, 0)); else cacheProvider.Put(key, results); return results; } else { //cache hit return (DataPortalResult)data; } } else { proxy = GetDataPortalProxy(); return proxy.Fetch(objectType, criteria, context); } }
private async Task <DataPortalResult> Execute(DataPortalTarget obj, DataPortalContext context, bool isSync) { DataPortalOperations operation = DataPortalOperations.Execute; Type objectType = obj.Instance.GetType(); var eventArgs = new DataPortalEventArgs(context, objectType, obj, operation); try { ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance); obj.OnDataPortalInvoke(eventArgs); await obj.ExecuteAsync(isSync); obj.ThrowIfBusy(); obj.OnDataPortalInvokeComplete(eventArgs); return(new DataPortalResult(obj.Instance)); } catch (Exception ex) { try { obj.OnDataPortalException(eventArgs, ex); } catch { // ignore exceptions from the exception handler } throw DataPortal.NewDataPortalException( "DataPortal.Execute " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Execute", ex), obj); } finally { object reference = null; if (obj != null) { reference = obj.Instance; } ApplicationContext.DataPortalActivator.FinalizeInstance(reference); } }
private static object Fetch(Type objectType, object criteria) { Server.DataPortalResult result; MethodInfo method = MethodCaller.GetFetchMethod(objectType, criteria); DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(RunLocal(method)); Server.DataPortalContext dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); OnDataPortalInvoke(new DataPortalEventArgs(dpContext)); try { result = proxy.Fetch(objectType, criteria, dpContext); } catch (Server.DataPortalException ex) { result = ex.Result; if (proxy.IsServerRemote) { ApplicationContext.SetGlobalContext(result.GlobalContext); } throw new DataPortalException( String.Format("DataPortal.Fetch {0} ({1})", Resources.Failed, ex.InnerException.InnerException), ex.InnerException, result.ReturnObject); } if (proxy.IsServerRemote) { ApplicationContext.SetGlobalContext(result.GlobalContext); } OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext)); return(result.ReturnObject); }
private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, object e, DataPortalContext context) { object factory = FactoryLoader.GetFactory(factoryTypeName); var eventArgs = new DataPortalEventArgs(context, objectType, e, operation); Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs); object result = null; try { result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName, e); var error = result as Exception; if (error != null) throw error; Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs); } catch (Exception ex) { Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeError", new DataPortalEventArgs(context, objectType, e, operation, ex)); throw; } return new DataPortalResult(result); }
/// <summary> /// Called by the client-side 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">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 Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync) { DataPortalResult result = null; if (isSync) throw new NotSupportedException("isSync == true"); try { if (!(criteria is IMobileObject)) criteria = new PrimitiveCriteria(criteria); var contextData = Csla.Serialization.Mobile.MobileFormatter.Serialize(context); var criteriaData = Csla.Serialization.Mobile.MobileFormatter.Serialize(criteria); var portal = new BrokeredHost(); var objectTypeName = objectType.AssemblyQualifiedName; var resultData = await portal.Create(objectTypeName, criteriaData, contextData); var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)Csla.Serialization.Mobile.MobileFormatter.Deserialize(resultData.ToArray()); var globalContext = (Csla.Core.ContextDictionary)Csla.Serialization.Mobile.MobileFormatter.Deserialize(response.GlobalContext); if (response != null && response.ErrorData == null) { var obj = MobileFormatter.Deserialize(response.ObjectData); result = new DataPortalResult(obj, 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; }
/// <summary> /// Get an existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</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 Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { try { DataPortalResult result; if (criteria is EmptyCriteria) { result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, context, isSync).ConfigureAwait(false); } else { result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, criteria, context, isSync).ConfigureAwait(false); } return(result); } catch (Exception ex) { throw DataPortal.NewDataPortalException( ApplicationContext, context.FactoryInfo.FetchMethodName + " " + Resources.FailedOnServer, new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, criteria, context.FactoryInfo.FetchMethodName, ex), null); } }
/// <summary> /// Get an existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</param> /// <param name="criteria">Criteria object describing business object.</param> /// <param name="context"> /// <see cref="Server.DataPortalContext" /> object passed to the server. /// </param> public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context) { try { DataPortalResult result = null; if (criteria is EmptyCriteria) { result = InvokeMethod(context.FactoryInfo.FactoryTypeName, context.FactoryInfo.FetchMethodName, context); } else { result = InvokeMethod(context.FactoryInfo.FactoryTypeName, context.FactoryInfo.FetchMethodName, criteria, context); } return(result); } catch (Exception ex) { throw new DataPortalException( context.FactoryInfo.FetchMethodName + " " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, criteria, context.FactoryInfo.FetchMethodName, ex), new DataPortalResult()); } }
public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { var task = new UpdateTask { Obj = obj, Context = context }; if (isSync) { DoUpdate(task); } else { await Task.Factory.StartNew(DoUpdate, task); } if (task.ResultException != null) { throw task.ResultException; } return(task.Result); }
public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { var task = new FetchTask { ObjectType = objectType, Criteria = criteria, Context = context }; if (isSync) { DoFetch(task); } else { await Task.Factory.StartNew(DoFetch, task); } if (task.ResultException != null) { throw task.ResultException; } return(task.Result); }
public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { var portal = new DataPortalSelector(); return(await portal.Fetch(objectType, criteria, context, isSync)); }
public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { var portal = new DataPortalSelector(); return(await portal.Update(obj, context, isSync)); }
public async Task <DataPortalResult> Delete(Type objectType, object criteria, Csla.Server.DataPortalContext context, bool isSync) { //evict cache items based on ObjectCacheEvictionAttribute RemoveCacheItems(objectType); proxy = GetDataPortalProxy(); return(await proxy.Delete(objectType, criteria, context, isSync)); }
private void SetContext(DataPortalContext context) { _oldLocation = Csla.ApplicationContext.LogicalExecutionLocation; ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server); // if the dataportal is not remote then // do nothing if (!context.IsRemotePortal) return; // set the context value so everyone knows the // code is running on the server ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server); // set the app context to the value we got from the // client ApplicationContext.SetContext(context.ClientContext, context.GlobalContext); // set the thread's culture to match the client #if NETFX_CORE var list = new System.Collections.ObjectModel.ReadOnlyCollection<string>(new List<string> { context.ClientUICulture }); Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; list = new System.Collections.ObjectModel.ReadOnlyCollection<string>(new List<string> { context.ClientCulture }); Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; #else System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(context.ClientCulture); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(context.ClientUICulture); #endif if (ApplicationContext.AuthenticationType == "Windows") { // When using integrated security, Principal must be null if (context.Principal != null) { Csla.Security.SecurityException ex = new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException); //ex.Action = System.Security.Permissions.SecurityAction.Deny; throw ex; } #if !SILVERLIGHT && !NETFX_CORE // Set .NET to use integrated security AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); #endif } else { // We expect the some Principal object if (context.Principal == null) { Csla.Security.SecurityException ex = new Csla.Security.SecurityException( Resources.BusinessPrincipalException + " Nothing"); //ex.Action = System.Security.Permissions.SecurityAction.Deny; throw ex; } ApplicationContext.User = context.Principal; } }
public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync) { LateBoundObject obj = null; IDataPortalTarget target = null; var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Delete); try { // create an instance of the business objet obj = new LateBoundObject(ApplicationContext.DataPortalActivator.CreateInstance(objectType)); ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance); target = obj.Instance as IDataPortalTarget; if (target != null) { target.DataPortal_OnDataPortalInvoke(eventArgs); } else { obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs); } // tell the business object to delete itself await obj.CallMethodTryAsync("DataPortal_Delete", criteria).ConfigureAwait(false); var busy = obj.Instance as Csla.Core.ITrackStatus; if (busy != null && busy.IsBusy) { throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name)); } if (target != null) { target.DataPortal_OnDataPortalInvokeComplete(eventArgs); } else { obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvokeComplete", eventArgs); } return(new DataPortalResult()); } catch (Exception ex) { try { if (target != null) { target.DataPortal_OnDataPortalException(eventArgs, ex); } else if (obj != null) { obj.CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex); } } catch { // ignore exceptions from the exception handler } throw DataPortal.NewDataPortalException( "DataPortal.Delete " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, obj, null, "DataPortal.Delete", ex), null); } }
/// <summary> /// Called by <see cref="DataPortal" /> to update a /// business object. /// </summary> /// <param name="obj">The business object to update.</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> Update(object obj, DataPortalContext context, bool isSync) #pragma warning restore 1998 { var request = GetBaseUpdateCriteriaRequest(); request.ObjectData = MobileFormatter.Serialize(obj); request = ConvertRequest(request); var proxy = GetProxy(); DataPortalResult result = null; try { var response = await proxy.UpdateAsync(request); response = ConvertResponse(response); var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext); if (response != null && response.ErrorData == null) { var newobj = MobileFormatter.Deserialize(response.ObjectData); result = new DataPortalResult(newobj, 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; }
public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { Type objectType = null; DataPortalOperations operation = DataPortalOperations.Update; try { SetContext(context); objectType = obj.GetType(); if (obj is Core.ICommandObject) { operation = DataPortalOperations.Execute; } Initialize(new InterceptArgs { ObjectType = objectType, Parameter = obj, Operation = operation, IsSync = isSync }); AuthorizeRequest(new AuthorizeRequest(objectType, obj, operation)); DataPortalResult result; DataPortalMethodInfo method; var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (factoryInfo != null) { string methodName; var factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName); var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { methodName = factoryInfo.DeleteMethodName; } else { methodName = factoryInfo.UpdateMethodName; } } else if (obj is Core.ICommandObject) { methodName = factoryInfo.ExecuteMethodName; } else { methodName = factoryInfo.UpdateMethodName; } method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, new object[] { obj }); } else { string methodName; var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { methodName = "DataPortal_DeleteSelf"; } else if (bbase.IsNew) { methodName = "DataPortal_Insert"; } else { methodName = "DataPortal_Update"; } } else if (obj is Core.ICommandObject) { methodName = "DataPortal_Execute"; } else { methodName = "DataPortal_Update"; } method = DataPortalMethodCache.GetMethodInfo(obj.GetType(), methodName); } #if !(ANDROID || IOS) && !NETFX_CORE context.TransactionalType = method.TransactionalAttribute.TransactionType; #else context.TransactionalType = method.TransactionalType; #endif IDataPortalServer portal; #if !(ANDROID || IOS) && !NETFX_CORE switch (method.TransactionalAttribute.TransactionType) { #if !MONO case TransactionalTypes.EnterpriseServices: portal = GetServicedComponentPortal(method.TransactionalAttribute); try { result = await portal.Update(obj, context, isSync).ConfigureAwait(false); } finally { ((System.EnterpriseServices.ServicedComponent)portal).Dispose(); } break; #endif case TransactionalTypes.TransactionScope: portal = new TransactionalDataPortal(method.TransactionalAttribute); result = await portal.Update(obj, context, isSync).ConfigureAwait(false); break; default: portal = new DataPortalBroker(); result = await portal.Update(obj, context, isSync).ConfigureAwait(false); break; } #else portal = new DataPortalBroker(); result = await portal.Update(obj, context, isSync).ConfigureAwait(false); #endif Complete(new InterceptArgs { ObjectType = objectType, Parameter = obj, Result = result, Operation = operation, IsSync = isSync }); return(result); } catch (Csla.Server.DataPortalException ex) { Complete(new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = ex, Operation = operation, IsSync = isSync }); throw; } catch (AggregateException ex) { Exception error = null; if (ex.InnerExceptions.Count > 0) { error = ex.InnerExceptions[0].InnerException; } else { error = ex; } var fex = DataPortal.NewDataPortalException( "DataPortal.Update " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", error), obj); Complete(new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync }); throw fex; } catch (Exception ex) { var fex = DataPortal.NewDataPortalException( "DataPortal.Update " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex), obj); Complete(new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync }); throw fex; } finally { ClearContext(context); } }
public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { var portal = new DataPortalSelector(); return await portal.Update(obj, context, isSync).ConfigureAwait(false); }
private CriteriaRequest GetRequest(ISerializationFormatter formatter, Type objectType, object criteria, DataPortalContext context) { CriteriaRequest request = new CriteriaRequest(); request.ClientContext = formatter.Serialize(Csla.ApplicationContext.ClientContext); request.ClientCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name; request.ClientUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name; request.CriteriaData = formatter.Serialize(criteria); request.GlobalContext = formatter.Serialize(Csla.ApplicationContext.GlobalContext); request.Principal = formatter.Serialize(Csla.ApplicationContext.User); request.TypeName = objectType.AssemblyQualifiedName; request = ConvertRequest(request); return request; }
public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { var portal = new DataPortalBroker(); return(await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false)); }
private async Task <object> DoCreateAsync(Type objectType, object criteria, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; try { DataPortal.OnDataPortalInitInvoke(null); if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, objectType)) { throw new Csla.Security.SecurityException(string.Format( Resources.UserNotAuthorizedException, "create", objectType.Name)); } var method = Server.DataPortalMethodCache.GetCreateMethod(objectType, criteria); DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(objectType, method.RunLocal); dpContext = new Csla.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create)); try { result = await proxy.Create(objectType, criteria, dpContext, isSync); GlobalContext = result.GlobalContext; if (isSync && proxy.IsServerRemote) { ApplicationContext.ContextManager.SetGlobalContext(GlobalContext); } } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { var dpe = ex.InnerExceptions[0] as Server.DataPortalException; if (dpe != null) { HandleCreateDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Create {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleCreateDataPortalException(ex, isSync, proxy); } DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create)); } catch (Exception ex) { DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create, ex)); throw; } return(result.ReturnObject); }
internal async Task <T> DoUpdateAsync(T obj, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; DataPortalOperations operation = DataPortalOperations.Update; Type objectType = obj.GetType(); try { DataPortal.OnDataPortalInitInvoke(null); Csla.Server.DataPortalMethodInfo method = null; var factoryInfo = Csla.Server.ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (factoryInfo != null) { var factoryType = Csla.Server.FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName); if (obj is Core.ICommandObject) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.ExecuteMethodName, new object[] { obj }); } } else { var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.DeleteMethodName, new object[] { obj }); } } // must check the same authorization rules as for DataPortal_XYZ methods else if (bbase.IsNew) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } else { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } else { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } if (method == null) { method = new Csla.Server.DataPortalMethodInfo(); } } else { string methodName; if (obj is Core.ICommandObject) { methodName = "DataPortal_Execute"; operation = DataPortalOperations.Execute; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } } else { var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { methodName = "DataPortal_DeleteSelf"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } } else if (bbase.IsNew) { methodName = "DataPortal_Insert"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } } else { methodName = "DataPortal_Update"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } } } else { methodName = "DataPortal_Update"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } } } method = Server.DataPortalMethodCache.GetMethodInfo(obj.GetType(), methodName); } DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(objectType, method.RunLocal); dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, obj, operation)); try { if (!proxy.IsServerRemote && ApplicationContext.AutoCloneOnUpdate) { // when using local data portal, automatically // clone original object before saving ICloneable cloneable = obj as ICloneable; if (cloneable != null) { obj = (T)cloneable.Clone(); } } result = await proxy.Update(obj, dpContext, isSync); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { var dpe = ex.InnerExceptions[0] as Server.DataPortalException; if (dpe != null) { HandleUpdateDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Update {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleUpdateDataPortalException(ex, isSync, proxy); } GlobalContext = result.GlobalContext; if (proxy.IsServerRemote && isSync) { ApplicationContext.ContextManager.SetGlobalContext(GlobalContext); } DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation)); } catch (Exception ex) { DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation, ex)); throw; } return((T)result.ReturnObject); }
/// <summary> /// 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 Task <DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync) { try { context.FactoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (context.FactoryInfo == null) { var dp = new SimpleDataPortal(); return(await dp.Create(objectType, criteria, context, isSync).ConfigureAwait(false)); } else { var dp = new FactoryDataPortal(); return(await dp.Create(objectType, criteria, context, isSync).ConfigureAwait(false)); } } catch (DataPortalException) { throw; } catch (Exception ex) { throw DataPortal.NewDataPortalException( "DataPortal.Create " + Resources.FailedOnServer, ex, null); } }
public DataPortalResult Update(object obj, DataPortalContext context) { Thread t = new Thread(DoUpdate); UpdateTask task = new UpdateTask(); task.Obj = obj; task.Context = context; t.Start(task); t.Join(); return task.Result; }
private void SetContext(DataPortalContext context) { _oldLocation = Csla.ApplicationContext.LogicalExecutionLocation; ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server); // if the dataportal is not remote then // do nothing if (!context.IsRemotePortal) { return; } // set the context value so everyone knows the // code is running on the server ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server); // set the app context to the value we got from the // client ApplicationContext.SetContext(context.ClientContext, context.GlobalContext); // set the thread's culture to match the client #if !PCL46 // rely on NuGet bait-and-switch for actual implementation #if NETCORE System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo(context.ClientCulture); System.Globalization.CultureInfo.CurrentUICulture = new System.Globalization.CultureInfo(context.ClientUICulture); #elif NETFX_CORE var list = new System.Collections.ObjectModel.ReadOnlyCollection <string>(new List <string> { context.ClientUICulture }); Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; list = new System.Collections.ObjectModel.ReadOnlyCollection <string>(new List <string> { context.ClientCulture }); Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages = list; #else System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(context.ClientCulture); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(context.ClientUICulture); #endif #endif if (ApplicationContext.AuthenticationType == "Windows") { // When using integrated security, Principal must be null if (context.Principal != null) { Csla.Security.SecurityException ex = new Csla.Security.SecurityException(Resources.NoPrincipalAllowedException); //ex.Action = System.Security.Permissions.SecurityAction.Deny; throw ex; } #if !(ANDROID || IOS) && !NETFX_CORE // Set .NET to use integrated security AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); #endif } else { // We expect the some Principal object if (context.Principal == null) { Csla.Security.SecurityException ex = new Csla.Security.SecurityException( Resources.BusinessPrincipalException + " Nothing"); //ex.Action = System.Security.Permissions.SecurityAction.Deny; throw ex; } ApplicationContext.User = context.Principal; } }
public DataPortalResult Delete(Type objectType, object criteria, DataPortalContext context) { Thread t = new Thread(DoDelete); DeleteTask task = new DeleteTask(); task.ObjectType = objectType; task.Criteria = criteria; task.Context = context; t.Start(task); t.Join(); return task.Result; }
public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { var portal = new DataPortalSelector(); return await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false); }
/// <summary> /// Called by the client-side DataPortal to delete an object. /// </summary> /// <remarks> /// This method delegates to /// <see cref="SimpleDataPortal">SimpleDataPortal</see> /// but wraps that call within a /// <see cref="TransactionScope">TransactionScope</see> /// to provide transactional support via /// System.Transactions. /// </remarks> /// <param name="objectType">Type of business object to create.</param> /// <param name="criteria">Object-specific criteria.</param> /// <param name="context">Context data from the client.</param> /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param> public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync) { DataPortalResult result; using (TransactionScope tr = CreateTransactionScope()) { var portal = new DataPortalBroker(); result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false); tr.Complete(); } return(result); }
public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync) { var task = new CreateTask{ ObjectType = objectType, Criteria = criteria, Context = context }; if (isSync) DoCreate(task); else await Task.Factory.StartNew(DoCreate, task); if (task.ResultException != null) throw task.ResultException; return task.Result; }
private UpdateRequest GetRequest(ISerializationFormatter formatter, object obj, DataPortalContext context) { UpdateRequest request = new UpdateRequest(); request.ClientContext = formatter.Serialize(Csla.ApplicationContext.ClientContext); request.ClientCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name; request.ClientUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name; request.GlobalContext = formatter.Serialize(Csla.ApplicationContext.GlobalContext); request.Principal = formatter.Serialize(Csla.ApplicationContext.User); request.ObjectData = formatter.Serialize(obj); request = ConvertRequest(request); return request; }
/// <summary> /// Called by <see cref="DataPortal" /> to update a /// business object. /// </summary> /// <param name="obj">The business object to update.</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> Update(object obj, 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 = GetBaseUpdateCriteriaRequest(); request.ObjectData = MobileFormatter.Serialize(obj); request = ConvertRequest(request); var serialized = Csla.Serialization.Mobile.MobileFormatter.Serialize(request); var httpRequest = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}?operation=update", 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)Csla.Serialization.Mobile.MobileFormatter.Deserialize(serialized); response = ConvertResponse(response); var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext); if (response != null && response.ErrorData == null) { var newobj = MobileFormatter.Deserialize(response.ObjectData); result = new DataPortalResult(newobj, 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; }
/// <summary> /// Get an existing business object. /// </summary> /// <param name="objectType">Type of business object to retrieve.</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 Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync) { try { SetContext(context); Initialize(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Fetch, IsSync = isSync }); AuthorizeRequest(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Fetch)); DataPortalResult result; DataPortalMethodInfo method = DataPortalMethodCache.GetFetchMethod(objectType, criteria); IDataPortalServer portal; #if !SILVERLIGHT && !NETFX_CORE switch (method.TransactionalAttribute.TransactionType) { #if !MONO case TransactionalTypes.EnterpriseServices: portal = GetServicedComponentPortal(method.TransactionalAttribute); try { result = await portal.Fetch(objectType, criteria, context, isSync); } finally { ((System.EnterpriseServices.ServicedComponent)portal).Dispose(); } break; #endif case TransactionalTypes.TransactionScope: portal = new TransactionalDataPortal(method.TransactionalAttribute); result = await portal.Fetch(objectType, criteria, context, isSync); break; default: portal = new DataPortalSelector(); result = await portal.Fetch(objectType, criteria, context, isSync); break; } #else portal = new DataPortalSelector(); result = await portal.Fetch(objectType, criteria, context, isSync); #endif Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Fetch, IsSync = isSync }); return(result); } catch (Csla.Server.DataPortalException ex) { Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Fetch, IsSync = isSync }); throw; } catch (AggregateException ex) { Exception error = null; if (ex.InnerExceptions.Count > 0) { error = ex.InnerExceptions[0].InnerException; } else { error = ex; } var fex = DataPortal.NewDataPortalException( "DataPortal.Fetch " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", error), null); Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync }); throw fex; } catch (Exception ex) { var fex = DataPortal.NewDataPortalException( "DataPortal.Fetch " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", ex), null); Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync }); throw fex; } finally { ClearContext(context); } }
public async Task <DataPortalResult> Create( Type objectType, object criteria, DataPortalContext context, bool isSync) { return(await portal.Create(objectType, criteria, context, isSync).ConfigureAwait(false)); }
/// <summary> /// 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> public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync) { try { SetContext(context); Initialize(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Delete, IsSync = isSync }); AuthorizeRequest(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Delete)); DataPortalResult result; DataPortalMethodInfo method; var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (factoryInfo != null) { var factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName); string methodName = factoryInfo.DeleteMethodName; method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, criteria); } else { method = DataPortalMethodCache.GetMethodInfo(objectType, "DataPortal_Delete", criteria); } IDataPortalServer portal; #if !(ANDROID || IOS) && !NETFX_CORE switch (method.TransactionalAttribute.TransactionType) { #if !MONO case TransactionalTypes.EnterpriseServices: portal = GetServicedComponentPortal(method.TransactionalAttribute); try { result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false); } finally { ((System.EnterpriseServices.ServicedComponent)portal).Dispose(); } break; #endif case TransactionalTypes.TransactionScope: portal = new TransactionalDataPortal(method.TransactionalAttribute); result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false); break; default: portal = new DataPortalBroker(); result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false); break; } #else portal = new DataPortalBroker(); result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false); #endif Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Delete, IsSync = isSync }); return(result); } catch (Csla.Server.DataPortalException ex) { Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Delete, IsSync = isSync }); throw; } catch (AggregateException ex) { Exception error = null; if (ex.InnerExceptions.Count > 0) { error = ex.InnerExceptions[0].InnerException; } else { error = ex; } var fex = DataPortal.NewDataPortalException( "DataPortal.Delete " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", error), null); Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync }); throw fex; } catch (Exception ex) { var fex = DataPortal.NewDataPortalException( "DataPortal.Delete " + Resources.FailedOnServer, new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", ex), null); Complete(new InterceptArgs { ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync }); throw fex; } finally { ClearContext(context); } }
/// <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; }
/// <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 { var request = GetBaseCriteriaRequest(); request.TypeName = objectType.AssemblyQualifiedName; if (!(criteria is IMobileObject)) { criteria = new PrimitiveCriteria(criteria); } request.CriteriaData = MobileFormatter.Serialize(criteria); request = ConvertRequest(request); var proxy = GetProxy(); DataPortalResult result = null; try { var response = await proxy.DeleteAsync(request); 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; }
private void ClearContext(DataPortalContext context) { ApplicationContext.SetLogicalExecutionLocation(_oldLocation); // if the dataportal is not remote then // do nothing if (!context.IsRemotePortal) return; ApplicationContext.Clear(); if (ApplicationContext.AuthenticationType != "Windows") ApplicationContext.User = null; }
public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { var portal = new DataPortalBroker(); return(await portal.Update(obj, context, isSync).ConfigureAwait(false)); }
public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync) { //Security.BusinessPrincipal //var temp = new DataPortalContext(new UnauthenticatedPrincipal(), context.IsRemotePortal); var task = new UpdateTask { Obj = obj, Context = context }; if (isSync) DoUpdate(task); else await Task.Factory.StartNew(DoUpdate, task); if (task.ResultException != null) throw task.ResultException; return task.Result; }