diff --git a/src/TheFlow/Conventions/ProcessModelConventions.cs b/src/TheFlow/Conventions/ProcessModelConventions.cs new file mode 100644 index 0000000..ca84782 --- /dev/null +++ b/src/TheFlow/Conventions/ProcessModelConventions.cs @@ -0,0 +1,8 @@ +namespace TheFlow.Conventions +{ + public class ProcessModelConventions + { + public ProcessModelNamingConventions Naming { get; } + = new ProcessModelNamingConventions(); + } +} diff --git a/src/TheFlow/Conventions/ProcessModelNamingConventions.cs b/src/TheFlow/Conventions/ProcessModelNamingConventions.cs new file mode 100644 index 0000000..cde51ad --- /dev/null +++ b/src/TheFlow/Conventions/ProcessModelNamingConventions.cs @@ -0,0 +1,27 @@ +using System; + +namespace TheFlow.Conventions +{ + public class ProcessModelNamingConventions + { + public Func ActivityName { get; set; } = input => + input.EndsWith("Activity") + ? input.Substring(0, input.Length - "Activity".Length) + : input; + + public Func DataObjectName { get; set; } = input => + (input.StartsWith("On") || input.StartsWith("on")) && char.IsUpper(input[2]) + ? input.Substring(2) + : input; + + public Func EventCatcher { get; set; } = input => + (input.StartsWith("On") || input.StartsWith("on")) && char.IsUpper(input[2]) + ? input + : $"On{input}"; + + public Func EventThrower { get; set; } = input => + input.EndsWith("EventThrower") + ? input.Substring(0, input.Length - "EventThrower".Length) + : input; + } +} \ No newline at end of file diff --git a/src/TheFlow/CoreConcepts/Name/DataObjectName.cs b/src/TheFlow/CoreConcepts/Name/DataObjectName.cs deleted file mode 100644 index 16f708c..0000000 --- a/src/TheFlow/CoreConcepts/Name/DataObjectName.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace TheFlow.CoreConcepts.Names -{ - public struct DataObjectName - { - private readonly string _name; - - private DataObjectName(string name) - { - _name = name; - } - - public static implicit operator DataObjectName(string input) - { - var dataObjectName = input; - if ((dataObjectName.StartsWith("On") || dataObjectName.StartsWith("on")) && - char.IsUpper(dataObjectName[2])) - - { - dataObjectName = dataObjectName.Substring(2); - } - - return new DataObjectName(dataObjectName); - } - - public override string ToString() - { - return _name; - } - } -} \ No newline at end of file diff --git a/src/TheFlow/CoreConcepts/ProcessInstance.cs b/src/TheFlow/CoreConcepts/ProcessInstance.cs index 91d0ea1..5275cb5 100644 --- a/src/TheFlow/CoreConcepts/ProcessInstance.cs +++ b/src/TheFlow/CoreConcepts/ProcessInstance.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using TheFlow.CoreConcepts.Names; namespace TheFlow.CoreConcepts { @@ -27,14 +26,14 @@ public void SetDataInputValue( _elementsState[key] = value; } - public void SetDataObjectValue(DataObjectName dataObjectName, object value) + public void SetDataObjectValue(string dataObjectName, object value) { - DataObjectsValues[dataObjectName.ToString()] = value; + DataObjectsValues[dataObjectName] = value; } - public object GetDataObjectValue(DataObjectName dataObjectName) + public object GetDataObjectValue(string dataObjectName) { - return DataObjectsValues[dataObjectName.ToString()]; + return DataObjectsValues[dataObjectName]; } } } \ No newline at end of file diff --git a/src/TheFlow/CoreConcepts/ProcessModel.Add.cs b/src/TheFlow/CoreConcepts/ProcessModel.Add.cs index b16b8f3..6e1c668 100644 --- a/src/TheFlow/CoreConcepts/ProcessModel.Add.cs +++ b/src/TheFlow/CoreConcepts/ProcessModel.Add.cs @@ -21,7 +21,7 @@ public ProcessModel AddEventCatcher(string name, IEventCatcher catcher) public ProcessModel AddEventCatcher() where TEvent : class - => AddEventCatcher($"On{typeof(TEvent).Name}"); + => AddEventCatcher(Conventions.Naming.EventCatcher(typeof(TEvent).Name)); public ProcessModel AddEventCatcher(string name) where TEvent : class @@ -104,14 +104,8 @@ public ProcessModel AddActivity(NamedProcessElement activity) public ProcessModel AddActivity() where TActivity : Activity { - var name = typeof(TActivity).Name; - if (name.EndsWith("Activity")) - { - name = name.Substring(0, name.Length - "Activity".Length); - } - var activity = Activator.CreateInstance(); - return AddActivity(name, activity); + return AddActivity(Conventions.Naming.ActivityName(typeof(TActivity).Name), activity); } public ProcessModel AddParallelGateway(string name) diff --git a/src/TheFlow/CoreConcepts/ProcessModel.cs b/src/TheFlow/CoreConcepts/ProcessModel.cs index 1df1bf8..fcbd22c 100644 --- a/src/TheFlow/CoreConcepts/ProcessModel.cs +++ b/src/TheFlow/CoreConcepts/ProcessModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using TheFlow.Conventions; using TheFlow.Elements; using TheFlow.Elements.Activities; using TheFlow.Elements.Connections; @@ -17,6 +18,9 @@ public partial class ProcessModel : IProcessModelProvider public ImmutableList> Elements { get; } public ImmutableList Associations { get; } + public ProcessModelConventions Conventions { get; } + = new ProcessModelConventions(); + #region Constructor and Empty Object public ProcessModel( string id, diff --git a/src/TheFlow/Elements/Events/CatchAnyEventCatcher.cs b/src/TheFlow/Elements/Events/CatchAnyEventCatcher.cs index a4b5ddf..f1d8f34 100644 --- a/src/TheFlow/Elements/Events/CatchAnyEventCatcher.cs +++ b/src/TheFlow/Elements/Events/CatchAnyEventCatcher.cs @@ -17,7 +17,10 @@ public static IEventCatcher Create() public void Handle(ExecutionContext context, object @event) { - context.Instance.SetDataObjectValue(context.Token.ExecutionPoint, @event); + context.Instance.SetDataObjectValue( + context.Model.Conventions.Naming.DataObjectName(context.Token.ExecutionPoint), + @event + ); _dataOutput?.Update(context, context.Token.ExecutionPoint, @event); } diff --git a/src/TheFlow/Elements/Events/TypedEventCatcher.cs b/src/TheFlow/Elements/Events/TypedEventCatcher.cs index aadb3aa..2adf7a4 100644 --- a/src/TheFlow/Elements/Events/TypedEventCatcher.cs +++ b/src/TheFlow/Elements/Events/TypedEventCatcher.cs @@ -1,5 +1,4 @@ -using System; -using TheFlow.CoreConcepts; +using TheFlow.CoreConcepts; using TheFlow.Elements.Data; namespace TheFlow.Elements.Events @@ -13,7 +12,9 @@ bool IEventCatcher.CanHandle(ExecutionContext context, object @event) => void IEventCatcher.Handle(ExecutionContext context, object @event) { - context.Instance.SetDataObjectValue(context.Token.ExecutionPoint, @event); + context.Instance.SetDataObjectValue( + context.Model.Conventions.Naming.DataObjectName(context.Token.ExecutionPoint), + @event); _dataOutput?.Update(context, context.Token.ExecutionPoint, @event); HandleImpl(context, @event as TEvent); @@ -30,7 +31,10 @@ public bool CanHandle(ExecutionContext context,TEvent @event) => public void Handle(ExecutionContext context, TEvent @event) { - context.Instance.SetDataObjectValue(context.Token.ExecutionPoint, @event); + context.Instance.SetDataObjectValue( + context.Model.Conventions.Naming.DataObjectName(context.Token.ExecutionPoint), + @event + ); _dataOutput?.Update(context, context.Token.ExecutionPoint, @event); HandleImpl(context, @event); diff --git a/test/TheFlow.Tests/Unit/DataAssociationShould.cs b/test/TheFlow.Tests/Unit/DataAssociationShould.cs index 4db7438..6d6c8a0 100644 --- a/test/TheFlow.Tests/Unit/DataAssociationShould.cs +++ b/test/TheFlow.Tests/Unit/DataAssociationShould.cs @@ -1,7 +1,4 @@ -using System; using FluentAssertions; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using TheFlow.CoreConcepts; using TheFlow.Elements.Data; using TheFlow.Infrastructure.Stores; diff --git a/test/TheFlow.Tests/Unit/DefaultProcessModelNamingConventionsShould.cs b/test/TheFlow.Tests/Unit/DefaultProcessModelNamingConventionsShould.cs new file mode 100644 index 0000000..e10b906 --- /dev/null +++ b/test/TheFlow.Tests/Unit/DefaultProcessModelNamingConventionsShould.cs @@ -0,0 +1,45 @@ +using FluentAssertions; +using TheFlow.Conventions; +using Xunit; + +namespace TheFlow.Tests.Unit +{ + public class DefaultProcessModelNamingConventionsShould + { + [Theory] + [InlineData("OnDataObject", "DataObject")] + [InlineData("onDataObject", "DataObject")] + [InlineData("OneInfo", "OneInfo")] + [InlineData("oneInfo", "oneInfo")] + public void ForDataObjects_NotStartWith_On_(string original, string expected) + { + new ProcessModelNamingConventions() + .DataObjectName(original).Should().Be(expected); + } + + [Theory] + [InlineData("FooActivity", "Foo")] + [InlineData("Inactivity", "Inactivity")] + public void ForAcitivities_NotHaveTheSuffix_Activity(string original, string expected) + { + new ProcessModelNamingConventions().ActivityName(original).Should().Be(expected); + } + + [Theory] + [InlineData("FooEventThrower", "Foo")] + [InlineData("TheEvent", "TheEvent")] + public void ForEventThrowers_NotHaveTheSuffix_EventThrower(string original, string expected) + { + new ProcessModelNamingConventions().EventThrower(original).Should().Be(expected); + } + + [Theory] + [InlineData("OnEvent", "OnEvent")] + [InlineData("onEvent", "onEvent")] + [InlineData("Event", "OnEvent")] + public void ForEventCatchers_ShouldStartWith_On(string original, string expected) + { + new ProcessModelNamingConventions().EventCatcher(original).Should().Be(expected); + } + } +}