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

Desbloqueie ferramentas de aprendizado com IA

Cadastre-se para acessar ferramentas poderosas que ajudam a aprender mais rápido com cada vídeo.

Explicador de cena Caça-frases Revisão com flashcards Prática de repetição Falar de Volta
Cadastrar grátis
Inglês 25:12 Science & Tech

Stop Overwriting State And Use Event Sourcing Instead

Late Night with Seth Meyers · 32,424 visualizações · Adicionado há 2 meses

Legendas (569 segmentos)

00:00

Imagine in a game your player's

00:02

inventory shows three swords, two bows,

00:05

and a banana, but you have no idea how

00:07

they got there. In particular, where did

00:09

the banana come from? The problem is if

00:11

you just maintain state, you lose the

00:14

history. And this is where the event

00:16

sourcing pattern comes in. So, today

00:19

I'll show you the basic pattern plus a

00:21

few more advanced techniques such as

00:24

caching and projections. This video is

00:27

sponsored by Flux. I'll talk more about

00:29

them later. Now, what is event sourcing?

00:31

Most applications store the result of a

00:34

series of changes like a balance, a list

00:38

of items, a status flag. In event

00:41

sourcing, you actually never store the

00:43

final state. You store each change that

00:46

leads to it. And these changes are

00:48

called events. For example, item added

00:51

or item removed. Then if you want to

00:54

know what the current state is, you

00:55

replay those events and you construct

00:57

the state on the fly. Now this can be

01:00

useful in for example banking where you

01:03

want to keep track of transactions and

01:05

not the balance. Blockchain is also an

01:07

example of event sourcing and this has

01:10

many applications like memecoins and

01:13

rockps. Another example is the tool that

01:16

99% of developers use daily which is

01:20

git. I mean, it's not exactly event

01:22

sourcing, but the core idea is the same.

01:24

You store every change, never throw

01:27

anything away, and you derive the

01:29

current state from the history of

01:31

changes. Let's quickly build a basic

01:33

example of event sourcing. And we're

01:35

going to start with an inventory in a

01:38

game, the example I mentioned at the

01:39

start of the video. So, I start with

01:41

nothing more than this empty main file

01:43

that I have here. So, let's add some

01:45

files. So the first thing that I'm going

01:47

to do is I will add an event file and

01:50

this is where we're going to define the

01:53

event types that our system is going to

01:55

work with. So what we'll need is two

01:57

classes. One is going to be an enum with

01:59

different types of events and one is

02:01

going to be the actual event class. So

02:05

from enum I'm going to import the string

02:08

enum type

02:11

and then I'm going to define an event

02:13

type class.

02:16

So let's say we have item added

02:23

and we have item removed. Obviously

02:26

later on we can add more types of events

02:29

here. Then I want to create an event

02:32

class. So let's use data class for that.

02:39

And since events are typically right

02:42

once, we don't want to modify events

02:44

after the fact. I'm going to make this

02:46

frozen.

02:50

This is going to have a type

02:53

which is the enum that I defined before.

02:55

And let's say it just has some string

02:57

data for the moment. And also it's going

03:01

to be helpful to have a time stamp so we

03:03

know when the event was raised. And in

03:06

order to store that I'm going to import

03:09

the date time object.

03:15

Time stamp is a datetime object and

03:18

we're going to use the field from data

03:21

classes

03:24

to give that a default value.

03:28

So we're going to use uh datetime dot

03:32

now as a factory in order to generate

03:35

this timestamp when we create the event

03:37

like so. So this gives us a basic event

03:41

and the event type setup. Next we're

03:43

going to need some sort of store that

03:45

keeps track of the events that have been

03:48

raised in the system.

03:52

And this event store is obviously going

03:55

to need the event.

03:59

And let's make that a class.

04:01

So we have event store.

04:05

There's an initializer. And the only

04:07

thing that this is going to have is a

04:09

list of events

04:14

that initially is going to be empty. And

04:17

then we need some helpful methods like

04:20

being able to append an event.

04:24

Note that here I'm only talking about

04:25

events. I'm not talking about inventory

04:27

yet. That comes later.

04:30

This is not going to return anything.

04:33

And the only thing that this does is

04:37

call the event method on the events list

04:41

like so.

04:44

And then let's also add a method that

04:47

gets all the events.

04:51

So this is going to return a list of

04:53

events

04:58

[snorts]

04:58

and I'm doing it like so so that you

05:01

actually get a copy and you're uh not

05:04

accidentally changing the list of the

05:06

events in the event store. So this is

05:09

really simple event store. In fact, you

05:12

could even decide to not have this at

05:14

all because it's literally just a list

05:16

of events. So, the basic ingredients for

Legendas completas disponíveis no player de vídeo

Pratique com exercícios

Gere exercícios de vocabulário, gramática e compreensão deste vídeo

Vocabulário e gramática Quiz de compreensão Exame IELTS Prática de escrita
Cadastre-se pra praticar
Nenhum comentário ainda. Seja o primeiro a compartilhar suas ideias!

Cadastre-se para desbloquear todos os recursos

Acompanhe seu progresso, salve vocabulário e pratique com exercícios

Aprenda idiomas de grátis