Skip to content

Commit

Permalink
Added AddNewAsync() methods to lists with AddNew which are using IObs…
Browse files Browse the repository at this point in the history
…ervableBindingList (MarimerLLC#3989)

Co-authored-by: Rockford Lhotka <rocky@lhotka.net>
  • Loading branch information
Freelancingonupwork and rockfordlhotka authored May 30, 2024
1 parent 14911b9 commit 2151a19
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Source/Csla.test/BusinessListBase/AddNewAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//-----------------------------------------------------------------------
// <copyright file="AddNewAsyncTests.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using Csla.TestHelpers;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Csla.Test.BusinessListBase
{
[TestClass]
public class AddNewAsyncTests
{
private static TestDIContext _testDIContext;

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_testDIContext = TestDIContextFactory.CreateDefaultContext();
}

[TestMethod]
public async Task RootAddNewCoreAsync()
{
bool changed = false;
var obj = CreateRootList();
obj.CollectionChanged += (_, _) =>
{
changed = true;
};
var child = await obj.AddNewAsync();
Assert.IsTrue(changed);
Assert.AreEqual(child, obj[0]);
}

[TestMethod]
public async Task ChildAddNewCoreAsync()
{
bool childChanged = false;
bool changed = false;
var obj = CreateRoot();
obj.ChildChanged += (_, _) =>
{
childChanged = true;
};
obj.Children.CollectionChanged += (_, _) =>
{
changed = true;
};
var child = await obj.Children.AddNewAsync();
Assert.IsTrue(childChanged, "ChildChanged should be true");
Assert.IsTrue(changed, "Collection changed should be true");
Assert.AreEqual(child, obj.Children[0]);
}

private Root CreateRoot()
{
IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();

return dataPortal.Create();
}

private RootList CreateRootList()
{
IDataPortal<RootList> dataPortal = _testDIContext.CreateDataPortal<RootList>();

return dataPortal.Create();
}
}
}
12 changes: 12 additions & 0 deletions Source/Csla/BusinessListBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ protected override C AddNewCore()
return item;
}

/// <summary>
/// Override this method to create a new object that is added
/// to the collection.
/// </summary>
protected override async Task<C> AddNewCoreAsync()
{
var dp = ApplicationContext.CreateInstanceDI<DataPortal<C>>();
var item = await dp.CreateChildAsync();
Add(item);
return item;
}

/// <summary>
/// This method is called by a child object when it
/// wants to be removed from the collection.
Expand Down
4 changes: 4 additions & 0 deletions Source/Csla/Core/IObservableBindingList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ public interface IObservableBindingList
/// removed from the list.
/// </summary>
event EventHandler<RemovingItemEventArgs> RemovingItem;
/// <summary>
/// Creates and adds a new item to the collection.
/// </summary>
Task<object> AddNewAsync();
}
}
43 changes: 43 additions & 0 deletions Source/Csla/Core/ObservableBindingList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ object IObservableBindingList.AddNew()
return AddNew();
}

/// <summary>
/// Adds a new item to this collection.
/// </summary>
public async Task<T> AddNewAsync()
{
var result = await AddNewCoreAsync();
await OnAddedNewAsync(result);
return result;
}

async Task<object> IObservableBindingList.AddNewAsync()
{
return await AddNewAsync();
}

#endregion

#region RemovingItem event
Expand Down Expand Up @@ -493,6 +508,25 @@ public virtual void OnAddedNew(T item)
}
}

/// <summary>
/// Raises the AddedNew event.
/// </summary>
/// <param name="item"></param>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public async virtual Task OnAddedNewAsync(T item)
{
if (_addedNewHandlers != null)
{
var args = new AddedNewEventArgs<T>(item);
var handlerTasks = _addedNewHandlers
.GetInvocationList()
.Cast<EventHandler<AddedNewEventArgs<T>>>()
.Select(handler => Task.Run(() => handler(this, args)));

await Task.WhenAll(handlerTasks);
}
}

#if ANDROID || IOS
/// <summary>
/// Override this method to create a new object that is added
Expand All @@ -511,6 +545,15 @@ protected virtual T AddNewCore()
{
throw new NotImplementedException(Resources.AddNewCoreMustBeOverriden);
}

/// <summary>
/// Override this method to create a new object that is added
/// to the collection.
/// </summary>
protected virtual async Task<T> AddNewCoreAsync()
{
return await Task.FromException<T>(new NotImplementedException(Resources.AddNewCoreMustBeOverriden));
}
#endif

#endregion
Expand Down
12 changes: 12 additions & 0 deletions Source/Csla/DynamicListBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ protected override T AddNewCore()
return item;
}

/// <summary>
/// Adds a new item to the list.
/// </summary>
/// <returns>The added object</returns>
protected override async Task<T> AddNewCoreAsync()
{
var dp = ApplicationContext.CreateInstanceDI<DataPortal<T>>();
T item = await dp.CreateAsync();
Add(item);
return item;
}

/// <summary>
/// Gives the new object a parent reference to this
/// list.
Expand Down

0 comments on commit 2151a19

Please sign in to comment.