Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
State or intent
1. STATE OR INTENT?
An Introduction to Event Sourcing
Oslo/NDC
Tomas Jansson
14/06/2013
2. THIS IS ME
Tomas Jansson
Manager & Group Lead .NET
BEKK Oslo
@TomasJansson
tomas.jansson@bekk.no
github.com/mastoj
blog.tomasjansson.com
3. WHY USE NOSQL IF WE ARE STORING THE SAME THING?
State
Tables
ORM
Anemic domain model
State
Document
Mapping
Anemic domain model
RDBMS
NoSQL
(Document store)
4. WHAT DO WE REALLY GAIN WITH A DOCUMENT STORE?
?
5. STORE THE INTENT INSTEAD OF THE STATE
Event
Sourcing
Understand
the
business
Testing
No
impedance
mismatch
Integration
Error fixing
Additional
value from
history
6. WHAT IS AN EVENT?
An event is something that has happened in the past
Examples:
CustomerMoved
TableReserved
OrderPlaced
Compare to actions:
MoveCustomer
ReserveTable
PlaceOrder
8. TESTING 1: SOMEONE CORRECTS THE ADDRESS OF A CUSTOMER
[TestFeature]
public class CustomerTests : BaseTestSetup
{
[Test]
public void When_The_Changing_The_Address_The_Address_Should_Change()
{
// Given
AddEvent(new CustomerCreated(1, "Tomas"));
AddEvent(
new AddressAdded(1, new Address("Street 1", "Oslol", "4040", "Norway")));
var customer = _domainRepository.Get<Customer>(1);
// When
customer.CorrectAddress(new Address("Street 1", "Oslo", "4040", "Norway"));
var events = _domainRepository.Commit();
// Then
Assert.AreEqual(1, events.Count());
Assert.Contains(
new AddressChanged("Street 1", "Oslo", "4040", "Norway"), events);
}
}
9. TESTING 2: A CUSTOMER MOVES
[TestFeature]
public class CustomerTests : BaseTestSetup
{
[Test]
public void When_The_Customer_Moves_The_Address_Should_Change_And_Notify()
{
// Given
AddEvent(new CustomerCreated(1, "Tomas"));
AddEvent(
new AddressAdded(1, new Address("Street 1", "Oslo", "4040", "Norway")));
var customer = _domainRepository.Get<Customer>(1);
// When
customer.Move(new Address("New Street 9", "Oslo", "6666", "Norway"));
var events = _domainRepository.Commit();
// Then
Assert.AreEqual(2, events.Count());
Assert.Contains(new CustomerMoved(1), events);
Assert.Contains(
new AddressChanged("Street 1", "Oslo", "4040", "Norway"), events);
}
}
Testing what we really expect to happen, and we also tests the
absence of side effects!
10. NO IMPEDANCE MISMATCH
public class Customer
{
private Address _address;
public Customer(IEnumerable<IEvent> events)
{ ... }
private void Handle(AddressChanged @event)
{
_address = @event.Address;
}
public void Move(Address address)
{ ... }
public void ChangeAddress(Address address)
{ ... }
.
.
.
}
Build instance from events
AddressChanged (an event)
is part of the domain
11. THE LAST THREE
Easier to make integration
async and explicit
Parties that are intrested can
just tail the event source
Focus is on behavior fixing
the root cause instead of
changing the data
Complete audit log
Just replay the events
Integration Error fixing
Additional value
from history
12. THE NEGATIVE SIDE OF EVENT SOURCING
Defining events is hard!
No tooling available
Lack of information regarding Event Sourcing
Projection of the events might be considered complex
13. RESOURCES
Rinat Abduling about why event sourcing: http://bliki.abdullin.com/event-sourcing/why
Events as a storage mechanism: http://cqrs.wordpress.com/documents/events-as-storage-mechanism/
My post about the subject: http://tinyurl.com/StateOrIntent
I’ve written down ”Anemic domain model” since the domain objects we have are loaded by mapping, and we are not setting the properties of our object based on domain actions/events.
I don’t know, it’s still just the data no extra information. Maybe some mapping, but the tools we have today for relational databases often solve that.We are not solving the real problem we are only subotimizing what we do. It’s like putting a bandaid on a broken leg.
And how can we do that? Use event sourcing.What will we gaing? Click.
What does this mean? This means you have to have your business logic before saving the event.
Instead of focusing on this data need to be stored and that should set this flag in this table you shift focus to what the intention of the action is. If the action succeed you store the intention (or event).
Testing is simple and explicit. The setup is clear, the action is clear and the intention is clear.
You can load them from a set of events.The events is part of the domain.