The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
ابزارهای یادگیری هوش مصنوعی را باز کنید
ثبتنام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک میکنند سریعتر از هر ویدیو یاد بگیرید.
Stop Overwriting State And Use Event Sourcing Instead
زیرنویسها (569 بخشها)
Imagine in a game your player's
inventory shows three swords, two bows,
and a banana, but you have no idea how
they got there. In particular, where did
the banana come from? The problem is if
you just maintain state, you lose the
history. And this is where the event
sourcing pattern comes in. So, today
I'll show you the basic pattern plus a
few more advanced techniques such as
caching and projections. This video is
sponsored by Flux. I'll talk more about
them later. Now, what is event sourcing?
Most applications store the result of a
series of changes like a balance, a list
of items, a status flag. In event
sourcing, you actually never store the
final state. You store each change that
leads to it. And these changes are
called events. For example, item added
or item removed. Then if you want to
know what the current state is, you
replay those events and you construct
the state on the fly. Now this can be
useful in for example banking where you
want to keep track of transactions and
not the balance. Blockchain is also an
example of event sourcing and this has
many applications like memecoins and
rockps. Another example is the tool that
99% of developers use daily which is
git. I mean, it's not exactly event
sourcing, but the core idea is the same.
You store every change, never throw
anything away, and you derive the
current state from the history of
changes. Let's quickly build a basic
example of event sourcing. And we're
going to start with an inventory in a
game, the example I mentioned at the
start of the video. So, I start with
nothing more than this empty main file
that I have here. So, let's add some
files. So the first thing that I'm going
to do is I will add an event file and
this is where we're going to define the
event types that our system is going to
work with. So what we'll need is two
classes. One is going to be an enum with
different types of events and one is
going to be the actual event class. So
from enum I'm going to import the string
enum type
and then I'm going to define an event
type class.
So let's say we have item added
and we have item removed. Obviously
later on we can add more types of events
here. Then I want to create an event
class. So let's use data class for that.
And since events are typically right
once, we don't want to modify events
after the fact. I'm going to make this
frozen.
This is going to have a type
which is the enum that I defined before.
And let's say it just has some string
data for the moment. And also it's going
to be helpful to have a time stamp so we
know when the event was raised. And in
order to store that I'm going to import
the date time object.
Time stamp is a datetime object and
we're going to use the field from data
classes
to give that a default value.
So we're going to use uh datetime dot
now as a factory in order to generate
this timestamp when we create the event
like so. So this gives us a basic event
and the event type setup. Next we're
going to need some sort of store that
keeps track of the events that have been
raised in the system.
And this event store is obviously going
to need the event.
And let's make that a class.
So we have event store.
There's an initializer. And the only
thing that this is going to have is a
list of events
that initially is going to be empty. And
then we need some helpful methods like
being able to append an event.
Note that here I'm only talking about
events. I'm not talking about inventory
yet. That comes later.
This is not going to return anything.
And the only thing that this does is
call the event method on the events list
like so.
And then let's also add a method that
gets all the events.
So this is going to return a list of
events
[snorts]
and I'm doing it like so so that you
actually get a copy and you're uh not
accidentally changing the list of the
events in the event store. So this is
really simple event store. In fact, you
could even decide to not have this at
all because it's literally just a list
of events. So, the basic ingredients for
زیرنویس کامل در پخشکننده ویدیو موجود است
با تمرینها یاد بگیرید
تمرینهای واژگان، گرامر و درک مطلب از این ویدیو بسازید
نظرات (0)
برای نظر دادن وارد شویدثبتنام کن و همه امکانات رو باز کن
پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن
حالت تعاملی
آزمون
پاسخ صحیح:
ویدیوهای مرتبط
Late Night with Seth Meyers
آزمون
پاسخ صحیح:
آزمونها هنگام تماشای ویدیو ظاهر میشوند
راهنمای حفظ
از این ویدیو
شروع رایگان یادگیری زبان