9using System.Collections.Concurrent;
10using System.Threading.Tasks;
12using System.Runtime.Loader;
23 private static readonly ConcurrentDictionary<Type, Tuple<string, DataPortalMethodNames>> _methodNameList =
24 new ConcurrentDictionary<Type, Tuple<string, DataPortalMethodNames>>();
26 private static readonly ConcurrentDictionary<Type, DataPortalMethodNames> _methodNameList =
new();
29 private readonly IDataPortalTarget _target;
30 private readonly DataPortalMethodNames _methodNames;
32 public DataPortalTarget(
object obj)
35 _target = obj as IDataPortalTarget;
38 var objectType = obj.GetType();
40 var methodNameListInfo = _methodNameList.GetOrAdd(
42 (t) => AssemblyLoadContextManager.CreateCacheInstance(
44 DataPortalMethodNames.Default,
45 OnAssemblyLoadContextUnload
49 _methodNames = methodNameListInfo.Item2;
51 _methodNames = _methodNameList.GetOrAdd(obj.GetType(),
52 (t) => DataPortalMethodNames.Default);
56 public void OnDataPortalInvoke(DataPortalEventArgs eventArgs)
59 _target.DataPortal_OnDataPortalInvoke(eventArgs);
64 public void Child_OnDataPortalInvoke(DataPortalEventArgs eventArgs)
67 _target.Child_OnDataPortalInvoke(eventArgs);
73 public void OnDataPortalInvokeComplete(DataPortalEventArgs eventArgs)
76 _target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
81 internal void Child_OnDataPortalInvokeComplete(DataPortalEventArgs eventArgs)
84 _target.Child_OnDataPortalInvokeComplete(eventArgs);
89 internal void OnDataPortalException(DataPortalEventArgs eventArgs, Exception ex)
92 _target.DataPortal_OnDataPortalException(eventArgs, ex);
98 internal void Child_OnDataPortalException(DataPortalEventArgs eventArgs, Exception ex)
101 _target.Child_OnDataPortalException(eventArgs, ex);
106 public void ThrowIfBusy()
109 throw new InvalidOperationException(
string.Format(
"{0}.IsBusy == true",
Instance.GetType().Name));
112 public void MarkNew()
120 public void MarkAsChild()
123 _target.MarkAsChild();
128 internal void MarkOld()
136 private async Task InvokeOperationAsync<T>(
object criteria,
bool isSync)
137 where T : DataPortalOperationAttribute
139 object[] parameters = DataPortal.GetCriteriaArray(criteria);
140 await CallMethodTryAsyncDI<T>(isSync, parameters).ConfigureAwait(
false);
143 public async Task CreateAsync(
object criteria,
bool isSync)
145 await InvokeOperationAsync<CreateAttribute>(criteria, isSync).ConfigureAwait(
false);
148 public async Task CreateChildAsync(params
object[] parameters)
150 await CallMethodTryAsyncDI<CreateChildAttribute>(
false, parameters).ConfigureAwait(
false);
153 public async Task FetchAsync(
object criteria,
bool isSync)
155 await InvokeOperationAsync<FetchAttribute>(criteria, isSync).ConfigureAwait(
false);
158 public async Task FetchChildAsync(params
object[] parameters)
160 await CallMethodTryAsyncDI<FetchChildAttribute>(
false, parameters).ConfigureAwait(
false);
164 public async Task UpdateAsync(
bool isSync)
166 if (Instance is Core.BusinessBase busObj)
168 if (busObj.IsDeleted)
173 await InvokeOperationAsync<DeleteSelfAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(
false);
182 await InvokeOperationAsync<InsertAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(
false);
187 await InvokeOperationAsync<UpdateAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(
false);
197 await InvokeOperationAsync<UpdateAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(
false);
202 public async Task UpdateChildAsync(params
object[] parameters)
205 if (Instance is Core.BusinessBase busObj)
207 if (busObj.IsDeleted)
212 await CallMethodTryAsyncDI<DeleteSelfChildAttribute>(
false, parameters).ConfigureAwait(
false);
221 await CallMethodTryAsyncDI<InsertChildAttribute>(
false, parameters).ConfigureAwait(
false);
226 await CallMethodTryAsyncDI<UpdateChildAttribute>(
false, parameters).ConfigureAwait(
false);
232 else if (Instance is Core.ICommandObject)
235 await CallMethodTryAsyncDI<ExecuteChildAttribute>(
false, parameters).ConfigureAwait(
false);
242 await CallMethodTryAsyncDI<UpdateChildAttribute>(
false, parameters).ConfigureAwait(
false);
247 public async Task ExecuteAsync(
bool isSync)
249 await InvokeOperationAsync<ExecuteAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(
false);
252 public async Task DeleteAsync(
object criteria,
bool isSync)
254 await InvokeOperationAsync<DeleteAttribute>(criteria, isSync).ConfigureAwait(
false);
258 private static void OnAssemblyLoadContextUnload(AssemblyLoadContext context)
260 AssemblyLoadContextManager.RemoveFromCache(_methodNameList, context,
true);
265 internal class DataPortalMethodNames
267 public static readonly DataPortalMethodNames
Default =
new();
268 public string OnDataPortalInvoke {
get;
set; } =
"DataPortal_OnDataPortalInvoke";
269 public string OnDataPortalInvokeComplete {
get;
set; } =
"DataPortal_OnDataPortalInvokeComplete";
270 public string OnDataPortalException {
get;
set; } =
"DataPortal_OnDataPortalException";
Enables simple invocation of methods against the contained object using late binding.
object CallMethodIfImplemented(string method)
Uses reflection to dynamically invoke a method if that method is implemented on the target object.
object Instance
Object instance managed by LateBoundObject.
bool IsBusy
Gets a value indicating whether the object, or any of the object's child objects, are busy running an...
Defines the common properties required objects that track their own status.
@ Default
Default value, rule can run in any context