Skip to content

Commit 1787258

Browse files
Merge pull request #193 from TimeWarpEngineering/Cramer/2019-10-16/bug/InitializeAfterConstructor
Initialize is called by the Store after construction complete.
2 parents c3d11a8 + aa68563 commit 1787258

10 files changed

Lines changed: 30 additions & 13 deletions

File tree

Source/BlazorState/Features/Routing/State/RouteState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public partial class RouteState : State<RouteState>
99
{
1010
public string Route { get; private set; }
1111

12-
protected override void Initialize() { }
12+
public override void Initialize() { }
1313
}
1414
}

Source/BlazorState/State/IState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public interface IState
77
{
88
Guid Guid { get; }
9+
void Initialize();
910
}
1011

1112
public interface IState<TState> : IState

Source/BlazorState/State/State.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
public abstract class State<TState> : IState<TState>
99
{
10-
public State()
11-
{
12-
Initialize();
13-
}
1410

1511
[IgnoreDataMember]
1612
public Guid Guid { get; protected set; } = Guid.NewGuid();
@@ -40,7 +36,6 @@ public void ThrowIfNotTestAssembly(Assembly aAssembly)
4036
/// <summary>
4137
/// Override this to Set the initial state
4238
/// </summary>
43-
protected abstract void Initialize();
44-
39+
public abstract void Initialize();
4540
}
4641
}

Source/BlazorState/Store/Store.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public object GetState(Type aType)
7676
Logger.LogDebug($"{GetType().Name}: Creating State of type: {typeName}");
7777
state = (IState)ServiceProvider.GetService(aType);
7878
if (state == null) throw new NullReferenceException("state is null");
79-
//state = (IState)Activator.CreateInstance(aType);
79+
state.Initialize();
8080
States.Add(typeName, state);
8181
}
8282
else

Tests/Client.Integration.Tests/Store/StoreTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,14 @@ public void ShouldLoadStatesFromJson()
4949
weatherForecastsState.WeatherForecasts[0].Date.Minute.ShouldBe(29);
5050
weatherForecastsState.WeatherForecasts[0].Date.Second.ShouldBe(54);
5151
}
52+
53+
/// <summary>
54+
/// WeatherForecatesState will throw an exception if items initialized in the constructor are null.
55+
/// </summary>
56+
public void ShouldInitializeStateAfterConstruction()
57+
{
58+
WeatherForecastsState state = Store.GetState<WeatherForecastsState>();
59+
state.ShouldNotBeNull();
60+
}
5261
}
5362
}

Tests/TestApp/Client/Features/Application/ApplicationState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ internal partial class ApplicationState : State<ApplicationState>
1010

1111
public ApplicationState() { }
1212

13-
protected override void Initialize() => Name = "Blazor State Demo Application";
13+
public override void Initialize() => Name = "Blazor State Demo Application";
1414
}
1515
}

Tests/TestApp/Client/Features/CloneTest/CloneTestState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CloneTestState() { }
1313
/// <summary>
1414
/// Set the Initial State
1515
/// </summary>
16-
protected override void Initialize() => Count = 3;
16+
public override void Initialize() => Count = 3;
1717
public object Clone() => new CloneTestState { Count = 42 };
1818
}
1919
}

Tests/TestApp/Client/Features/Counter/CounterState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public CounterState() { }
1212
/// <summary>
1313
/// Set the Initial State
1414
/// </summary>
15-
protected override void Initialize() => Count = 3;
15+
public override void Initialize() => Count = 3;
1616
}
1717
}

Tests/TestApp/Client/Features/EventStream/EventStreamState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public EventStreamState()
1313
_Events = new List<string>();
1414
}
1515

16-
protected override void Initialize() { }
16+
public override void Initialize() { }
1717
}
1818
}

Tests/TestApp/Client/Features/WeatherForecast/WeatherForecastState.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public WeatherForecastsState()
1515
_WeatherForecasts = new List<WeatherForecastDto>();
1616
}
1717

18-
protected override void Initialize() { }
18+
19+
/// <summary>
20+
///
21+
/// </summary>
22+
/// <remarks>used to test that constructor is complete before Initialize is called</remarks>
23+
public override void Initialize()
24+
{
25+
if (_WeatherForecasts is null)
26+
{
27+
throw new System.ArgumentNullException(nameof(_WeatherForecasts));
28+
}
29+
30+
}
1931
}
2032
}

0 commit comments

Comments
 (0)