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.
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
22{
27 public class HttpPortal
28 {
29 private IDataPortalServer dataPortalServer;
30 private ApplicationContext ApplicationContext { get; set; }
31
37 public HttpPortal(ApplicationContext applicationContext, IDataPortalServer dataPortal)
38 {
39 dataPortalServer = dataPortal;
40 ApplicationContext = applicationContext;
41 }
42
47#pragma warning disable 1998
48 public async Task<DataPortalResponse> Create(CriteriaRequest request)
49#pragma warning restore 1998
50 {
52 try
53 {
54 request = ConvertRequest(request);
55
56 // unpack criteria data into object
57 object criteria = GetCriteria(ApplicationContext, request.CriteriaData);
59 {
60 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
61 }
62
63 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
64 var context = new DataPortalContext(
65 ApplicationContext, (IPrincipal)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.Principal),
66 true,
67 request.ClientCulture,
68 request.ClientUICulture,
69 (ContextDictionary)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.ClientContext));
70
71 var dpr = await dataPortalServer.Create(objectType, criteria, context, true);
72
73 if (dpr.Error != null)
75 result.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(dpr.ReturnObject);
76 }
77 catch (Exception ex)
78 {
80 throw;
81 }
82 finally
83 {
84 result = ConvertResponse(result);
85 }
86 return result;
87 }
88
93#pragma warning disable 1998
94 public async Task<DataPortalResponse> Fetch(CriteriaRequest request)
95#pragma warning restore 1998
96 {
98 try
99 {
100 request = ConvertRequest(request);
101
102 // unpack criteria data into object
103 object criteria = GetCriteria(ApplicationContext, request.CriteriaData);
105 {
106 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
107 }
108
109 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
110 var context = new DataPortalContext(
111 ApplicationContext, (IPrincipal)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.Principal),
112 true,
113 request.ClientCulture,
114 request.ClientUICulture,
115 (ContextDictionary)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.ClientContext));
116
117 var dpr = await dataPortalServer.Fetch(objectType, criteria, context, true);
118
119 if (dpr.Error != null)
121 result.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(dpr.ReturnObject);
122 }
123 catch (Exception ex)
124 {
126 throw;
127 }
128 finally
129 {
130 result = ConvertResponse(result);
131 }
132 return result;
133 }
134
139#pragma warning disable 1998
140 public async Task<DataPortalResponse> Update(UpdateRequest request)
141#pragma warning restore 1998
142 {
144 try
145 {
146 request = ConvertRequest(request);
147 // unpack object
148 object obj = GetCriteria(ApplicationContext, request.ObjectData);
149
150 var context = new DataPortalContext(
151 ApplicationContext, (IPrincipal)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.Principal),
152 true,
153 request.ClientCulture,
154 request.ClientUICulture,
155 (ContextDictionary)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.ClientContext));
156
157 var dpr = await dataPortalServer.Update(obj, context, true);
158
159 if (dpr.Error != null)
161
162 result.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(dpr.ReturnObject);
163 }
164 catch (Exception ex)
165 {
167 throw;
168 }
169 finally
170 {
171 result = ConvertResponse(result);
172 }
173 return result;
174 }
175
180#pragma warning disable 1998
181 public async Task<DataPortalResponse> Delete(CriteriaRequest request)
182#pragma warning restore 1998
183 {
185 try
186 {
187 request = ConvertRequest(request);
188
189 // unpack criteria data into object
190 object criteria = GetCriteria(ApplicationContext, request.CriteriaData);
192 {
193 criteria = ((Csla.DataPortalClient.PrimitiveCriteria)criteria).Value;
194 }
195
196 var objectType = Csla.Reflection.MethodCaller.GetType(AssemblyNameTranslator.GetAssemblyQualifiedName(request.TypeName), true);
197 var context = new DataPortalContext(
198 ApplicationContext, (IPrincipal)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.Principal),
199 true,
200 request.ClientCulture,
201 request.ClientUICulture,
202 (ContextDictionary)SerializationFormatterFactory.GetFormatter(ApplicationContext).Deserialize(request.ClientContext));
203
204 var dpr = await dataPortalServer.Delete(objectType, criteria, context, true);
205
206 if (dpr.Error != null)
208 result.ObjectData = SerializationFormatterFactory.GetFormatter(ApplicationContext).Serialize(dpr.ReturnObject);
209 }
210 catch (Exception ex)
211 {
213 throw;
214 }
215 finally
216 {
217 result = ConvertResponse(result);
218 }
219 return result;
220 }
221
222 #region Criteria
223
224 private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData)
225 {
226 object criteria = null;
227 if (criteriaData != null)
228 criteria = SerializationFormatterFactory.GetFormatter(applicationContext).Deserialize(criteriaData);
229 return criteria;
230 }
231
232 #endregion Criteria
233
234 #region Extention Method for Requests
235
241 protected virtual UpdateRequest ConvertRequest(UpdateRequest request)
242 {
243 return request;
244 }
245
252 {
253 return request;
254 }
255
262 {
263 return response;
264 }
265
266 #endregion Extention Method for Requests
267 }
268}
Csla.Server.DataPortalContext DataPortalContext
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...
object CreateInstance(Type objectType, params object[] parameters)
Creates an object using Activator.
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Class used as a wrapper for criteria based requests that use primitives
Message sent to the WCF data portal.
string ClientCulture
Serialized client culture.
byte[] CriteriaData
Serialized data for the criteria object.
string TypeName
Assembly qualified name of the business object type to create.
byte[] ClientContext
Serialized data for the client context object.
byte[] Principal
Serialized data for the principal object.
string ClientUICulture
Serialized client UI culture.
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[] ClientContext
Serialized data for the client context object.
string ClientCulture
Serialized client culture.
string ClientUICulture
Serialized client UI culture.
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:48
virtual DataPortalResponse ConvertResponse(DataPortalResponse response)
Override to convert the response data after it comes back from the network.
Definition: HttpPortal.cs:261
async Task< DataPortalResponse > Delete(CriteriaRequest request)
Delete a business object.
Definition: HttpPortal.cs:181
async Task< DataPortalResponse > Update(UpdateRequest request)
Update a business object.
Definition: HttpPortal.cs:140
virtual UpdateRequest ConvertRequest(UpdateRequest request)
Override to convert the request data before it is transferred over the network.
Definition: HttpPortal.cs:241
HttpPortal(ApplicationContext applicationContext, IDataPortalServer dataPortal)
Creates an instance of the type
Definition: HttpPortal.cs:37
async Task< DataPortalResponse > Fetch(CriteriaRequest request)
Get an existing business object.
Definition: HttpPortal.cs:94
virtual CriteriaRequest ConvertRequest(CriteriaRequest request)
Override to convert the request data before it is transferred over the network.
Definition: HttpPortal.cs:251
Interface implemented by server-side data portal components.
Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.