Skip to content

BusyHelper for IsBusy Change #3953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Source/Csla.Blazor.Test/PersonEdit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.ComponentModel.DataAnnotations;
using Csla;
using Csla.Rules;

namespace Csla.Blazor.Test
{
[Serializable]
public class PersonEdit : BusinessBase<PersonEdit>
{

public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[ObjectAuthorizationRules]
public static void AddObjectAuthorizationRules()
{
}

protected override void AddBusinessRules()
{
base.AddBusinessRules();
BusinessRules.AddRule(new DelayAsyncRule(NameProperty));
}

[Create]
private void Create()
{
base.Child_Create();
}

[Insert]
private void Insert()
{
}

private sealed class DelayAsyncRule : BusinessRuleAsync
{
public DelayAsyncRule(Csla.Core.IPropertyInfo primaryProperty) : base(primaryProperty)
{
}

/// <inheritdoc />
protected override async Task ExecuteAsync(IRuleContext context)
{
await Task.Delay(TimeSpan.FromSeconds(20)).ConfigureAwait(false);
}
}
}

[Serializable]
public class Person : BusinessBase<Person>
{

public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[ObjectAuthorizationRules]
public static void AddObjectAuthorizationRules()
{
}

protected override void AddBusinessRules()
{
base.AddBusinessRules();
BusinessRules.AddRule(new OverDelayAsyncRule(NameProperty));
}

[Create]
private void Create()
{
base.Child_Create();
}

private sealed class OverDelayAsyncRule : BusinessRuleAsync
{
public OverDelayAsyncRule(Csla.Core.IPropertyInfo primaryProperty) : base(primaryProperty)
{
}

/// <inheritdoc />
protected override async Task ExecuteAsync(IRuleContext context)
{
await Task.Delay(TimeSpan.FromSeconds(40)).ConfigureAwait(false);
}
}
}
}
45 changes: 45 additions & 0 deletions Source/Csla.Blazor.Test/ViewModelSaveAsyncErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ public async Task SavingSuccess_ErrorEventIsNotInvoked()
Assert.IsNull(vm.ViewModelErrorText);
}

[TestMethod]
public async Task SavingSuccess_BusyHelper()
{
// Arrange
IDataPortal<PersonEdit> dataPortal;
PersonEdit person;

// Create an instance of a DataPortal that can be used for instantiating objects
dataPortal = _testDIContext.CreateDataPortal<PersonEdit>();
person = dataPortal.Create();
person.Name = "TestTest";

var appCntxt = _testDIContext.CreateTestApplicationContext();
var vm = new ViewModel<PersonEdit>(appCntxt)
{
Model = person
};

await vm.SaveAsync();
Assert.IsNull(vm.Exception);
}

[TestMethod]
public async Task SavingFailure_ErrorTimeOut()
{
// Arrange
IDataPortal<Person> dataPortal;
Person person;

// Create an instance of a DataPortal that can be used for instantiating objects
dataPortal = _testDIContext.CreateDataPortal<Person>();
person = dataPortal.Create();
person.Name = "TestTest";

var appCntxt = _testDIContext.CreateTestApplicationContext();
var vm = new ViewModel<Person>(appCntxt)
{
Model = person
};

await vm.SaveAsync();
Assert.IsNotNull(vm.Exception);
Assert.AreEqual(vm.Exception.Message, "Csla.Blazor.Test.Person.SaveAsync - 00:00:30.");
}

#region Helper Methods

FakePerson GetValidFakePerson()
Expand Down
18 changes: 8 additions & 10 deletions Source/Csla.Blazor/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq.Expressions;
using Csla.Reflection;
using Csla.Rules;
using Csla.Core;

namespace Csla.Blazor
{
Expand Down Expand Up @@ -236,16 +237,8 @@ public async Task SaveAsync()
{
if (Model is Core.ITrackStatus obj && !obj.IsSavable)
{
if (obj.IsBusy)
{
var stopTime = DateTime.Now + BusyTimeout;
while (obj.IsBusy)
{
if (DateTime.Now > stopTime)
throw new TimeoutException("SaveAsync");
await Task.Delay(1);
}
}
await BusyHelper.WaitForIdle(obj, BusyTimeout).ConfigureAwait(false);

if (!obj.IsValid)
{
ViewModelErrorText = ModelErrorText;
Expand Down Expand Up @@ -279,6 +272,11 @@ public async Task SaveAsync()
Exception = ex;
ViewModelErrorText = ex.BusinessExceptionMessage;
}
catch (TimeoutException ex)
{
Exception = ex;
ViewModelErrorText = ex.Message;
}
catch (Exception ex)
{
Exception = ex;
Expand Down
5 changes: 4 additions & 1 deletion Source/Csla/Core/BusyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ namespace Csla.Core {
/// <summary>
/// Helper class for busy related functionality spread across different business type implementations.
/// </summary>
internal static class BusyHelper
public static class BusyHelper
{
/// <summary>
/// Helper class method for busy related functionality spread across different business type implementations.
/// </summary>
public static async Task WaitForIdle(INotifyBusy source, TimeSpan timeout, [CallerMemberName] string methodName = "")
{
if (!source.IsBusy)
Expand Down
Loading