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.
HttpPortal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="HttpPortal.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Exposes server-side DataPortal functionality</summary>
7//-----------------------------------------------------------------------
9using System;
10using System.Collections.Generic;
11using System.IO;
12using System.Linq;
13using System.Net.Http;
14using System.Text;
15using System.Threading.Tasks;
16using Csla.Core;
17using System.Security.Principal;
20
21namespace Csla.Server.Hosts
22{
27 public class HttpPortal
28 {
33#pragma warning disable 1998
34 public async Task<DataPortalResponse> Create(CriteriaRequest request)
35#pragma warning restore 1998
36 {
37 var result = new DataPortalResponse();
38 try
39 {
40 request = ConvertRequest(request);
41
42 // unpack criteria data into object
43 object criteria = GetCriteria(request.CriteriaData);
45 {
46 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
47 }
48
49 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
50 var context = new DataPortalContext(
51 (IPrincipal)SerializationFormatterFactory.GetFormatter().Deserialize(request.Principal),
52 true,
53 request.ClientCulture,
54 request.ClientUICulture,
55 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.ClientContext),
56 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.GlobalContext));
57
58 var prtl = new Csla.Server.DataPortal();
59 var dpr = await prtl.Create(objectType, criteria, context, true);
60
61 if (dpr.Error != null)
62 result.ErrorData = new DataPortalErrorInfo(dpr.Error);
63 result.GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(dpr.GlobalContext);
64 result.ObjectData = SerializationFormatterFactory.GetFormatter().Serialize(dpr.ReturnObject);
65 }
66 catch (Exception ex)
67 {
68 result.ErrorData = new DataPortalErrorInfo(ex);
69 throw;
70 }
71 finally
72 {
73 result = ConvertResponse(result);
74 }
75 return result;
76 }
77
82#pragma warning disable 1998
83 public async Task<DataPortalResponse> Fetch(CriteriaRequest request)
84#pragma warning restore 1998
85 {
86 var result = new DataPortalResponse();
87 try
88 {
89 request = ConvertRequest(request);
90
91 // unpack criteria data into object
92 object criteria = GetCriteria(request.CriteriaData);
94 {
95 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
96 }
97
98 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
99 var context = new DataPortalContext(
100 (IPrincipal)SerializationFormatterFactory.GetFormatter().Deserialize(request.Principal),
101 true,
102 request.ClientCulture,
103 request.ClientUICulture,
104 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.ClientContext),
105 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.GlobalContext));
106
107 var prtl = new Csla.Server.DataPortal();
108 var dpr = await prtl.Fetch(objectType, criteria, context, true);
109
110 if (dpr.Error != null)
111 result.ErrorData = new DataPortalErrorInfo(dpr.Error);
112 result.GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(dpr.GlobalContext);
113 result.ObjectData = SerializationFormatterFactory.GetFormatter().Serialize(dpr.ReturnObject);
114 }
115 catch (Exception ex)
116 {
117 result.ErrorData = new DataPortalErrorInfo(ex);
118 throw;
119 }
120 finally
121 {
122 result = ConvertResponse(result);
123 }
124 return result;
125 }
126
131#pragma warning disable 1998
132 public async Task<DataPortalResponse> Update(UpdateRequest request)
133#pragma warning restore 1998
134 {
135 var result = new DataPortalResponse();
136 try
137 {
138 request = ConvertRequest(request);
139 // unpack object
140 object obj = GetCriteria(request.ObjectData);
141
142 var context = new DataPortalContext(
143 (IPrincipal)SerializationFormatterFactory.GetFormatter().Deserialize(request.Principal),
144 true,
145 request.ClientCulture,
146 request.ClientUICulture,
147 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.ClientContext),
148 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.GlobalContext));
149
150 var prtl = new Csla.Server.DataPortal();
151 var dpr = await prtl.Update(obj, context, true);
152
153 if (dpr.Error != null)
154 result.ErrorData = new DataPortalErrorInfo(dpr.Error);
155
156 result.GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(dpr.GlobalContext);
157 result.ObjectData = SerializationFormatterFactory.GetFormatter().Serialize(dpr.ReturnObject);
158 }
159 catch (Exception ex)
160 {
161 result.ErrorData = new DataPortalErrorInfo(ex);
162 throw;
163 }
164 finally
165 {
166 result = ConvertResponse(result);
167 }
168 return result;
169 }
170
175#pragma warning disable 1998
176 public async Task<DataPortalResponse> Delete(CriteriaRequest request)
177#pragma warning restore 1998
178 {
179 var result = new DataPortalResponse();
180 try
181 {
182 request = ConvertRequest(request);
183
184 // unpack criteria data into object
185 object criteria = GetCriteria(request.CriteriaData);
187 {
188 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
189 }
190
191 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
192 var context = new DataPortalContext(
193 (IPrincipal)SerializationFormatterFactory.GetFormatter().Deserialize(request.Principal),
194 true,
195 request.ClientCulture,
196 request.ClientUICulture,
197 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.ClientContext),
198 (ContextDictionary)SerializationFormatterFactory.GetFormatter().Deserialize(request.GlobalContext));
199
200 var prtl = new Csla.Server.DataPortal();
201 var dpr = await prtl.Delete(objectType, criteria, context, true);
202
203 if (dpr.Error != null)
204 result.ErrorData = new DataPortalErrorInfo(dpr.Error);
205 result.GlobalContext = SerializationFormatterFactory.GetFormatter().Serialize(dpr.GlobalContext);
206 result.ObjectData = SerializationFormatterFactory.GetFormatter().Serialize(dpr.ReturnObject);
207 }
208 catch (Exception ex)
209 {
210 result.ErrorData = new DataPortalErrorInfo(ex);
211 throw;
212 }
213 finally
214 {
215 result = ConvertResponse(result);
216 }
217 return result;
218 }
219
220 #region Criteria
221
222 private static object GetCriteria(byte[] criteriaData)
223 {
224 object criteria = null;
225 if (criteriaData != null)
226 criteria = SerializationFormatterFactory.GetFormatter().Deserialize(criteriaData);
227 return criteria;
228 }
229
230 #endregion Criteria
231
232 #region Extention Method for Requests
233
239 protected virtual UpdateRequest ConvertRequest(UpdateRequest request)
240 {
241 return request;
242 }
243
250 {
251 return request;
252 }
253
260 {
261 return response;
262 }
263
264 #endregion Extention Method for Requests
265 }
266}
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Class used as a wrapper for criteria based requests that use primitives
Provides consistent context information between the client and server DataPortal objects.
Implements the server-side DataPortal message router as discussed in Chapter 4.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
byte[] CriteriaData
Serialized data for the criteria object.
string TypeName
Assembly qualified name of the business object type to create.
byte[] GlobalContext
Serialized data for the global context object.
byte[] ClientContext
Serialized data for the client context object.
byte[] Principal
Serialized data for the principal object.
Message containing details about any server-side exception.
Response message for returning the results of a data portal call.
Request message for updating a business object.
byte[] GlobalContext
Serialized data for the global context object.
byte[] ClientContext
Serialized data for the client context object.
byte[] Principal
Serialized data for the principal object.
Exposes server-side DataPortal functionality through HTTP request/response.
Definition: HttpPortal.cs:28
async Task< DataPortalResponse > Create(CriteriaRequest request)
Create and initialize an existing business object.
Definition: HttpPortal.cs:34
virtual DataPortalResponse ConvertResponse(DataPortalResponse response)
Override to convert the response data after it comes back from the network.
Definition: HttpPortal.cs:259
async Task< DataPortalResponse > Delete(CriteriaRequest request)
Delete a business object.
Definition: HttpPortal.cs:176
async Task< DataPortalResponse > Update(UpdateRequest request)
Update a business object.
Definition: HttpPortal.cs:132
virtual UpdateRequest ConvertRequest(UpdateRequest request)
Override to convert the request data before it is transferred over the network.
Definition: HttpPortal.cs:239
async Task< DataPortalResponse > Fetch(CriteriaRequest request)
Get an existing business object.
Definition: HttpPortal.cs:83
virtual CriteriaRequest ConvertRequest(CriteriaRequest request)
Override to convert the request data before it is transferred over the network.
Definition: HttpPortal.cs:249