Skip to content

Commit

Permalink
MarimerLLC#1102 Get create/fetch/update working with ChildDataPortal
Browse files Browse the repository at this point in the history
  • Loading branch information
rockfordlhotka committed Jul 31, 2019
1 parent f22bdf6 commit f97e7f4
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 165 deletions.
4 changes: 2 additions & 2 deletions Source/Csla.Axml.Android/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions Source/Csla.Shared/DataPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public static T CreateChild<T>(params object[] parameters)
public static async Task<T> CreateChildAsync<T>()
{
Server.ChildDataPortal portal = new Server.ChildDataPortal();
return await portal.CreateAsync<T>();
return await portal.CreateAsync<T>(EmptyCriteria.Instance);
}

/// <summary>
Expand Down Expand Up @@ -704,7 +704,7 @@ public static T FetchChild<T>(params object[] parameters)
public static async Task<T> FetchChildAsync<T>()
{
Server.ChildDataPortal portal = new Server.ChildDataPortal();
return await portal.FetchAsync<T>();
return await portal.FetchAsync<T>(EmptyCriteria.Instance);
}

/// <summary>
Expand Down Expand Up @@ -752,6 +752,35 @@ public static void UpdateChild(object child, params object[] parameters)
portal.Update(child, parameters);
}

/// <summary>
/// Inserts, updates or deletes an existing
/// child business object.
/// </summary>
/// <param name="child">
/// Business object to update.
/// </param>
public static async Task UpdateChildAsync(object child)
{
Server.ChildDataPortal portal = new Server.ChildDataPortal();
await portal.UpdateAsync(child).ConfigureAwait(false);
}

/// <summary>
/// Inserts, updates or deletes an existing
/// child business object.
/// </summary>
/// <param name="child">
/// Business object to update.
/// </param>
/// <param name="parameters">
/// Parameters passed to child update method.
/// </param>
public static async Task UpdateChildAsync(object child, params object[] parameters)
{
Server.ChildDataPortal portal = new Server.ChildDataPortal();
await portal.UpdateAsync(child, parameters).ConfigureAwait(false);
}

private static DataPortalClient.IDataPortalProxyFactory _dataProxyFactory;

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion Source/Csla.Shared/DataPortalOperationAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Csla
/// via dependency injection.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter)]
public class FromServicesAttribute : Attribute
public class InjectAttribute : Attribute
{ }

/// <summary>
Expand Down Expand Up @@ -124,4 +124,13 @@ public class UpdateChildAttribute : Attribute
[AttributeUsage(AttributeTargets.Method)]
public class DeleteSelfChildAttribute : Attribute
{ }

/// <summary>
/// Specifies a method used by the server-side
/// data portal to execute a child command
/// object during an update operation.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class ExecuteChildAttribute : Attribute
{ }
}
4 changes: 2 additions & 2 deletions Source/Csla.Shared/DataPortalT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public DataPortalAsyncResult(T result, Csla.Core.ContextDictionary globalContext
}
}

private static object GetCriteriaFromArray(params object[] criteria)
internal static object GetCriteriaFromArray(params object[] criteria)
{
if (criteria == null)
return null;
Expand All @@ -82,7 +82,7 @@ private static object GetCriteriaFromArray(params object[] criteria)
return new Core.MobileList<object>(criteria);
}

private static object[] GetCriteriaArray(object criteria)
internal static object[] GetCriteriaArray(object criteria)
{
if (criteria == EmptyCriteria)
return null;
Expand Down
4 changes: 2 additions & 2 deletions Source/Csla.Shared/Reflection/ServiceProviderMethodCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static ParameterInfo[] GetCriteriaParameters(System.Reflection.MethodInf
var result = new List<ParameterInfo>();
foreach (var item in method.GetParameters())
{
if (item.CustomAttributes.Count(a => a.AttributeType == typeof(FromServicesAttribute)) == 0)
if (item.CustomAttributes.Count(a => a.AttributeType == typeof(InjectAttribute)) == 0)
result.Add(item);
}
return result.ToArray();
Expand Down Expand Up @@ -224,7 +224,7 @@ public static async Task<object> CallMethodTryAsync(object obj, System.Reflectio

foreach (var item in methodParameters)
{
if (item.CustomAttributes.Where(a => a.AttributeType == typeof(FromServicesAttribute)).Count() > 0)
if (item.CustomAttributes.Where(a => a.AttributeType == typeof(InjectAttribute)).Count() > 0)
{
if (service != null)
plist[index] = service.GetService(item.ParameterType);
Expand Down
173 changes: 60 additions & 113 deletions Source/Csla.Shared/Server/ChildDataPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public object Create(System.Type objectType, params object[] parameters)
/// </summary>
public async Task<T> CreateAsync<T>()
{
return (T) await Create(typeof(T), false, null).ConfigureAwait(false);
return (T) await Create(typeof(T), false, EmptyCriteria.Instance).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -59,11 +59,7 @@ public async Task<T> CreateAsync<T>(params object[] parameters)

private async Task<object> Create(System.Type objectType, bool hasParameters, params object[] parameters)
{
object criteria = EmptyCriteria.Instance;
if (hasParameters && parameters == null)
criteria = null;
else if (hasParameters)
criteria = parameters;
var criteria = DataPortal<object>.GetCriteriaFromArray(parameters);

DataPortalTarget obj = null;
var eventArgs = new DataPortalEventArgs(null, objectType, criteria, DataPortalOperations.Create);
Expand Down Expand Up @@ -131,7 +127,7 @@ public object Fetch(Type objectType, params object[] parameters)
/// <param name="objectType">Type of business object to retrieve.</param>
public async Task<T> FetchAsync<T>()
{
return (T)await Fetch(typeof(T), false, null).ConfigureAwait(false);
return (T)await Fetch(typeof(T), false, EmptyCriteria.Instance).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -148,11 +144,7 @@ public async Task<T> FetchAsync<T>(params object[] parameters)

private async Task<object> Fetch(Type objectType, bool hasParameters, params object[] parameters)
{
object criteria = EmptyCriteria.Instance;
if (hasParameters && parameters == null)
criteria = null;
else if (hasParameters)
criteria = parameters;
var criteria = DataPortal<object>.GetCriteriaFromArray(parameters);

DataPortalTarget obj = null;
var eventArgs = new DataPortalEventArgs(null, objectType, parameters, DataPortalOperations.Fetch);
Expand Down Expand Up @@ -197,7 +189,7 @@ private async Task<object> Fetch(Type objectType, bool hasParameters, params obj
/// <param name="obj">Business object to update.</param>
public void Update(object obj)
{
Update(obj, false, false, null);
Update(obj, false, false, null).Wait();
}

/// <summary>
Expand All @@ -209,7 +201,28 @@ public void Update(object obj)
/// </param>
public void Update(object obj, params object[] parameters)
{
Update(obj, true, false, parameters);
Update(obj, true, false, parameters).Wait();
}

/// <summary>
/// Update a business object.
/// </summary>
/// <param name="obj">Business object to update.</param>
public async Task UpdateAsync(object obj)
{
await Update(obj, false, false, null).ConfigureAwait(false);
}

/// <summary>
/// Update a business object.
/// </summary>
/// <param name="obj">Business object to update.</param>
/// <param name="parameters">
/// Parameters passed to method.
/// </param>
public async Task UpdateAsync(object obj, params object[] parameters)
{
await Update(obj, true, false, parameters).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -218,7 +231,7 @@ public void Update(object obj, params object[] parameters)
/// <param name="obj">Business object to update.</param>
public void UpdateAll(object obj)
{
Update(obj, false, true, null);
Update(obj, false, true, null).Wait();
}

/// <summary>
Expand All @@ -230,127 +243,61 @@ public void UpdateAll(object obj)
/// </param>
public void UpdateAll(object obj, params object[] parameters)
{
Update(obj, true, true, parameters);
Update(obj, true, true, parameters).Wait();
}

private void Update(object obj, bool hasParameters, bool bypassIsDirtyTest, params object[] parameters)
/// <summary>
/// Update a business object. Include objects which are not dirty.
/// </summary>
/// <param name="obj">Business object to update.</param>
public async Task UpdateAllAsync(object obj)
{
await Update(obj, false, true, null).ConfigureAwait(false);
}

/// <summary>
/// Update a business object. Include objects which are not dirty.
/// </summary>
/// <param name="obj">Business object to update.</param>
/// <param name="parameters">
/// Parameters passed to method.
/// </param>
public async Task UpdateAllAsync(object obj, params object[] parameters)
{
await Update(obj, true, true, parameters).ConfigureAwait(false);
}

private async Task Update(object obj, bool hasParameters, bool bypassIsDirtyTest, params object[] parameters)
{
if (obj == null)
return;

var busObj = obj as Core.BusinessBase;
if (busObj != null && busObj.IsDirty == false && bypassIsDirtyTest == false)
if (obj is Core.BusinessBase busObj && busObj.IsDirty == false && bypassIsDirtyTest == false)
{
// if the object isn't dirty, then just exit
return;
}

var criteria = DataPortal<object>.GetCriteriaFromArray(parameters);
var operation = DataPortalOperations.Update;
Type objectType = obj.GetType();
IDataPortalTarget target = obj as IDataPortalTarget;
LateBoundObject lb = new LateBoundObject(obj);
DataPortalTarget lb = new DataPortalTarget(obj);
ApplicationContext.DataPortalActivator.InitializeInstance(lb.Instance);

try
{
if (target != null)
target.Child_OnDataPortalInvoke(
new DataPortalEventArgs(null, objectType, obj, operation));
else
lb.CallMethodIfImplemented("Child_OnDataPortalInvoke",
lb.Child_OnDataPortalInvoke(
new DataPortalEventArgs(null, objectType, obj, operation));
await lb.UpdateChildAsync(criteria).ConfigureAwait(false);
lb.Child_OnDataPortalInvokeComplete(
new DataPortalEventArgs(null, objectType, obj, operation));

// tell the business object to update itself
if (busObj != null)
{
if (busObj.IsDeleted)
{
if (!busObj.IsNew)
{
// tell the object to delete itself
if (hasParameters)
lb.CallMethod("Child_DeleteSelf", parameters);
else
lb.CallMethod("Child_DeleteSelf");
}
if (target != null)
target.MarkNew();
else
lb.CallMethodIfImplemented("MarkNew");

}
else
{
if (busObj.IsNew)
{
// tell the object to insert itself
if (hasParameters)
lb.CallMethod("Child_Insert", parameters);
else
{
lb.CallMethod("Child_Insert");
}

}
else
{
// tell the object to update itself
if (hasParameters)
lb.CallMethod("Child_Update", parameters);
else
{
lb.CallMethod("Child_Update");
}
}
if (target != null)
target.MarkOld();
else
lb.CallMethodIfImplemented("MarkOld");
}

}
else if (obj is Core.ICommandObject)
{
// tell the object to update itself
if (hasParameters)
lb.CallMethod("Child_Execute", parameters);
else
lb.CallMethod("Child_Execute");
operation = DataPortalOperations.Execute;

}
else
{
// this is an updatable collection or some other
// non-BusinessBase type of object
// tell the object to update itself
if (hasParameters)
lb.CallMethod("Child_Update", parameters);
else
lb.CallMethod("Child_Update");
if (target != null)
target.MarkOld();
else
lb.CallMethodIfImplemented("MarkOld");
}

if (target != null)
target.Child_OnDataPortalInvokeComplete(
new DataPortalEventArgs(null, objectType, obj, operation));
else
lb.CallMethodIfImplemented("Child_OnDataPortalInvokeComplete",
new DataPortalEventArgs(null, objectType, obj, operation));

}
catch (Exception ex)
{
try
{
if (target != null)
target.Child_OnDataPortalException(
new DataPortalEventArgs(null, objectType, obj, operation), ex);
else if (lb != null)
lb.CallMethodIfImplemented("Child_OnDataPortalException",
if (lb != null)
lb.Child_OnDataPortalException(
new DataPortalEventArgs(null, objectType, obj, operation), ex);
}
catch
Expand Down
Loading

0 comments on commit f97e7f4

Please sign in to comment.