CSLA.NET 6.0.0
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;
13using Csla.Server;
15
17{
22 public abstract class DataPortalProxy : IDataPortalProxy
23 {
28 public DataPortalProxy(ApplicationContext applicationContext)
29 {
30 ApplicationContext = applicationContext;
31 }
32
36 protected ApplicationContext ApplicationContext { get; set; }
37
42 public virtual bool IsServerRemote => true;
43
48 public virtual int Timeout { get; set; }
49
54 public string DataPortalUrl { get; protected set; }
55
66 public async virtual Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
67 {
68 DataPortalResult result;
69 try
70 {
71 var request = GetBaseCriteriaRequest();
72 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
73 if (criteria is not IMobileObject)
74 {
75 criteria = new PrimitiveCriteria(criteria);
76 }
77 request.CriteriaData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(criteria);
78 request = ConvertRequest(request);
79 var serialized = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(request);
80 serialized = await CallDataPortalServer(serialized, "create", GetRoutingToken(objectType), isSync);
81 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(serialized);
82 response = ConvertResponse(response);
83
84 if (response.ErrorData == null)
85 {
86 var obj = SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(response.ObjectData);
87 result = new DataPortalResult(ApplicationContext, obj, null);
88 }
89 else if (response.ErrorData != null)
90 {
91 var ex = new DataPortalException(response.ErrorData);
92 result = new DataPortalResult(ApplicationContext, null, ex);
93 }
94 else
95 {
96 throw new DataPortalException("null response", null);
97 }
98 }
99 catch (Exception ex)
100 {
101 result = new DataPortalResult(ApplicationContext, null, ex);
102 }
103 if (result.Error != null)
104 throw result.Error;
105 return result;
106 }
107
118 public async virtual Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
119 {
120 DataPortalResult result;
121 try
122 {
123 var request = GetBaseCriteriaRequest();
124 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
125 if (criteria is not IMobileObject)
126 {
127 criteria = new PrimitiveCriteria(criteria);
128 }
129 request.CriteriaData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(criteria);
130 request = ConvertRequest(request);
131
132 var serialized = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(request);
133
134 serialized = await CallDataPortalServer(serialized, "fetch", GetRoutingToken(objectType), isSync);
135
136 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(serialized);
137 response = ConvertResponse(response);
138 if (response.ErrorData == null)
139 {
140 var obj = SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(response.ObjectData);
141 result = new DataPortalResult(ApplicationContext, obj, null);
142 }
143 else if (response.ErrorData != null)
144 {
145 var ex = new DataPortalException(response.ErrorData);
146 result = new DataPortalResult(ApplicationContext, null, ex);
147 }
148 else
149 {
150 throw new DataPortalException("null response", null);
151 }
152 }
153 catch (Exception ex)
154 {
155 result = new DataPortalResult(ApplicationContext, null, ex);
156 }
157 if (result.Error != null)
158 throw result.Error;
159 return result;
160 }
161
171 public async virtual Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
172 {
173 DataPortalResult result;
174 try
175 {
176 var request = GetBaseUpdateCriteriaRequest();
177 request.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(obj);
178 request = ConvertRequest(request);
179
180 var serialized = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(request);
181
182 serialized = await CallDataPortalServer(serialized, "update", GetRoutingToken(obj.GetType()), isSync);
183
184 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(serialized);
185 response = ConvertResponse(response);
186 if (response.ErrorData == null)
187 {
188 var newobj = SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(response.ObjectData);
189 result = new DataPortalResult(ApplicationContext, newobj, null);
190 }
191 else if (response.ErrorData != null)
192 {
193 var ex = new DataPortalException(response.ErrorData);
194 result = new DataPortalResult(ApplicationContext, null, ex);
195 }
196 else
197 {
198 throw new DataPortalException("null response", null);
199 }
200 }
201 catch (Exception ex)
202 {
203 result = new DataPortalResult(ApplicationContext, null, ex);
204 }
205 if (result.Error != null)
206 throw result.Error;
207 return result;
208 }
209
220 public async virtual Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context,
221 bool isSync)
222 {
223 DataPortalResult result;
224 try
225 {
226 var request = GetBaseCriteriaRequest();
227 request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
228 if (criteria is not IMobileObject)
229 {
230 criteria = new PrimitiveCriteria(criteria);
231 }
232 request.CriteriaData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(criteria);
233 request = ConvertRequest(request);
234
235 var serialized = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(request);
236
237 serialized = await CallDataPortalServer(serialized, "delete", GetRoutingToken(objectType), isSync);
238
239 var response = (DataPortalResponse)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(serialized);
240 response = ConvertResponse(response);
241 if (response.ErrorData == null)
242 {
243 result = new DataPortalResult(ApplicationContext, null, null);
244 }
245 else if (response.ErrorData != null)
246 {
247 var ex = new DataPortalException(response.ErrorData);
248 result = new DataPortalResult(ApplicationContext, null, ex);
249 }
250 else
251 {
252 throw new DataPortalException("null response", null);
253 }
254 }
255 catch (Exception ex)
256 {
257 result = new DataPortalResult(ApplicationContext, null, ex);
258 }
259 if (result.Error != null)
260 throw result.Error;
261 return result;
262 }
263
270 {
271 return request;
272 }
273
279 protected virtual UpdateRequest ConvertRequest(UpdateRequest request)
280 {
281 return request;
282 }
283
290 {
291 return response;
292 }
293
303 protected abstract Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string routingToken, bool isSync);
304
305 private string GetRoutingToken(Type objectType)
306 {
307 string result = null;
308 var list = objectType.GetCustomAttributes(typeof(DataPortalServerRoutingTagAttribute), false);
309 if (list.Length > 0)
310 result = ((DataPortalServerRoutingTagAttribute)list[0]).RoutingTag;
311 return result;
312 }
313
314 #region Criteria
315
316 private CriteriaRequest GetBaseCriteriaRequest()
317 {
319 result.CriteriaData = null;
320 result.ClientContext = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(ApplicationContext.ClientContext);
321 result.Principal = SerializationFormatterFactory.GetFormatter(ApplicationContext)
322 .Serialize(ApplicationContext.AuthenticationType == "Windows" ? null : ApplicationContext.User);
323 result.ClientCulture = System.Globalization.CultureInfo.CurrentCulture.Name;
324 result.ClientUICulture = System.Globalization.CultureInfo.CurrentUICulture.Name;
325 return result;
326 }
327
328 private UpdateRequest GetBaseUpdateCriteriaRequest()
329 {
331 result.ObjectData = null;
332 result.ClientContext = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(ApplicationContext.ClientContext);
333 result.Principal = SerializationFormatterFactory.GetFormatter(ApplicationContext)
334 .Serialize(ApplicationContext.AuthenticationType == "Windows" ? null : ApplicationContext.User);
335 result.ClientCulture = Thread.CurrentThread.CurrentCulture.Name;
336 result.ClientUICulture = Thread.CurrentThread.CurrentUICulture.Name;
337 return result;
338 }
339
340 #endregion Criteria
341 }
342}
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
static string AuthenticationType
Gets the authentication type being used by the CSLA .NET framework.
IPrincipal User
Get or set the current IPrincipal object representing the user's identity.
ContextDictionary ClientContext
Returns the application-specific context data provided by the client.
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.
DataPortalProxy(ApplicationContext applicationContext)
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.
Message sent to the WCF data portal.
byte[] CriteriaData
Serialized data for the criteria object.
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...