Lecture thumbnail 0:00 / 0:00 So in this demonstration, what I want to show you is a clip from my reactive extension course.
And the reason why it’s relevant is because we’re going to build something called an event broker.
An event broker is a component which, just like in the case of a chat room, it allows the exchange
of messages between different components without them being aware that they’re present in the system.
So we’re going to take a look at how to build such an event broker, and we’re going to use reactive
extensions to do it.
So we’re going to simulate a football game where you have a bunch of players and a football coach and
they’re all participating in the game and they’re communicating with one another through some sort of
central agent, that agent being an event broker.
So we’re going to build the whole thing using reactive extensions as well as a dependency injection
container.
In my case, I’m using Autofac, which is my favorite container.
So let’s begin.
Let’s take a look at how we can build the whole thing.
So we’re building an actor model so we’ll have the root actor class called Actor.
Now, in addition, we’ll have different actors.
We’ll have a football player who happens to be an actor, and of course we’ll have the coach as well.
So let’s have a football coach who also happens to be an actor.
Okay, so we’ve got the actors in now.
What about the kind of things that the football player can actually do?
Like the player can score a goal, the player can be sent off so we can have a base class, some kind
of player event.
And then of course, we can have its descendants.
So for example, we can have player scored event and we can also have player sent off event.
Okay, so these are all player actions.
So we might as well store the player name name like.
So now in addition for the player scored, we might want to store the number of goals which are actually
scored.
So public int goals scored get set like so.
Now for the player sent off.
We might want the reason why the player was sent off.
So let’s have that as well.
So we now have a hierarchy of events.
We also have the different actors.
All that’s missing is the event broker to glue everything together.
So let’s put that in.
So let’s have event broker.
Now Event broker is going to be an iobservable of these player events.
So it’s essentially going to be iobservable of player event.
Now the question is, well, how do we want to implement our observable?
And here to save some time, I’m going to cheat by using a subject because I’m allowed to cheat.
It’s my course.
I can cheat as much as I want.
So what I’m going to do is I’m going to make a subject of player event.
So it’s going to be called subscriptions.
I’ll make a new subject of well, subject specifically of player event like so.
And then when it comes to subscribing, all I’m going to do is I’m going to actually add it to the subscription.
So I’m going to say subscriptions dot subscribe and add the appropriate argument, which is the observer.
And similarly, I’ll have some sort of method for actually publishing events.
So public void publish player event P And here all I’m going to do is I’m going to go and do subscriptions
dot on next and notify each one of those.
So essentially I’m reusing a part of reactive extensions infrastructure so that I don’t have to implement
this myself.
So there we go.
We actually need to return from here and disposable.
But the broker itself is already done.
So all we have to do is we have to propagate the broker so that when dependency injection kicks in,
it’s everywhere.
Now for that, we’ll go into the actor, we’ll make protected event broker, broker here, we’ll make
a constructor.
And now what happens after we use the latest?
C-sharp seven features, of course, is we need to implement the constructors in the Descendant.
So let’s do that.
Let’s implement the missing members here and let’s implement the missing members here as well.
Okay.
So now we need to decide what kind of information we want from the broker.
Like, for example, if we’ve got a football coach, what does the coach care about?
Now, the coach obviously wants the player to score goals, so let’s do that.
So we take the broker, which by the way, is observable of player events and we say of type.
So we want the player scored event.
So for every single one of these, we subscribe.
So we say subscribe with a dot.
Obviously Subscribe.
There we go.
We take the player event and then we process it somehow.
Okay, so here, if the player has scored less than three goals, so let’s actually look at goal scored.
If it’s less than three, we want to congratulate the player.
So the coach here says well done.
And then the player name.
Exclamation mark.
There we go.
We should have the player.
If we don’t have it, that means we didn’t do the inheritance on the events, which yes, we forgot
to do.
So both of these are actually player events.
Player events.
There we go.
Okay, so now we have the name itself correctly.
So that’s one of the things that the coach cares about.
Another thing might be, for example, when the player is sent off.
So here broker can say of type player sent off.
And if that happens, we also subscribe.
So we have some sort of player event.
And here if the reason for the sending off is violence, then we might want to say something sad.
So if the player event reason is equal to violence, we might want to ask the player how could they
do this whatever it is that they did?
So here the coach says, the coach says, How could you?
And then the name of the player name again.
So there we go.
And that’s the subscriptions of the coach.
Now the player itself participating as another actor can also react to certain different things which
are happening on the field.
So let’s add those.
All right.
So I’m going to actually make a constructor if we go to the football player and if we actually well,
we already have a constructor with the event broker, we may as well make some sort of more sophisticated
constructor where we also specify the name.
But of course we haven’t provided the name yet, so let’s have the name like so.
And then when we go and generate the constructor, it’s going to be a bit more interesting.
So here is the constructor, but we also want to subscribe to particular events that are taking place
on the event bus or the event broker.
So for example, what we can say is broker dot of type.
Once again, we’re interested for player scored events and we want to congratulate our fellow teammate,
but we, we can filter it because when we ourselves score, we don’t want to congratulate ourselves.
So we need to filter using the where operator where so where the name is not equal to our name.
So we don’t want this to be ourselves.
We don’t want to congratulate ourselves basically.
But if it’s not ourselves, then we can subscribe and we can congratulate regardless of the number of
goals scored.
So, for example, we can say Oops, and here we can write line and we can say that this is our name
and we say, nicely done, comma, and then BS dot name.
So we’re congratulating the other player and we can say it’s your and then the number of goals they
scored.
So PS dot goals scored goal like so.
Okay so that’s pretty much it for the subscriptions for other player scoring.
However, if the player gets sent off, we might want to say something else.
We actually want to maybe keep a subscription to it, but let’s do it like this for now.
So broker of type player sent off event in the case the player is sent off.
We can once again filter that.
It’s not ourselves we’re listening to.
So PS goes to not PS dot name dot equals our name if we’re not congratulating ourselves.
So in this case, not congratulating because being sent off, but we’re going to write line something
different.
So we’re going to write line and we’re going to say name.
That’s how a name see you in the lockers.
And then the name of the player that’s just being sent off.
So PS dot name.
There we go.
Okay, so we have some subscriptions on the overall event bus on both the player as well as the coach.
And what we need to do now is we need to glue everything together using a dependency injection container,
which really isn’t difficult.
So in Autofac I’ll make a new container builder like so let’s get all the right imports in place.
I will first of all register a couple of types.
I need to register the event broker and remember the event broker has to be shared between all the components.
It’s injected into the same component.
So it has to be a singleton because we only need one really.
And then of course we register the football types.
So we register the football coach, we register the player.
But remember with the player, we got this situation where you need to.
Feed the player the name.
So we made a constructor, which actually takes not just the event broker but the name as well.
So that’s going to be a bit tricky.
But once again, in Autofac, everything is possible, so we’re going to register the player just doing
it differently.
So we’re going to say register with a lambda.
So it’s going to be C comma P, And here what we’re going to do is we’re going to make a new football
player
and the event broker has to be resolved from the container.
So that’s what we’re going to do here.
I’ll just lay it out nicely.
So here the first argument is that we take the container and we resolve a football or we resolve the
event broker rather.
So that’s the first argument.
And the second argument is going to be provided later on.
So that’s going to be an argument named it’s going to be a string argument named name.
There we go.
So this is how you configure things in the dependency injection container so you can subsequently provide
the names of whatever players you need.
So now we’re going to build the container.
So using var C equals container builder dot build, and we can actually start using the container to
actually set up our scenario.
We’re going to have some players and whatnot.
So I’m going to have a coach that’s going to be C dot resolve and get the coach in here, football coach
like so.
Now in addition, I’m going to make two football players.
So var player one equals and then I’ll resolve the football player.
But I’ll give them names.
So here I make a new named parameter called Name, and I’ll provide the name John and I’ll duplicate
this and provide the name Chris.
Okay, so that’s going to be player one and player two.
And then of course, I want to simulate them scoring goals.
So I’m going to have player one dot score.
Do we have score or we haven’t done it yet?
Yes, that’s another thing that we need to do because notifications, because you need to actually notify
when somebody scores a goal.
So we haven’t implemented this yet, but we’re going to do it now.
It’s not particularly difficult.
So let’s go to football player and let’s add a particular method for scoring goals.
So in this case, I’m going to just have a method called score.
And essentially we’re going to keep the number of goals scored.
So I’ll have public int, int goals scored, get set originally zero, and whenever you score, you
do goals scored plus plus followed by publishing something on the broker.
So here we’re going to say broker dot publish and we’re going to publish an event on the broker for
everyone to see.
So that’s going to be a player scored event where the name of the player is the name and the number
of goals scored is the number of goals scored that we are kind of storing here.
Goals scored.
Interesting.
I got too many equals in here.
Okay, so this is how you publish a score event.
And similarly, if you decide to assault the referee, then you’re going to be sent off.
So in this case, all you do is you publish a different event, you publish a new player sent off event.
And here the the name is still the name.
And the reason is violence.
Reason is violence.
There we go.
In quotes, obviously, because it’s a string.
So let’s do this.
Okay.
We’re now done in terms of actually having methods for generating different events on the player.
So coming back to our set up, what we can do is we can get the player scoring first of all and maybe
player getting sent off.
So Player one dot score, well we already got it once, so we may as well do it three times in a row
because only get two notifications thanks to the code.
So we shouldn’t get this one.
This should be completely ignored.
And then of course, player one gets to assault the referee and subsequently gets sent off and then
player two scores.
All right.
So we now have this rather large setup for the coach and two players being created using a dependency
injection container.
This is relevant because the event broker gets automatically injected and it’s a singleton.
So let’s see if we can actually get this to run and to provide certain amount of output.
It should be quite a lot because, well, we have many places where there is output.
Okay, so let’s go through this step by step.
So player one scores the player, one name is John, so the coach is happy and also Chris, the other
player is happy.
Then player one scores again.
So we have well done.
John from both the player and the coach.
But the third one, remember the coach doesn’t care if you score more than two goals, so we only get
the congratulations from the second player.
Now what happens is player one decides to assault the referee so they get sent off and the coach says,
How could you, John?
And by this point, John has effectively left.
But when he’s leaving, Chris says, See you in the lockers.
John So that’s exactly what we wanted.
And then finally Chris scores and the coach says, Well done, Chris.
And John also says, Nicely done, Chris And this is one of the things that you could probably kill
in the sense that this subscription has to be ended because John is not on the field.
So if you want to end the subscription prematurely, there’s absolutely no problem in actually doing
this because essentially you can you can store the subscription in the sense that whenever you have
these things on the player, you can say that, Oh, whenever we’re sent off, the subscriptions are
actually, you know, off the table.
They are no longer no longer relevant.
So that’s, that’s quite easy to do.
You just store the subscription and you get rid of it.
So this is a small demo of how you would set up an event bus or an event broker, depending on the terminology
you prefer, using reactive extensions as well as a dependency injection container.
Play Play Stop Play Start Play information alert