Skip to content

Commit

Permalink
Merge pull request #1172 from j055/feature/update-all-children
Browse files Browse the repository at this point in the history
Added UpdateAllChildren to FieldManager
  • Loading branch information
rockfordlhotka committed May 31, 2019
2 parents 666b502 + 8bb020f commit 6e3dc99
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Source/Csla.Shared/Core/FieldManager/FieldDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,25 @@ public void UpdateChildren(params object[] parameters)
}
}

/// <summary>
/// Invokes the data portal to update
/// all child objects, including those which are not dirty,
/// contained in the list of fields.
/// </summary>
public void UpdateAllChildren(params object[] parameters)
{
Server.ChildDataPortal portal = new Server.ChildDataPortal();
foreach (var item in _fieldData)
{
if (item != null)
{
object obj = item.Value;
if (obj is IEditableBusinessObject || obj is IEditableCollection)
portal.UpdateAll(obj, parameters);
}
}
}

#endregion

#if (ANDROID || IOS) || NETFX_CORE || NETSTANDARD2_0
Expand Down
29 changes: 25 additions & 4 deletions Source/Csla.Shared/Server/ChildDataPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private object Fetch(Type objectType, bool hasParameters, params object[] parame
/// <param name="obj">Business object to update.</param>
public void Update(object obj)
{
Update(obj, false, null);
Update(obj, false, false, null);
}

/// <summary>
Expand All @@ -216,16 +216,37 @@ public void Update(object obj)
/// </param>
public void Update(object obj, params object[] parameters)
{
Update(obj, true, parameters);
Update(obj, true, false, parameters);
}

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

/// <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 void UpdateAll(object obj, params object[] parameters)
{
Update(obj, true, true, parameters);
}

private void 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)
if (busObj != null && busObj.IsDirty == false && bypassIsDirtyTest == false)
{
// if the object isn't dirty, then just exit
return;
Expand Down
81 changes: 81 additions & 0 deletions Source/Csla.test/FieldManager/Child.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//-----------------------------------------------------------------------
// <copyright file="Child.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using System;

namespace Csla.Test.FieldManager
{
[Serializable]
public class Child : BusinessBase<Child>
{
public static Child NewChild()
{
return Csla.DataPortal.CreateChild<Child>();
}

public static Child GetChild()
{
return Csla.DataPortal.FetchChild<Child>();
}

private Child()
{
MarkAsChild();
}

private static PropertyInfo<string> DataProperty = RegisterProperty<string>(typeof(Child), new PropertyInfo<string>("Data"));
public string Data
{
get { return GetProperty<string>(DataProperty); }
set { SetProperty<string>(DataProperty, value); }
}

private static PropertyInfo<string> RootDataProperty = RegisterProperty<string>(typeof(Child), new PropertyInfo<string>("RootData", string.Empty));
public string RootData
{
get { return GetProperty<string>(RootDataProperty); }
set { SetProperty<string>(RootDataProperty, value); }
}

private string _status;
public string Status
{
get { return _status; }
}

public void DeleteChild()
{
MarkDeleted();
}

protected override void Child_Create()
{
_status = "Created";
}

protected void Child_Fetch()
{
_status = "Fetched";
}

protected void Child_Insert()
{
_status = "Inserted";
}

protected void Child_Update()
{
_status = "Updated";
}

protected void Child_DeleteSelf()
{
_status = "Deleted";
}
}
}
48 changes: 48 additions & 0 deletions Source/Csla.test/FieldManager/ChildList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//-----------------------------------------------------------------------
// <copyright file="ChildList.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using System;

namespace Csla.Test.FieldManager
{
[Serializable]
public class ChildList : BusinessBindingListBase<ChildList, Child>
{
public static ChildList GetList()
{
return Csla.DataPortal.FetchChild<ChildList>();
}

private ChildList()
{
MarkAsChild();
}

public object MyParent
{
get { return this.Parent; }
}

private string _status;
public string Status
{
get { return _status; }
}

protected void Child_Fetch()
{
_status = "Fetched";
}

protected override void Child_Update(params object[] p)
{
base.Child_Update();
_status = "Updated";
}
}
}
54 changes: 54 additions & 0 deletions Source/Csla.test/FieldManager/ChildUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//-----------------------------------------------------------------------
// <copyright file="DataPortalChildTests.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using Microsoft.VisualStudio.TestTools.UnitTesting;
#if !NUNIT

#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace Csla.Test.FieldManager
{
[TestClass()]
public class ChildUpdateTests
{
[TestMethod]
public void FetchAndSaveChild()
{
Root root = new Root();
root.FetchChild();

Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
Assert.AreEqual("Fetched", root.Child.Status, "Child status incorrect after fetch");

root = root.Save();

Assert.AreEqual("Fetched", root.Child.Status, "Child status incorrect after Save");
}

[TestMethod]
public void FetchAndSaveAnyChild()
{
var root = new RootUpdateAllChildren();
root.FetchChild();

Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
Assert.AreEqual("Fetched", root.Child.Status, "Child status incorrect after fetch");

root = root.Save();

Assert.AreEqual("Updated", root.Child.Status, "Child status incorrect after Save");
}

}
}
60 changes: 60 additions & 0 deletions Source/Csla.test/FieldManager/Root.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//-----------------------------------------------------------------------
// <copyright file="Root.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using System;

namespace Csla.Test.FieldManager
{
[Serializable]
public class Root : BusinessBase<Root>
{
private static PropertyInfo<string> DataProperty = RegisterProperty<string>(typeof(Root), new PropertyInfo<string>("Data"));
public string Data
{
get { return GetProperty<string>(DataProperty); }
set { SetProperty<string>(DataProperty, value); }
}

private static PropertyInfo<Child> ChildProperty = RegisterProperty<Child>(typeof(Root), new PropertyInfo<Child>("Child"));
public Child Child
{
get
{
if (!FieldManager.FieldExists(ChildProperty))
SetProperty<Child>(ChildProperty, Child.NewChild());
return GetProperty<Child>(ChildProperty);
}
}

private static PropertyInfo<ChildList> ChildListProperty = RegisterProperty<ChildList>(typeof(Root), new PropertyInfo<ChildList>("ChildList"));
public ChildList ChildList
{
get
{
if (!FieldManager.FieldExists(ChildListProperty))
SetProperty<ChildList>(ChildListProperty, ChildList.GetList());
return GetProperty<ChildList>(ChildListProperty);
}
}

public void FetchChild()
{
SetProperty<Child>(ChildProperty, Child.GetChild());
}

protected override void DataPortal_Insert()
{
FieldManager.UpdateChildren();
}

protected override void DataPortal_Update()
{
FieldManager.UpdateChildren();
}
}
}
52 changes: 52 additions & 0 deletions Source/Csla.test/FieldManager/RootUpdateAllChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;

namespace Csla.Test.FieldManager
{
[Serializable]
public class RootUpdateAllChildren : BusinessBase<RootUpdateAllChildren>
{
private static PropertyInfo<string> DataProperty = RegisterProperty<string>(typeof(RootUpdateAllChildren), new PropertyInfo<string>("Data"));
public string Data
{
get { return GetProperty<string>(DataProperty); }
set { SetProperty<string>(DataProperty, value); }
}

private static PropertyInfo<Child> ChildProperty = RegisterProperty<Child>(typeof(RootUpdateAllChildren), new PropertyInfo<Child>("Child"));
public Child Child
{
get
{
if (!FieldManager.FieldExists(ChildProperty))
SetProperty<Child>(ChildProperty, Child.NewChild());
return GetProperty<Child>(ChildProperty);
}
}

private static PropertyInfo<ChildList> ChildListProperty = RegisterProperty<ChildList>(typeof(RootUpdateAllChildren), new PropertyInfo<ChildList>("ChildList"));
public ChildList ChildList
{
get
{
if (!FieldManager.FieldExists(ChildListProperty))
SetProperty<ChildList>(ChildListProperty, ChildList.GetList());
return GetProperty<ChildList>(ChildListProperty);
}
}

public void FetchChild()
{
SetProperty<Child>(ChildProperty, Child.GetChild());
}

protected override void DataPortal_Insert()
{
FieldManager.UpdateAllChildren();
}

protected override void DataPortal_Update()
{
FieldManager.UpdateAllChildren();
}
}
}
5 changes: 5 additions & 0 deletions Source/Csla.test/csla.test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@
<Compile Include="EditableRootList\EditableRootListTests.cs" />
<Compile Include="EditableRootList\ERitem.cs" />
<Compile Include="EditableRootList\ERlist.cs" />
<Compile Include="FieldManager\Child.cs" />
<Compile Include="FieldManager\ChildList.cs" />
<Compile Include="FieldManager\ChildUpdateTests.cs" />
<Compile Include="FieldManager\Root.cs" />
<Compile Include="FieldManager\RootUpdateAllChildren.cs" />
<Compile Include="GraphMerge\GraphMergerTests.cs" />
<Compile Include="GraphMerge\IdentityTests.cs" />
<Compile Include="GraphMerge\TestClasses.cs" />
Expand Down

0 comments on commit 6e3dc99

Please sign in to comment.