CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortalProxy.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalProxy.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Abstract data portal proxy with common data portal proxy behaviour. Implements IDataPortalProxy</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Threading;
10using System.Threading.Tasks;
11using Csla.Core;
14using Csla.Server;
16
18{
23 public abstract class DataPortalProxy : IDataPortalProxy
24 {
29 public virtual bool IsServerRemote => true;
30
35 public virtual int Timeout { get; set; }
36
41 public string DataPortalUrl { get; protected set; } = ApplicationContext.DataPortalUrlString;
42
53 public async virtual Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
54 {
55 DataPortalResult result;
56 try
57 {
58 var request = GetBaseCriteriaRequest();
59 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
60 if (!(criteria is IMobileObject))
61 {
62 criteria = new PrimitiveCriteria(criteria);
63 }
64 request.CriteriaData = SerializationFormatterFactory.GetFormatter().Serialize(criteria);
65 request = ConvertRequest(request);
66 var serialized = SerializationFormatterFactory.GetFormatter().Serialize(request);
67 serialized = await CallDataPortalServer(serialized, "create", GetRoutingToken(objectType), isSync);
68 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter().Deserialize(serialized);
69 response = ConvertResponse(response);
70
71 var globalContext = (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(response.GlobalContext);
72 if (response.ErrorData == null)
73 {
74 var obj = SerializationFormatterFactory.GetFormatter().Deserialize(response.ObjectData);
75 result = new DataPortalResult(obj, null, globalContext);
76 }
77 else if (response.ErrorData != null)
78 {
79 var ex = new DataPortalException(response.ErrorData);
80 result = new DataPortalResult(null, ex, globalContext);
81 }
82 else
83 {
84 throw new DataPortalException("null response", null);
85 }
86 }
87 catch (Exception ex)
88 {
89 result = new DataPortalResult(null, ex, null);
90 }
91 if (result.Error != null)
92 throw result.Error;
93 return result;
94 }
95
106 public async virtual Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
107 {
108 DataPortalResult result;
109 try
110 {
111 var request = GetBaseCriteriaRequest();
112 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
113 if (!(criteria is IMobileObject))
114 {
115 criteria = new PrimitiveCriteria(criteria);
116 }
117 request.CriteriaData = SerializationFormatterFactory.GetFormatter().Serialize(criteria);
118 request = ConvertRequest(request);
119
120 var serialized = SerializationFormatterFactory.GetFormatter().Serialize(request);
121
122 serialized = await CallDataPortalServer(serialized, "fetch", GetRoutingToken(objectType), isSync);
123
124 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter().Deserialize(serialized);
125 response = ConvertResponse(response);
126 var globalContext = (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(response.GlobalContext);
127 if (response.ErrorData == null)
128 {
129 var obj = SerializationFormatterFactory.GetFormatter().Deserialize(response.ObjectData);
130 result = new DataPortalResult(obj, null, globalContext);
131 }
132 else if (response.ErrorData != null)
133 {
134 var ex = new DataPortalException(response.ErrorData);
135 result = new DataPortalResult(null, ex, globalContext);
136 }
137 else
138 {
139 throw new DataPortalException("null response", null);
140 }
141 }
142 catch (Exception ex)
143 {
144 result = new DataPortalResult(null, ex, null);
145 }
146 if (result.Error != null)
147 throw result.Error;
148 return result;
149 }
150
160 public async virtual Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
161 {
162 DataPortalResult result;
163 try
164 {
165 var request = GetBaseUpdateCriteriaRequest();
166 request.ObjectData = SerializationFormatterFactory.GetFormatter().Serialize(obj);
167 request = ConvertRequest(request);
168
169 var serialized = SerializationFormatterFactory.GetFormatter().Serialize(request);
170
171 serialized = await CallDataPortalServer(serialized, "update", GetRoutingToken(obj.GetType()), isSync);
172
173 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter().Deserialize(serialized);
174 response = ConvertResponse(response);
175 var globalContext = (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(response.GlobalContext);
176 if (response.ErrorData == null)
177 {
178 var newobj = SerializationFormatterFactory.GetFormatter().Deserialize(response.ObjectData);
179 result = new DataPortalResult(newobj, null, globalContext);
180 }
181 else if (response.ErrorData != null)
182 {
183 var ex = new DataPortalException(response.ErrorData);
184 result = new DataPortalResult(null, ex, globalContext);
185 }
186 else
187 {
188 throw new DataPortalException("null response", null);
189 }
190 }
191 catch (Exception ex)
192 {
193 result = new DataPortalResult(null, ex, null);
194 }
195 if (result.Error != null)
196 throw result.Error;
197 return result;
198 }
199
210 public async virtual Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context,
211 bool isSync)
212 {
213 DataPortalResult result;
214 try
215 {
216 var request = GetBaseCriteriaRequest();
217 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
218 if (!(criteria is IMobileObject))
219 {
220 criteria = new PrimitiveCriteria(criteria);
221 }
222 request.CriteriaData = SerializationFormatterFactory.GetFormatter().Serialize(criteria);
223 request = ConvertRequest(request);
224
225 var serialized = SerializationFormatterFactory.GetFormatter().Serialize(request);
226
227 serialized = await CallDataPortalServer(serialized, "delete", GetRoutingToken(objectType), isSync);
228
229 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter().Deserialize(serialized);
230 response = ConvertResponse(response);
231 var globalContext = (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(response.GlobalContext);
232 if (response.ErrorData == null)
233 {
234 result = new DataPortalResult(null, null, globalContext);
235 }
236 else if (response.ErrorData != null)
237 {
238 var ex = new DataPortalException(response.ErrorData);
239 result = new DataPortalResult(null, ex, globalContext);
240 }
241 else
242 {
243 throw new DataPortalException("null response", null);
244 }
245 }
246 catch (Exception ex)
247 {
248 result = new DataPortalResult(null, ex, null);
249 }
250 if (result.Error != null)
251 throw result.Error;
252 return result;
253 }
254
261 {
262 return request;
263 }
264
270 protected virtual UpdateRequest ConvertRequest(UpdateRequest request)
271 {
272 return request;
273 }
274
281 {
282 return response;
283 }
284
294 protected abstract Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync);
295
296 private string GetRoutingToken(Type objectType)
297 {
298 string result = null;
299 var list = objectType.GetCustomAttributes(typeof(DataPortalServerRoutingTagAttribute), false);
300 if (list.Length > 0)
301 result = ((DataPortalServerRoutingTagAttribute)list[0]).RoutingTag;
302 return result;
303 }
304
305 #region Criteria
306
307 private static CriteriaRequest GetBaseCriteriaRequest()
308 {
309 return new CriteriaRequest
310 {
311 CriteriaData = null,
312 ClientContext = SerializationFormatterFactory.GetFormatter().Serialize(ApplicationContext.ClientContext),
313#pragma warning disable CS0618 // Type or member is obsolete
314 GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(ApplicationContext.GlobalContext),
315#pragma warning restore CS0618 // Type or member is obsolete
316 Principal = SerializationFormatterFactory.GetFormatter()
317 .Serialize(ApplicationContext.AuthenticationType == "Windows" ? null : ApplicationContext.User),
318 ClientCulture = System.Globalization.CultureInfo.CurrentCulture.Name,
319 ClientUICulture = System.Globalization.CultureInfo.CurrentUICulture.Name
320 };
321 }
322
323 private static UpdateRequest GetBaseUpdateCriteriaRequest()
324 {
325 return new UpdateRequest
326 {
327 ObjectData = null,
328 ClientContext = SerializationFormatterFactory.GetFormatter().Serialize(ApplicationContext.ClientContext),
329#pragma warning disable CS0618 // Type or member is obsolete
330 GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(ApplicationContext.GlobalContext),
331#pragma warning restore CS0618 // Type or member is obsolete
332 Principal = SerializationFormatterFactory.GetFormatter()
333 .Serialize(ApplicationContext.AuthenticationType == "Windows" ? null : ApplicationContext.User),
334 ClientCulture = Thread.CurrentThread.CurrentCulture.Name,
335 ClientUICulture = Thread.CurrentThread.CurrentUICulture.Name
336 };
337 }
338
339 #endregion Criteria
340 }
341}
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Implements a data portal proxy to relay data portal calls to a remote application server.
virtual async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to delete a business object.
virtual async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Called by DataPortal to update a business object.
virtual async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to create a new business object.
virtual int Timeout
Gets or sets the Client timeout in milliseconds (0 uses default timeout).
virtual bool IsServerRemote
Gets a value indicating whether the data portal is hosted on a remote server.
string DataPortalUrl
Gets the URL address for the data portal server used by this proxy instance.
virtual async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to load an existing business object.
abstract Task< byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync)
Override this method with implementation of sending and receiving of data to the server Returns seria...
virtual CriteriaRequest ConvertRequest(CriteriaRequest request)
Override this method to manipulate the message request data sent to the server.
virtual UpdateRequest ConvertRequest(UpdateRequest request)
Override this method to manipulate the message request data sent to the server.
virtual DataPortalResponse ConvertResponse(DataPortalResponse response)
Override this method to manipulate the message request data returned from the server.
Class used as a wrapper for criteria based requests that use primitives
This exception is returned for any errors occurring during the server-side DataPortal invocation.
DataPortalResult defines the results of DataPortal operation.
Exception Error
Error that occurred during the DataPotal call.
Specifies a routing tag for use by the server-side data portal.
Provides consistent context information between the client and server DataPortal objects.
Response message for returning the results of a data portal call.
Request message for updating a business object.
Interface implemented by client-side data portal proxy objects.
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...