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 1 commit
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
89 changes: 89 additions & 0 deletions Source/Csla.Blazor.Test/PersonEdit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.ComponentModel.DataAnnotations;
using Csla;
using Csla.Rules;

namespace Csla.Blazor.Test
{
[Serializable]
public class PersonEdit : BusinessBase<PersonEdit>
{
public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(nameof(Id));
public int Id
{
get { return GetProperty(IdProperty); }
set { SetProperty(IdProperty, value); }
}

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

public static readonly PropertyInfo<int> NameLengthProperty = RegisterProperty<int>(nameof(NameLength));
public int NameLength
{
get => GetProperty(NameLengthProperty);
set => SetProperty(NameLengthProperty, value);
}

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[ObjectAuthorizationRules]
public static void AddObjectAuthorizationRules()
{
//Csla.Rules.BusinessRules.AddRule(typeof(PersonEdit),
// new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.CreateObject, "Admin"));
//Csla.Rules.BusinessRules.AddRule(typeof(PersonEdit),
// new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.EditObject, "Admin"));
//Csla.Rules.BusinessRules.AddRule(typeof(PersonEdit),
// new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.DeleteObject, "Admin"));
}

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

[Create]
private void Create()
{
Id = -1;
base.Child_Create();
}

public class CheckCase : BusinessRule
{
public CheckCase(Csla.Core.IPropertyInfo primaryProperty)
: base(primaryProperty)
{ }

protected override void Execute(IRuleContext context)
{
var text = (string)ReadProperty(context.Target, PrimaryProperty);
if (string.IsNullOrWhiteSpace(text)) return;
var ideal = text.Substring(0, 1).ToUpper();
ideal += text.Substring(1).ToLower();
if (text != ideal)
context.AddErrorResult("Check capitalization");
}
}

public class NoZAllowed : BusinessRule
{
public NoZAllowed(Csla.Core.IPropertyInfo primaryProperty)
: base(primaryProperty)
{ }

protected override void Execute(IRuleContext context)
{
var text = (string)ReadProperty(context.Target, PrimaryProperty);
if (text.ToLower().Contains("z"))
context.AddErrorResult("No letter Z allowed");
}
}

}
}
24 changes: 24 additions & 0 deletions Source/Csla.Blazor.Test/ViewModelSaveAsyncErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ public async Task SavingSuccess_ErrorEventIsNotInvoked()
Assert.IsNull(vm.ViewModelErrorText);
}

[TestMethod]
public async Task CheckBusyHelper()
{
// 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 = "z";

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

// Act
await vm.SaveAsync();
Assert.IsNotNull(vm.ViewModelErrorText);
}

#region Helper Methods

FakePerson GetValidFakePerson()
Expand Down
13 changes: 5 additions & 8 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,15 +237,11 @@ public async Task SaveAsync()
{
if (Model is Core.ITrackStatus obj && !obj.IsSavable)
{
if (obj.IsBusy)
if (obj is INotifyBusy notifyBusy)
{
var stopTime = DateTime.Now + BusyTimeout;
while (obj.IsBusy)
{
if (DateTime.Now > stopTime)
throw new TimeoutException("SaveAsync");
await Task.Delay(1);
}
var cslaOptions = ApplicationContext.GetRequiredService<Csla.Configuration.CslaOptions>();
var _waitForIdleTimeout = TimeSpan.FromSeconds(cslaOptions.DefaultWaitForIdleTimeoutInSeconds);
await BusyHelper.WaitForIdle(notifyBusy, _waitForIdleTimeout, "SaveAsync").ConfigureAwait(false);
}
if (!obj.IsValid)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Csla/Core/BusyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
/// <summary>
/// Helper class for busy related functionality spread across different business type implementations.
/// </summary>
internal static class BusyHelper
public static class BusyHelper
{
public static async Task WaitForIdle(INotifyBusy source, TimeSpan timeout, [CallerMemberName] string methodName = "")

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'

Check warning on line 17 in Source/Csla/Core/BusyHelper.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BusyHelper.WaitForIdle(INotifyBusy, TimeSpan, string)'
{
if (!source.IsBusy)
{
Expand Down
Loading