MassTransit / Automatonymous

A state machine library for .Net - 100% code - No doodleware
Apache License 2.0
736 stars 116 forks source link

Jcouls29/dynamic #70

Closed Jcouls29 closed 3 years ago

Jcouls29 commented 3 years ago

Due to a need for a more dynamic setup of workflow, a static method was added to AutomatonymousStateMachine to allow for the builder pattern. New interfaces were created and all current tests were copied and converted to the builder pattern to ensure everything was up to spec.

The general principle is that, by using the builder pattern, anyone can build a system (or framework) on top of this library where the need to build the workflow within the constructor isn't ideal. By allowing the states and events to live outside of the workflow, allows for more flexibility for these type of use cases, especially in a dynamic workflow situation where configurable workflows may be necessary.

Open to any thoughts on this approach.

Example Usage

State Waiting;
Event Start;
Event First;
Event Second;
Event Third;

AutomatonymousStateMachine<Instance>
    .Build(builder => builder
        .State("Waiting", out Waiting)
        .Event("Start", out Start)
        .Event("First", out First)
        .Event("Second", out Second)
        .Event("Third", out Third)
        .Initially()
            .When(Start, b => b.TransitionTo(Waiting))
        .During(Waiting)
            .When(First, b => b.Then(context => { context.Instance.CalledAfterAll = false; }))
            .When(Second, b => b.Then(context => { context.Instance.CalledAfterAll = false; }))
        .CompositeEvent(Third, b => b.CompositeStatus, First, Second)
        .During(Waiting)
            .When(Third, b => b
                .Then(context =>
                {
                    context.Instance.Called = true;
                    context.Instance.CalledAfterAll = true;
                })
                .Finalize()
            )

    );
phatboyg commented 3 years ago

Is your intention to make this usable with MassTransit as well, or just Automatonymous?

Jcouls29 commented 3 years ago

In all honesty, I'm new to Automatonymous itself and haven't really touched MassTransit. I wouldn't feel comfortable doing any work outside of this library until I understand it more. If you're saying that this change will require some changes over there, I can definitely help modify to the best of my ability there as well. I had no intention of using MassTransit or modifying it.

phatboyg commented 3 years ago

No need to modify MassTransit, so long as Automatonymous remains fully compatible with MassTransit after adding the builder features.

Jcouls29 commented 3 years ago

@phatboyg Sounds good. I tried to make these changes "additive" in nature and not cause any issues with how folks use it now. Just let me know about creating the static ASM class. If you'd like me to add that and alter the tests to use it, I can. Otherwise, I have completed all the other changes requested.

phatboyg commented 3 years ago

I'll pull in your changes tonight and look them over again. Thanks!

phatboyg commented 3 years ago

Still on my todo list...

phatboyg commented 3 years ago

This has been merged, I changed Create to New since Create is pretty commonly used as an Event name.