Skip to content

Commit

Permalink
Update Policies
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenTCramer committed Aug 3, 2024
1 parent 5cf1557 commit 23dd6c9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 35 deletions.
37 changes: 37 additions & 0 deletions Source/TimeWarp.State.Rules/Policies.ActionHandlerPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace TimeWarp.State.Rules;

public static partial class Policies
{
public static PolicyDefinition CreateActionHandlerPolicy(params Assembly[] assemblies)
{
BeNestedInStateCustomRule beNestedInState = new();
return Policy.Define("TimeWarp Action Handler Policy", "See https://timewarpengineering.github.io/timewarp-architecture/")
.For(Types.InAssemblies(assemblies))
.Add
(
t => t
.That()
.Inherit(typeof(ActionHandler<>))
.And()
.AreNotAbstract()
.Should()
.MeetCustomRule(beNestedInState),
"Nest Handlers",
"Action Handlers must be nested in the State they act upon."
)
.Add
(
t => t
.That()
.Inherit(typeof(ActionHandler<>))
.And()
.AreNotAbstract()
.Should()
.BeSealed()
.And()
.BeInternal(),
"internal sealed Handler",
"Handler should be `internal sealed`."
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace TimeWarp.State.Rules;

public static class Policies
public static partial class Policies
{

// TODO: Action and Handler should be in the same file which should be named `<State>.<MethodName>.cs` and located in a `Actions` folder.


public static PolicyDefinition CreateActionPolicy(params Assembly[] assemblies)
{
BeNestedInStateCustomRule beNestedInState = new();
Expand Down Expand Up @@ -56,37 +55,4 @@ public static PolicyDefinition CreateActionPolicy(params Assembly[] assemblies)
"Actions should be `internal sealed`."
);
}

public static PolicyDefinition CreateActionHandlerPolicy(params Assembly[] assemblies)
{
BeNestedInStateCustomRule beNestedInState = new();
return Policy.Define("TimeWarp Action Handler Policy", "See https://timewarpengineering.github.io/timewarp-architecture/")
.For(Types.InAssemblies(assemblies))
.Add
(
t => t
.That()
.Inherit(typeof(ActionHandler<>))
.And()
.AreNotAbstract()
.Should()
.MeetCustomRule(beNestedInState),
"Nest Handlers",
"Action Handlers must be nested in the State they act upon."
)
.Add
(
t => t
.That()
.Inherit(typeof(ActionHandler<>))
.And()
.AreNotAbstract()
.Should()
.BeSealed()
.And()
.BeInternal(),
"internal sealed Handler",
"Handler should be `internal sealed`."
);
}
}
20 changes: 20 additions & 0 deletions Source/TimeWarp.State.Rules/Policies.ActionSetPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace TimeWarp.State.Rules;

public static partial class Policies
{
public static PolicyDefinition CreateActionSetPolicy(params Assembly[] assemblies)
{
return Policy.Define("TimeWarp Action Set Policy", "See https://timewarpengineering.github.io/timewarp-architecture/")
.For(Types.InAssemblies(assemblies))
.Add
(
t => t
.That()
.HaveNameEndingWith("ActionSet")
.Should()
.BeInternal(),
"internal ActionSet",
"ActionSets should be internal. They can still be registered with DI, but they are not intended to be called directly. The associated method will be exposed on the State."
);
}
}
46 changes: 46 additions & 0 deletions Source/TimeWarp.State.Rules/Policies.StatePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace TimeWarp.State.Rules;

public static partial class Policies
{
public static PolicyDefinition CreateStatePolicy(params Assembly[] assemblies)
{
return Policy.Define("TimeWarp State Policy", "See https://timewarpengineering.github.io/timewarp-architecture/")
.For(Types.InAssemblies(assemblies))
.Add
(
t => t
.That()
.Inherit(typeof(State<>))
.And()
.AreNotAbstract()
.Should()
.HaveNameEndingWith("State"),
"suffix State",
"State filenames should be suffixed with `State`."
)
.Add
(
t => t
.That()
.Inherit(typeof(State<>))
.And()
.AreNotAbstract()
.Should()
.BePublic(),
"public State",
"States should be public. This allows for them to be packaged in separate assemblies. And their methods can be called from other assemblies."
)
.Add
(
t => t
.That()
.Inherit(typeof(State<>))
.And()
.AreNotAbstract()
.Should()
.BeSealed(),
"sealed State",
"States should be sealed. If they were to be extended, they should be made abstract."
);
}
}

0 comments on commit 23dd6c9

Please sign in to comment.