9using System.Collections;
10using System.Threading;
11using System.Threading.Tasks;
24 public class LocalProxy : DataPortalClient.IDataPortalProxy, IDisposable
33 OriginalApplicationContext = applicationContext;
34 var currentServiceProvider = OriginalApplicationContext.CurrentServiceProvider;
42 _scope = OriginalApplicationContext.CurrentServiceProvider.CreateScope();
43 currentServiceProvider = _scope.ServiceProvider;
48 var runtimeInfo = currentServiceProvider.GetRequiredService<
IRuntimeInfo>();
53 _portal = currentServiceProvider.GetRequiredService<Server.IDataPortalServer>();
58 private readonly LocalProxyOptions Options;
60 private readonly IServiceScope _scope;
61 private readonly Server.IDataPortalServer _portal;
62 private bool disposedValue;
68 if (!Options.UseLocalScope)
72 SetApplicationContext(useApplicationContext, applicationContext);
75 private void SetApplicationContext(
IUseApplicationContext useApplicationContext, ApplicationContext applicationContext)
79 if (!Options.UseLocalScope)
82 if (useApplicationContext !=
null && !ReferenceEquals(useApplicationContext.
ApplicationContext, applicationContext))
86 if (useApplicationContext is IUseFieldManager useFieldManager)
87 SetApplicationContext(useFieldManager.FieldManager, applicationContext);
89 if (useApplicationContext is IUseBusinessRules useBusinessRules)
90 SetApplicationContext(useBusinessRules.BusinessRules, applicationContext);
92 if (useApplicationContext is IManageProperties target)
94 foreach (var managedProperty
in target.GetManagedProperties())
100 if (!isLazyLoadedProperty
101 || (isLazyLoadedProperty && target.FieldExists(managedProperty)))
108 else if (typeof(IEnumerable).IsAssignableFrom(managedProperty.Type))
111 var enumerable = (IEnumerable)target.ReadProperty(managedProperty);
112 if (enumerable !=
null)
114 foreach (var item
in enumerable)
116 SetApplicationContext(item, applicationContext);
124 if (useApplicationContext is IContainsDeletedList containsDeletedList)
125 foreach (var item
in containsDeletedList.DeletedList)
126 SetApplicationContext(item, applicationContext);
128 if (useApplicationContext is IEnumerable list)
129 foreach (var item
in list)
130 SetApplicationContext(item, applicationContext);
140 private void ResetApplicationContext()
142 if (Options.UseLocalScope)
143 CurrentApplicationContext.ApplicationContextAccessor = OriginalApplicationContext.ApplicationContextAccessor;
156 public async Task<DataPortalResult>
Create(
160 SetApplicationContext(criteria, CurrentApplicationContext);
163 result = await _portal.
Create(objectType, criteria, context, isSync);
167 if (!Options.FlowSynchronizationContext || SynchronizationContext.Current ==
null)
168 result = await Task.Run(() => this._portal.
Create(objectType, criteria, context, isSync));
170 result = await await Task.Factory.StartNew(() => this._portal.
Create(objectType, criteria, context, isSync),
171 CancellationToken.None,
172 TaskCreationOptions.None,
173 TaskScheduler.FromCurrentSynchronizationContext());
175 ResetApplicationContext();
192 SetApplicationContext(criteria, CurrentApplicationContext);
195 result = await _portal.
Fetch(objectType, criteria, context, isSync);
199 if (!Options.FlowSynchronizationContext || SynchronizationContext.Current ==
null)
200 result = await Task.Run(() => this._portal.
Fetch(objectType, criteria, context, isSync));
202 result = await await Task.Factory.StartNew(() => this._portal.
Fetch(objectType, criteria, context, isSync),
203 CancellationToken.None,
204 TaskCreationOptions.None,
205 TaskScheduler.FromCurrentSynchronizationContext());
207 ResetApplicationContext();
223 SetApplicationContext(obj, CurrentApplicationContext);
226 result = await _portal.
Update(obj, context, isSync);
230 if (!Options.FlowSynchronizationContext || SynchronizationContext.Current ==
null)
231 result = await Task.Run(() => this._portal.
Update(obj, context, isSync));
233 result = await await Task.Factory.StartNew(() => this._portal.
Update(obj, context, isSync),
234 CancellationToken.None,
235 TaskCreationOptions.None,
236 TaskScheduler.FromCurrentSynchronizationContext());
238 ResetApplicationContext();
255 SetApplicationContext(criteria, CurrentApplicationContext);
258 result = await _portal.
Delete(objectType, criteria, context, isSync);
262 if (!Options.FlowSynchronizationContext || SynchronizationContext.Current ==
null)
263 result = await Task.Run(() => this._portal.
Delete(objectType, criteria, context, isSync));
265 result = await await Task.Factory.StartNew(() => this._portal.
Delete(objectType, criteria, context, isSync),
266 CancellationToken.None,
267 TaskCreationOptions.None,
268 TaskScheduler.FromCurrentSynchronizationContext());
270 ResetApplicationContext();
281 get {
return false; }
288 protected virtual void Dispose(
bool disposing)
296 disposedValue =
true;
307 GC.SuppressFinalize(
this);
Provides consistent context information between the client and server DataPortal objects.
LogicalExecutionLocations LogicalExecutionLocation
Return Logical Execution Location - Client or Server This is applicable to Local mode as well
object GetRequiredService(Type serviceType)
Attempts to get service via DI using ServiceProviderServiceExtensions.GetRequiredService.
LogicalExecutionLocations
Enum representing the logical execution location The setting is set to server when server is execting...
bool IsAStatefulContextManager
Gets a value indicating whether the current context manager is used in a stateful context (e....
Implements a data portal proxy to relay data portal calls to an application server hosted locally in ...
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to delete a business object.
virtual void Dispose(bool disposing)
Dispose current object
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to create a new business object.
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Called by DataPortal to update a business object.
bool IsServerRemote
Gets a value indicating whether this proxy will invoke a remote data portal server,...
LocalProxy(ApplicationContext applicationContext, LocalProxyOptions options)
Creates an instance of the type
void Dispose()
Dispose current object
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by DataPortal to load an existing business object.
bool UseLocalScope
Gets or sets a value indicating whether a new dependency injection scope should be created for each d...
DataPortalResult defines the results of DataPortal operation.
Provides consistent context information between the client and server DataPortal objects.
Implement if a class requires access to the CSLA ApplicationContext type.
ApplicationContext ApplicationContext
Gets or sets the current ApplicationContext object.
Information about the current runtime environment.
bool LocalProxyNewScopeExists
Gets a value indicating whether a LocalProxy (if configured and used) has created a new scope in prep...
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.
RelationshipTypes
List of valid relationship types between a parent object and another object through a managed propert...