Skip to content

Commit

Permalink
MarimerLLC#1102 Create and use method name cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rockfordlhotka committed May 28, 2019
1 parent 0ffa01a commit 762d8b3
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions Source/Csla.Shared/Server/DataPortalTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@
// <summary>Encapsulates server-side data portal invocations</summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Csla.Reflection;

namespace Csla.Server
{
internal class DataPortalTarget : LateBoundObject
{
private static readonly ConcurrentDictionary<Type, DataPortalMethodNames> _methodNameList =
new ConcurrentDictionary<Type, DataPortalMethodNames>();
private readonly IDataPortalTarget _target;
private readonly DataPortalMethodNames _methodNames;

public DataPortalTarget(object obj)
: base(obj)
{
_target = obj as IDataPortalTarget;
_methodNames = _methodNameList.GetOrAdd(obj.GetType(),
(t) => DataPortalMethodNames.Default); // TODO: get method names dynamically
}

public void OnDataPortalInvoke(DataPortalEventArgs eventArgs)
{
if (_target != null)
_target.DataPortal_OnDataPortalInvoke(eventArgs);
else
CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs);
CallMethodIfImplemented(_methodNames.OnDataPortalInvoke, eventArgs);
}

public void OnDataPortalInvokeComplete(DataPortalEventArgs eventArgs)
{
if (_target != null)
_target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
else
CallMethodIfImplemented("DataPortal_OnDataPortalInvokeComplete", eventArgs);
CallMethodIfImplemented(_methodNames.OnDataPortalInvokeComplete, eventArgs);
}

internal void OnDataPortalException(DataPortalEventArgs eventArgs, Exception ex)
{
if (_target != null)
_target.DataPortal_OnDataPortalException(eventArgs, ex);
else
CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex);
CallMethodIfImplemented(_methodNames.OnDataPortalException, eventArgs, ex);
}

public void ThrowIfBusy()
Expand Down Expand Up @@ -71,27 +77,27 @@ public async Task CreateAsync(object criteria, bool isSync)
{
if (criteria is EmptyCriteria)
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Create");
await CallMethodTryAsync("DataPortal_Create").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Create);
await CallMethodTryAsync(_methodNames.Create).ConfigureAwait(false);
}
else
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Create", criteria);
await CallMethodTryAsync("DataPortal_Create", criteria).ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.CreateCriteria, criteria);
await CallMethodTryAsync(_methodNames.CreateCriteria, criteria).ConfigureAwait(false);
}
}

public async Task FetchAsync(object criteria, bool isSync)
{
if (criteria is EmptyCriteria)
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Fetch");
await CallMethodTryAsync("DataPortal_Fetch").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Fetch);
await CallMethodTryAsync(_methodNames.Fetch).ConfigureAwait(false);
}
else
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Fetch", criteria);
await CallMethodTryAsync("DataPortal_Fetch", criteria).ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.FetchCriteria, criteria);
await CallMethodTryAsync(_methodNames.FetchCriteria, criteria).ConfigureAwait(false);
}
}

Expand All @@ -104,8 +110,8 @@ public async Task UpdateAsync(bool isSync)
if (!busObj.IsNew)
{
// tell the object to delete itself
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_DeleteSelf");
await CallMethodTryAsync("DataPortal_DeleteSelf").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.DeleteSelf);
await CallMethodTryAsync(_methodNames.DeleteSelf).ConfigureAwait(false);
}
MarkNew();
}
Expand All @@ -114,14 +120,14 @@ public async Task UpdateAsync(bool isSync)
if (busObj.IsNew)
{
// tell the object to insert itself
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Insert");
await CallMethodTryAsync("DataPortal_Insert").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Insert);
await CallMethodTryAsync(_methodNames.Insert).ConfigureAwait(false);
}
else
{
// tell the object to update itself
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Update");
await CallMethodTryAsync("DataPortal_Update").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Update);
await CallMethodTryAsync(_methodNames.Update).ConfigureAwait(false);
}
MarkOld();
}
Expand All @@ -131,22 +137,39 @@ public async Task UpdateAsync(bool isSync)
// this is an updatable collection or some other
// non-BusinessBase type of object
// tell the object to update itself
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Update");
await CallMethodTryAsync("DataPortal_Update").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Update);
await CallMethodTryAsync(_methodNames.Update).ConfigureAwait(false);
MarkOld();
}
}

public async Task ExecuteAsync(bool isSync)
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Execute");
await CallMethodTryAsync("DataPortal_Execute").ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Execute);
await CallMethodTryAsync(_methodNames.Execute).ConfigureAwait(false);
}

public async Task DeleteAsync(object criteria, bool isSync)
{
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, "DataPortal_Delete", criteria);
await CallMethodTryAsync("DataPortal_Delete", criteria).ConfigureAwait(false);
Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, Instance, _methodNames.Delete, criteria);
await CallMethodTryAsync(_methodNames.Delete, criteria).ConfigureAwait(false);
}
}

internal class DataPortalMethodNames
{
public static readonly DataPortalMethodNames Default = new DataPortalMethodNames();
public string Create { get; set; } = "DataPortal_Create";
public string CreateCriteria { get; set; } = "DataPortal_Create";
public string Fetch { get; set; } = "DataPortal_Fetch";
public string FetchCriteria { get; set; } = "DataPortal_Fetch";
public string Insert { get; set; } = "DataPortal_Insert";
public string Update { get; set; } = "DataPortal_Update";
public string Execute { get; set; } = "DataPortal_Execute";
public string Delete { get; set; } = "DataPortal_Delete";
public string DeleteSelf { get; set; } = "DataPortal_DeleteSelf";
public string OnDataPortalInvoke { get; set; } = "DataPortal_OnDataPortalInvoke";
public string OnDataPortalInvokeComplete { get; set; } = "DataPortal_OnDataPortalInvokeComplete";
public string OnDataPortalException { get; set; } = "DataPortal_OnDataPortalException";
}
}

0 comments on commit 762d8b3

Please sign in to comment.