0

Publishing Events from CRUD or Commands?

 2 years ago
source link: https://codeopinion.com/publishing-events-from-crud-or-commands/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Publishing Events from CRUD or Commands?

There are many different ways to create events in Event Driven Architecture. Events from CRUD or created from specific actions that have occurred. In some cases, consumers only care about the full scope of an entity/data. In other boundaries, consumers only care about what has occurred. When publishing events from CRUD you’ll likely care more about propagating state. However, events derived from tasks/commands are often used more as notifications about business process occurrences.

YouTube

Check out my YouTube channel where I post all kinds of content that accompanies my posts including this video showing everything that is in this post.

When developers start getting into Event Drive Architecture, I find they most often create events that are represented by Create/Update/Delete of an Entity. Meaning if there is a Product Entity, then they have a ProductCreated event, ProductUpdated event, or ProductDeleted event.

public record ProductCreatedEvent(Product Product); public record ProductUpdatedEvent(Product Product); public record ProductDeletedEvent(Sku Sku);

This is most often driven by UI and APIs that are CRUD and Entity oriented. I call these APIs Entity Services and try to avoid them when working in the core of a domain. For more info on Entity Services, check out my post AVOID Entity Services by Focusing on Capabilities

The next most common type of event created for those new to Event Driven Architecture is still driven by CRUD and Entities, but all the updates are property-based. If the Product Entity has a Quantity property, then the event published ends up being a ProductQuantityUpdated event.

public record ProductQuantityUpdated(Sku Sku, int NewQuantity); public record ProductWarehouseLocationUpdated(Sku Sku, WarehouseLocation WarehouseLocation);

In both types of events what’s common about them is the event contains data. If it’s a Created or Update type event, often it will contain the entire Entity that was created. If it’s a Property Updated event, it will likely contain the new value of the property, but it’s also common that people will put the old value as well.

The reason why this is common is that a lot of systems are developed primarily based on CRUD. If you’re UI and APIs are driven by CRUD, you’re going to develop events that are derived from CRUD.

Behavioral

What’s the alternative to CRUD? Taking a more task-driven approach to developing a system. Providing end-users with capabilities that are directly related to the business functions they perform. In my example above with ha Product Entity, a user doesn’t “Update the Quantity”. There is a reason for this that’s related to a task. Did the warehouse do a stock count and realized they found more products on the actual shelves than are stated in the system? If so they would perform an “Inventory Adjustment”. There’s a difference between “Inventory Adjustment” and “Update the Quantity”.

public record ProductShipped(Sku Sku, int Quantity, DateTime ShippedOn); public record ProductReceived(Sku Sku, int Quantity, DateTime ReceivedOn); public record InventoryAdjusted(Sku Sku, int Quantity, DateTime AdjustedOn); public record ProductMoved(Sku Sku, WarehouseLocation WarehouseLocation, DateTime MovedOn);

In the events above, they are all behavioral. These events are statements of facts of actual business events. In order to publish these types of events, you need to move away from CRUD and move towards Commands and a Task Based-UI. For more on this check out my post Decomposing CRUD to a Task Based UI.

In the above screenshot, we’ve moved away from CRUD in some aspects. The Name and Description can be saved by a simple update however we can no longer just change the Quantity. We need to specify an action/task that is a business reason why we are changing the Quantity.

From this, we can derive that we are creating and publishing an InventoryAdjusted event which allows us to be much more explicit.

Explicit vs Implicit

CRUD-based events (ProductCreated, ProductUpdated, etc) aren’t inherently bad. They can be useful for referential data that lives on the edge of your system. Data that is not a part of the (sub)domain you’re focusing on. For this reason, you might publish CRUD-based type events because other service boundaries want a local cache copy of that data. However, since they aren’t explicit about the reason or business task that occurred and created the event, you won’t have that insight from a consumer. You would have to imply it, if you could, based on data that changed.

Behavioral Events (ProductShipped, ProductInventoryAdjusted, etc) are explicit. They tell consumers exactly what has occurred. These types of events are generally used as a notification to other service boundaries that might be a part of a long-running business process. They usually only contain a minimal amount of data. They are generally used for data propagation for local cache copies. Check out my post on Event Choreography & Orchestration that covers how behavioral events are used between services.

Source Code

Developer-level members of my CodeOpinion YouTube channel get access to the full source for any working demo application that I post on my blog or YouTube. Check out the membership for more info.

Related Links

Follow @CodeOpinion on Twitter

Leave this field empty if you're human:


Recommend

  • 58
    • www.tuicool.com 6 years ago
    • Cache

    REST was Never about CRUD

    REST was NEVER about CRUD A popular myth is that REST-based APIs must be CRUD-based – that couldn’t be further from the truth. It is simply one pattern in our API desig...

  • 56
    • www.tuicool.com 6 years ago
    • Cache

    CRUD Operations in SQL Server

    By: Jeffrey Yao |  |   Related Tips:More >Database Design Problem I am a new S...

  • 3
    • marcosh.github.io 3 years ago
    • Cache

    Real world events are commands

    Real world events are commands Mar 23, 2017 • Marco Perone These days (maybe it’s better to say, months…) I am working on a ES/CQRS application where command and event messages play a really important role. Usually it is...

  • 3

    From legacy to DDD: Start with publishing events When you start your journey with DDD, it’s quite easy to apply DDD in a fresh app. It’s a bit more complicated for existing, legacy apps. This blog post shows how you...

  • 4
    • codeopinion.com 3 years ago
    • Cache

    Messaging Commands & Events Explained!

    In service oriented architecture (including microservices) that leverage messaging, you’re going to run into both types of messages: Commands & Events. What’s the difference between Commands & Events? When should you use a command an...

  • 12

    In this blog post we’re going to cover some best practices you can use when designing domain events and objects intended to work with Akka.NET. If you follow these best practices you’ll run into fewer errors, clearer log messages, and a bette...

  • 6

    Turning Events into Commands – .NET Development Addict Have you ever used some control in Xamarin.Forms that appears to have an event instead of a command? You are working the MVVM love and then you come across that annoying control....

  • 0

    Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me...

  • 1
    • codeopinion.com 1 year ago
    • Cache

    Don’t Fail Publishing Events!

    Don't Fail Publishing Events! Skip to main content Sponsor: 

  • 5

    ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK