Lecture thumbnail 0:00 / 5:37 All right.

So now that we’ve looked at how to construct a state machine by hand, we’re going to look at one of

the many ways in which you can construct a state machine using a library solution.

So in this case, I’m going to use a slightly less politically correct example of a state machine.

And we’re going to take a look at, guess what, reproductive health, because, well, it’s also an

important topic.

So that’s what we’re going to look at now this time around, if we look at the nugget references, let

me go into nugget packages for a moment.

You’ll see that I’m using an external nugget package called Stateless and Stateless is an implementation

of a hierarchical state machine, which is it’s a library written by Nicholas Blumgart, who is also

one of the authors of the Autofac Container, which I use personally.

So that’s what we’re going to use.

And I’ve included the nugget package here so we can start using the state machine.

Now, once again, we’re going to define two enumerations.

One is going to be the set of states that you can be in.

So let’s enum, let’s say since we’re doing reproductive health, I’m going to call this health and

we might have non reproductive.

Maybe you’re too young and then pregnant and then reproductive.

So these are the three states.

And then there are a couple of things you can do in order to transition from one state to another.

So let’s make a bunch of activities.

So there’s something that you can do in order to move one state from another.

For example, give birth, you can grow older or maybe, I don’t know, reach puberty that’s better,

and then have an abortion.

Sorry, but that also happens.

Have unprotected sex and maybe have an operation to remove all the reproductive parts because they’re

so terrible and so wrecked me.

Sorry about being so macabre, but this is real life.

So I’m having a real life scenario here.

Okay, so we’re going to make a state machine using this stateless library.

I’m not going to orchestrate the state machine, meaning I’m not going to do the please input your current

state and please input the transition kind of stuff that we did previously.

But I will show you how the state machine works.

So you basically define a state machine, let’s call it just machine.

And this is a new state machine.

And notice it takes two template arguments.

The first is this state and the second is the trigger.

So the state in this case, it’s going to be health and the triggers are activities that you can do.

So activity like so.

So this is the state machine.

But having made the state machine, we also need to specify the starting state.

So here are the initial state is going to be, well, let’s have non-reproductive to begin with.

Okay.

So now that you have this, what you do is you configure the set of transitions from a particular state

to a set of other states.

And the way this is done is as follows.

So you say machine dot configure, and then you specify the state that you want to transition from.

So the entry state.

So for example, if you’re in a non-reproductive state, then you can transition to a reproductive state

by reaching puberty.

And the way this is done is by using the permit method.

Now, as you can see here, there is a ton of customization here in terms of the kind of things that

you can do, for example, permit re-entry and stuff like that.

So state machines are really complicated topics and in actual fact, you could do a separate course

on how to design state machines and make them do all the things that you want.

I’m only covering the design pattern here, so I want to show you how it works.

So essentially we permit the transition to a reproductive state, assuming that you reach puberty,

reach puberty and you transition to a reproductive state.

So this is how you would define a transition.

And then of course, you can do dot permit and you can specify an additional transition.

And after you have a couple of transitions, you may end up with something like the following.

Let me just paste something in here that I’ve prepared already.

So yeah, that should be machine.

So let’s have state machine like so and I’m going to change state machine.

So and this, this should be slightly less ambiguous.

Okay.

So what do we have here?

Well most of these are just permit, but some of these are conditional, like for example, permit.

If means that you can transition from whatever state you were reproductive to a pregnant state if you

have unprotected sex.

But the condition is that your parents are not watching because if they are, they’ll probably tell

you to use protection.

So here you can make a make a property, for example, a Boolean property called Parents Not Watching.

And you can be.

Dependent upon it, which means that the transition will only occur.

It’s only possible if this boolean has the value.

True.

If the value is false, then you cannot transition from reproductive to pregnant.

So this is a once again a small overview of how this is done in the real world.

So you probably don’t want to write your own dictionary.

You probably want to use an external library of some kind.

And certainly Microsoft has some libraries.

Microsoft has this thing called Workflow Foundation, which is a bit overengineered.

I like the stateless Library, so this is a small example of how you use it.

Play Play Play Play Play Stop Start Play information alert