Dariusz Pawlukiewicz
IT Academic Day 20.10.2016
Blog: Forever F[r]ame
Podcast: DevReview
GitHub
@d_pawlukiewicz
Forever Frame
dpawlukiewicz
foreverframe.pl
devreview.pl
github.com/GooRiOn
async Task<string> GetTextAsync()
{
Task<string> task = DoSomethingAsync();
DoSomethingSync();
string taskResult = await task;
return taskResult;
}
async Task<string> DoSomethingAsync()
=> await Task.Run(async () =>
{
await Task.Delay(5000);
return "I ran async";
});
void DoSomethingSync()
{
for (var i = 0; i < 100; ++i)
Console.WriteLine(i);
}
Command Query Separation (Bertrand Mayer, 1986)
Methods
Commands
Queries
~ Martin Fowler
Command
Query
Idempotent
Removes AND returns the object at the top of the Stack.
Command Query Responsibility Segregation
Methods
Commands
Queries
Objects
Command Bus
Command Handler
DAL
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
Object which represents user's intention
DTO
Data centric
Command
Behavior centric
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
DAL
Command Bus
Command Handler
Domain object
Write DB
Contains business logic
Requires more skill to implement
DB can be optimized for writing
DAL
Event Bus
Event Handler
Read DB
Read DB abstraction
NO business logic
Way easier to implement
DB can be optimized for read
| Id | Name | Quantity | Price | IsDeleted |
|---|---|---|---|---|
| 1 | Book1 | 23 | 34 | 0 |
| 2 | Book2 | 12 | 20 | 1 |
Get Book1 data
Set quantity to 3
Save changes
Get Book1 data
Set quantity to 22
Save changes
t
| Id | Name | Price |
|---|---|---|
| 1 | Book1 | 12 |
Event
Get Book1 data
Create new BookSold event
Save changes
Get Book1 data
Save changes
t
Create new BookSold event
| Id | BookId | Data | Type | DateTime |
|---|---|---|---|---|
| 1 | 1 | {Name: 'Book1',...} | BookCreated | 2016-02-12 12:00 |
| 2 | 1 | {Quantity: 1} | BookSold | 2016-04-23 09:34 |
| 3 | 2 | {Name: 'Book2',...} | BookCreated | 2016-06-12 12:44 |
| 4 | 5 | {Name: 'Book5'} | BookRenamed | 2016-06-15 23:44 |
| ... | ... | ... | ... | ... |
| 678 | 1 | {Quantity: 12} | BookSold | 2016-12-12 20:49 |
| Id | BookId | Data | Type | DateTime |
|---|---|---|---|---|
| 1 | 1 | {Name: 'Book1',...} | BookCreated | 2016-02-12 12:00 |
| 2 | 1 | {Quantity: 1} | BookSold | 2016-04-23 09:34 |
| 3 | 2 | {Name: 'Book2',...} | BookCreated | 2016-06-12 12:44 |
| 4 | 5 | {Name: 'Book5'} | BookRenamed | 2016-06-15 23:44 |
| ... | ... | ... | ... | ... |
| 678 | 1 | {Quantity: 12} | BookSold | 2016-12-12 20:49 |
SELECT Data, Type FROM BookEvents
WHERE BookId = 1
ORDER BY DateTime
| BookId | Name | Quantity | Price | IsDeleted |
|---|---|---|---|---|
| 1 | Book1 | 23 | 37 | 0 |
BookCreated
{
Name: 'Book1',
Quantity: 23,
Price: 37
}
BookSold
{
Quantity: 20
}
BookSold
{
Quantity: 1
}
3
2
| Id | BookId | Data | Type | DateTime |
|---|---|---|---|---|
| 1 | 1 | {Name: 'Book1',...} | BookCreated | 2016-02-12 12:00 |
| 2 | 1 | {Quantity: 1} | BookSold | 2016-04-23 09:34 |
| 3 | 2 | {Name: 'Book2',...} | BookCreated | 2016-06-12 12:44 |
| 4 | 5 | {Name: 'Book5'} | BookRenamed | 2016-06-15 23:44 |
| ... | ... | ... | ... | ... |
| 678 | 1 | {Quantity: 12} | BookSold | 2016-12-12 20:49 |
SELECT Data, Type FROM BookEvents
WHERE BookId = 1
ORDER BY DateTime
AND DateTime < '2016-05-12'
...
Event
#1
Event
#2
Event #1002
Event #1003
...
Event
#998
Event
#999
Snapshot
Event #1000
...
Command Bus
Command Handler
DAL
Domain object
Write DB
Event Bus
Event Handler
Read DB
Read DB abstraction
command
read model
query
ack
Event Store
Event Store
abstraction
@d_pawlukiewicz
foreverframe.pl