Skip to content

Commit

Permalink
Naming Conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
ElemarJR committed Sep 5, 2018
1 parent 60c7137 commit 1ee91b0
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 51 deletions.
8 changes: 8 additions & 0 deletions src/TheFlow/Conventions/ProcessModelConventions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TheFlow.Conventions
{
public class ProcessModelConventions
{
public ProcessModelNamingConventions Naming { get; }
= new ProcessModelNamingConventions();
}
}
27 changes: 27 additions & 0 deletions src/TheFlow/Conventions/ProcessModelNamingConventions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace TheFlow.Conventions
{
public class ProcessModelNamingConventions
{
public Func<string, string> ActivityName { get; set; } = input =>
input.EndsWith("Activity")
? input.Substring(0, input.Length - "Activity".Length)
: input;

public Func<string, string> DataObjectName { get; set; } = input =>
(input.StartsWith("On") || input.StartsWith("on")) && char.IsUpper(input[2])
? input.Substring(2)
: input;

public Func<string, string> EventCatcher { get; set; } = input =>
(input.StartsWith("On") || input.StartsWith("on")) && char.IsUpper(input[2])
? input
: $"On{input}";

public Func<string, string> EventThrower { get; set; } = input =>
input.EndsWith("EventThrower")
? input.Substring(0, input.Length - "EventThrower".Length)
: input;
}
}
30 changes: 0 additions & 30 deletions src/TheFlow/CoreConcepts/Name/DataObjectName.cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/TheFlow/CoreConcepts/ProcessInstance.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using TheFlow.CoreConcepts.Names;

namespace TheFlow.CoreConcepts
{
Expand Down Expand Up @@ -27,14 +26,14 @@ public partial class ProcessInstance : IProcessInstanceProvider
_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];
}
}
}
10 changes: 2 additions & 8 deletions src/TheFlow/CoreConcepts/ProcessModel.Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ProcessModel AddEventCatcher(string name, IEventCatcher catcher)

public ProcessModel AddEventCatcher<TEvent>()
where TEvent : class
=> AddEventCatcher<TEvent>($"On{typeof(TEvent).Name}");
=> AddEventCatcher<TEvent>(Conventions.Naming.EventCatcher(typeof(TEvent).Name));

public ProcessModel AddEventCatcher<TEvent>(string name)
where TEvent : class
Expand Down Expand Up @@ -104,14 +104,8 @@ public ProcessModel AddActivity(NamedProcessElement<Activity> activity)
public ProcessModel AddActivity<TActivity>()
where TActivity : Activity
{
var name = typeof(TActivity).Name;
if (name.EndsWith("Activity"))
{
name = name.Substring(0, name.Length - "Activity".Length);
}

var activity = Activator.CreateInstance<TActivity>();
return AddActivity(name, activity);
return AddActivity(Conventions.Naming.ActivityName(typeof(TActivity).Name), activity);
}

public ProcessModel AddParallelGateway(string name)
Expand Down
4 changes: 4 additions & 0 deletions src/TheFlow/CoreConcepts/ProcessModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,9 @@ public partial class ProcessModel : IProcessModelProvider
public ImmutableList<IProcessElement<IElement>> Elements { get; }
public ImmutableList<Association> Associations { get; }

public ProcessModelConventions Conventions { get; }
= new ProcessModelConventions();

#region Constructor and Empty Object
public ProcessModel(
string id,
Expand Down
5 changes: 4 additions & 1 deletion src/TheFlow/Elements/Events/CatchAnyEventCatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 8 additions & 4 deletions src/TheFlow/Elements/Events/TypedEventCatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using TheFlow.CoreConcepts;
using TheFlow.CoreConcepts;
using TheFlow.Elements.Data;

namespace TheFlow.Elements.Events
Expand All @@ -13,7 +12,9 @@ public class TypedEventCatcher<TEvent>

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);
Expand All @@ -30,7 +31,10 @@ void IEventCatcher.SetEventDataOutput(DataOutput dataOutput)

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);
Expand Down
3 changes: 0 additions & 3 deletions test/TheFlow.Tests/Unit/DataAssociationShould.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 1ee91b0

Please sign in to comment.