<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mads Engel Lundt on Medium]]></title>
        <description><![CDATA[Stories by Mads Engel Lundt on Medium]]></description>
        <link>https://medium.com/@madslundt?source=rss-20be1ee61cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AENTdtic-WGgdSxS8ZbZdQ.jpeg</url>
            <title>Stories by Mads Engel Lundt on Medium</title>
            <link>https://medium.com/@madslundt?source=rss-20be1ee61cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:19:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@madslundt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to write a well structured API from the beginning]]></title>
            <link>https://medium.com/@madslundt/how-to-write-a-well-structured-api-from-the-beginning-1b15992e09ce?source=rss-20be1ee61cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b15992e09ce</guid>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[mediator]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[cqrs]]></category>
            <dc:creator><![CDATA[Mads Engel Lundt]]></dc:creator>
            <pubDate>Thu, 31 Aug 2023 21:44:26 GMT</pubDate>
            <atom:updated>2023-09-01T21:39:43.731Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*UYjceH2dyHDk9JR5bNSHfA.jpeg" /></figure><p>Frequently, I observe individuals initiating new projects and immediately attempting to fragment them into microservices. Please understand that I do find microservices captivating and impressive, and they can indeed enhance the work experience. Nevertheless, it’s essential to acknowledge that microservices also carry certain downsides, such as heightened maintenance expenses, increased complexities in terms of organization and coordination, as well as additional overhead, among other factors.</p><p>Often, people are in a more advantageous position by embracing a simpler approach initially, such as a monolithic architecture. A monolithic architecture is often characterized by its simplicity, though it might lack scalability, reliability, and exhibit slower development speed as the application expands.</p><p>In 2009, Netflix encountered some of these challenges when it struggled to meet the demand for its rapidly growing video streaming services. To address this, Netflix began transitioning its technology stack into microservices, eventually becoming one of the prominent pioneers in successfully migrating from a monolith to a cloud-based microservices architecture.</p><p>Netflix’s journey underscores the necessity of microservices to support large-scale applications. However, this necessity might only arise later in the application’s development, if it arises at all. This led me to contemplate the idea of blending principles from monolithic architecture with methodologies from microservices. This could be achieved by segmenting the project into distinct domains, aiming for a well-organized and scalable system.</p><h3>Monolithic application with microservice methodologies</h3><p>To establish a well-structured and scalable system, leveraging existing libraries can prove highly beneficial. One of my personal favorites is MediatR, developed by Jimmy Bogard.</p><p>MediatR facilitates in-process messaging within an application, promoting loose coupling and minimizing dependencies. A standout feature of MediatR is its encouragement of segregating read and write operations, achieved through CQRS (Command and Query Responsibility Segregation). This approach allows distinct optimization of models for specific tasks, resulting in a flexible and easily maintainable application architecture.</p><p>Furthermore, MediatR offers support for domain-event communication, which permits the explicit handling of side effects resulting from changes. This is particularly useful for events that have occurred in the past and need to be communicated to other components within the same domain; for instance, the creation of a user.</p><p>What’s noteworthy is that both of these capabilities unfold within the confines of a single application, yet they embody the principles associated with a microservices architecture. Integrating these features into your approach should provide a solid foundation for your next project and contribute significantly to its organizational structure.</p><h3>CQRS</h3><p>Let’s initiate the process of implementing these concepts in our application. Typically, my approach involves introducing a dedicated project to house all the infrastructure components. This project remains independent of other segments within the application, enabling the establishment of various functionalities such as CQRS, exception handling, and authentication.</p><p>While MediatR already provides CQRS capabilities, I often opt for a slightly different organization. I prefer to configure distinct components: a command bus for write models, a query bus for read models, and an event bus for handling events.</p><p>Incorporating IRequest and IRequestHandler from MediatR, the setup for read models (queries) has been implemented as demonstrated below:</p><pre>public interface IQuery&lt;out TResponse&gt; : IRequest&lt;TResponse&gt;<br>{ }<br><br>public interface IQueryBus<br>{<br>    Task&lt;TResponse&gt; Send&lt;TResponse&gt;(IQuery&lt;TResponse&gt; query, CancellationToken cancellationToken = default);<br>}<br><br>public interface IQueryHandler&lt;in TRequest, TResponse&gt; : IRequestHandler&lt;TRequest, TResponse&gt; where TRequest : IQuery&lt;TResponse&gt;<br>{ }</pre><p>The establishment of the QueryBus implementation, as well as the incorporation of commands and events, can be explored <a href="https://github.com/madslundt/NetCoreMediatrSample/tree/master/Infrastructure/CQRS">here</a>.</p><h3>Strongly typed ids</h3><p>Utilizing strongly typed ids offers us the flexibility to generate identifiers like UserId and UserTaskId. This approach proves valuable in preventing the confusion that might arise when dealing with ids across various data models.</p><p>In contrast, object ids that are human-readable provide a means to quickly discern the purpose of an id. Achieving this involves a straightforward technique of prefixing the id with a domain identifier, resulting in a format like “us_1234” where “us_” serves as the domain-specific prefix marking it as a user id.</p><p>To address these considerations, I’ve introduced a strongly typed id base entity. This entity serves as an abstract class, assisting in the creation of distinct ids for various data models.</p><pre>public abstract record StronglyTypedIdBaseEntity<br>{<br>    public string Prefix { get; } = null!;<br>    public string Value { get; }<br><br>    public sealed override string ToString() =&gt; Value;<br><br>    protected StronglyTypedIdBaseEntity(string prefix, string value)<br>    {<br>        if (string.IsNullOrWhiteSpace(prefix))<br>        {<br>            throw new InvalidStronglyTypedIdException($&quot;{nameof(prefix)} is not defined&quot;);<br>        }<br><br>        if (string.IsNullOrWhiteSpace(value))<br>        {<br>            throw new InvalidStronglyTypedIdException($&quot;{nameof(value)} is not defined&quot;);<br>        }<br><br>        Prefix = prefix;<br>        Value = value.ToLowerInvariant();<br>    }<br><br>    public static T New&lt;T&gt;() where T : StronglyTypedIdBaseEntity<br>    {<br>        var instance = (T) Activator.CreateInstance(typeof(T), Ulid.NewUlid().ToString())!;<br>        var id = $&quot;{instance.Prefix}{instance.Value}&quot;;<br>        return (T) Activator.CreateInstance(typeof(T), id)!;<br>    }<br><br>    public static string GetPlaceholder&lt;T&gt;() where T : StronglyTypedIdBaseEntity<br>    {<br>        var instance = (T) Activator.CreateInstance(typeof(T), Ulid.NewUlid().ToString())!;<br>        return $&quot;{instance.Prefix}{new string(&#39;x&#39;, instance.Value.Length)}&quot;;<br>    }<br><br>    public bool IsValid()<br>    {<br>        if (!Value.StartsWith(Prefix, StringComparison.InvariantCultureIgnoreCase))<br>        {<br>            return false;<br>        }<br><br>        return Ulid.TryParse(Value.Substring(Prefix.Length), out _);<br>    }<br>}</pre><p>In my illustration, I’m employing Ulid for the generation of random yet lexicographically sortable ids. This choice ensures a well-organized order for these identifiers. Consequently, crafting a UserId or UserTaskId becomes an effortless process.</p><pre>public record UserId(string Value) : StronglyTypedIdBaseEntity(&quot;us_&quot;, Value);<br><br>public record UserTaskId(string Value) : StronglyTypedIdBaseEntity(&quot;ut_&quot;, Value);</pre><p>Employing strongly typed ids along with prefixes presents an additional advantage: the ability to validate the id’s authenticity before querying the database. This verification can be easily accomplished by examining the prefix. This rationale is precisely why the IsValid function has been integrated into the base entity.<br>More of the strongly typed id implementation can be seen <a href="https://github.com/madslundt/NetCoreMediatrSample/tree/master/Infrastructure/StronglyTypedIds">here</a>.</p><h3>Model validation</h3><p>Implementing validation for input models brings about multiple benefits, including maintaining the cleanliness of your functions and establishing rules for models outside the scope of your business logic.</p><p>Among the libraries available, one of my preferred choices is FluentValidation. This tool enables the separation of validation concerns from the business logic, offering a range of basic validation rules along with the flexibility to craft custom validation rules tailored to your needs.</p><p>Remarkably, MediatR goes a step further by facilitating model validation when employing in-process messaging. This functionality is achieved through the utilization of pipeline behaviors.</p><p>An illustrative instance of a custom validation rule involves verifying the correctness of an id within the specific domain, leveraging the IsValid function.</p><p>By adopting these practices and tools, you can ensure robust validation practices while maintaining a clean and organized codebase.</p><pre>public static void IdMustBeValid&lt;TId, T&gt;(this IRuleBuilder&lt;T, string&gt; ruleBuilder)<br>    where TId : StronglyTypedIdBaseEntity<br>{<br>    ruleBuilder.Must((_, id, _) =&gt; !string.IsNullOrWhiteSpace(id) &amp;&amp;<br>                                   ((TId) Activator.CreateInstance(typeof(TId), id)!).IsValid())<br>        .WithMessage(<br>            $&quot;Id is not valid. Valid format is {StronglyTypedIdBaseEntity.GetPlaceholder&lt;TId&gt;()}&quot;);<br>}<br><br>public static void OptionalIdMustBeValid&lt;TId, T&gt;(this IRuleBuilder&lt;T, string?&gt; ruleBuilder)<br>    where TId : StronglyTypedIdBaseEntity<br>{<br>    ruleBuilder.Must((_, id, _) =&gt;<br>            string.IsNullOrWhiteSpace(id) || ((TId) Activator.CreateInstance(typeof(TId), id)!).IsValid())<br>        .WithMessage(<br>            $&quot;Id is not valid. Valid format is {StronglyTypedIdBaseEntity.GetPlaceholder&lt;TId&gt;()}&quot;);<br>}</pre><h3>Database models</h3><p>Once the foundational infrastructure is in place, the subsequent step involves establishing our data model. In the context of this article, I’ve devised a straightforward illustration involving users and their associated tasks.</p><p>In this setup, a user is characterized by an id, a status, as well as their first and last names. On the other hand, a user task is delineated by an id, a status, a title, a description, the author, and potentially an assigned user.</p><p>What’s common across most tables is the necessity for an id and a timestamp denoting when the record was created. To circumvent the repetitive implementation of these properties across all tables, I’ve introduced a concept called BaseModel.</p><p>The BaseModel streamlines the process by automatically incorporating the fundamental columns and initializing the requisite data. This approach not only simplifies the code but also ensures consistency across the data models, promoting a well-structured and organized system.</p><pre>public abstract class BaseModel&lt;TId&gt; where TId : StronglyTypedIdBaseEntity<br>{<br>    public TId Id { get; } = StronglyTypedIdBaseEntity.New&lt;TId&gt;();<br><br>    public DateTime CreatedUtc { get; } = DateTime.UtcNow;<br>}</pre><p>Now we can create User and UserTask tables using our BaseModel</p><pre>public class User : BaseModel&lt;UserId&gt;<br>{<br>    public string FirstName { get; set; } = null!;<br>    public string LastName { get; set; } = null!;<br><br>    public UserStatusRef StatusRef { get; set; } = null!;<br>    public UserStatusEnum StatusEnum { get; set; } = UserStatusEnum.Active;<br><br>    public ICollection&lt;UserTask&gt; CreatedUserTasks { get; set; }<br>    public ICollection&lt;UserTask&gt; AssignedToUserTasks { get; set; }<br>}</pre><pre>public class UserTask : BaseModel&lt;UserTaskId&gt;<br>{<br>    public string Title { get; set; } = null!;<br>    public string Description { get; set; } = null!;<br><br>    public TaskStatusRef StatusRef { get; set; } = null!;<br>    public TaskStatusEnum StatusEnum { get; set; } = TaskStatusEnum.Created;<br><br>    public User CreatedByUser { get; set; } = null!;<br>    public UserId CreatedByUserId { get; set; } = null!;<br><br>    public User? AssignedToUser { get; set; }<br>    public UserId? AssignedToUserId { get; set; }<br>}</pre><p>A crucial aspect to consider when employing strongly typed ids is to inform our Object-Relational Mapping (ORM), such as Entity Framework, about the mechanism to extract and insert the specific strongly typed ids.</p><p>To address this requirement, an extension method has been implemented that proves invaluable when configuring the entity type. This extension method serves as a tool to guide the ORM on the appropriate procedures for handling these strongly typed ids during extraction and insertion processes.</p><p>By incorporating this extension into the entity type configuration, we establish a seamless integration that ensures our data model and its unique id structure are effectively managed by the ORM, contributing to a well-aligned and functional system.</p><pre>public static PropertyBuilder&lt;TProperty&gt; SupportStronglyTypedId&lt;TProperty&gt;(this PropertyBuilder&lt;TProperty&gt; property)<br>      where TProperty : StronglyTypedIdBaseEntity?<br>  {<br>      return property.HasConversion(<br>          id =&gt; id != null ? id.ToString() : null,<br>          value =&gt; value != null ? (TProperty) Activator.CreateInstance(typeof(TProperty), value) : null<br>      );<br>  }</pre><h3>Components/features</h3><p>Moving forward, the focus shifts to constructing the components or features within which our business logic can thrive. This is where we bring to life our MediatR objects and handlers, enabling the execution of our application’s core operations.</p><p>Let’s consider an example: the creation of a user task.</p><pre>public class CreateUserTask<br>{<br>    public class Command : ICommand&lt;Result&gt;<br>    {<br>        public string Title { get; init; } = null!;<br>        public string Description { get; init; } = null!;<br>        public string UserId { get; init; } = null!;<br>        public string? AssignedUserId { get; init; }<br>    }<br><br>    public class CreateUserTaskValidator : AbstractValidator&lt;Command&gt;<br>    {<br>        public CreateUserTaskValidator()<br>        {<br>            RuleFor(command =&gt; command.Title).NotEmpty();<br>            RuleFor(command =&gt; command.Description).NotEmpty();<br>            RuleFor(command =&gt; command.UserId).IdMustBeValid&lt;UserId, Command&gt;();<br>            RuleFor(command =&gt; command.AssignedUserId).OptionalIdMustBeValid&lt;UserId, Command&gt;();<br>        }<br>    }<br><br>    public class Result<br>    {<br>        public string Id { get; init; } = null!;<br>    }<br><br>    public class Handler : ICommandHandler&lt;Command, Result&gt;<br>    {<br>        private readonly DatabaseContext _db;<br><br>        public Handler(DatabaseContext db)<br>        {<br>            _db = db;<br>        }<br><br>        public async Task&lt;Result&gt; Handle(Command request, CancellationToken cancellationToken)<br>        {<br>            var task = new UserTask<br>            {<br>                Title = request.Title,<br>                Description = request.Description,<br>                AssignedToUserId = request.AssignedUserId != null ? new UserId(request.AssignedUserId) : null,<br>                CreatedByUserId = new UserId(request.UserId),<br>            };<br><br>            _db.Add(task);<br>            var @event = new UserTaskCreatedEvent<br>            {<br>                UserTaskId = task.Id<br>            };<br>            await _db.SaveChangesAndCommitAsync(cancellationToken, @event);<br><br>            var result = new Result<br>            {<br>                Id = task.Id.ToString()<br>            };<br><br>            return result;<br>        }<br>    }<br>}</pre><p>A typical handler encompasses the following components:</p><ul><li>Command: This serves as the input for the handler, providing the necessary information for the operation.</li><li>Validator: Responsible for validating the input, ensuring it meets the required criteria and constraints.</li><li>Result: This represents the output of the handler’s execution, encapsulating the outcomes or data resulting from the operation.</li><li>Handler: The handler itself is the function that executes once the input has been validated. It carries out the core business logic of the operation.</li></ul><p>Together, these components work in unison to process the input, validate it, execute the designated logic, and produce the expected results.</p><p>A crucial point to emphasize is that whenever a database commit is performed, it’s essential to ensure the publication of a corresponding event. This practice holds true in situations like the creation of a user task, as exemplified by the UserTaskCreatedEvent.</p><h3>Event handling</h3><p>Now that we’ve observed how events can be published through our components, let’s delve into the concept of event handling.</p><p>Events serve as valuable tools for managing side effects within the application that might not be directly related to the initiating action. For instance, when creating a user task, it might be relevant to notify if a user has been assigned to that task.</p><p>One key consideration when publishing an event is to ensure that the event’s description is written in the past tense. Furthermore, the event should encompass just enough information to enable the identification of the changes that occurred. In the example at hand, having the user task id suffices, as additional information can be extracted through subsequent database queries.</p><p>For instance, a pertinent side effect could involve notifying the assigned user about the newly assigned task. This way, events and their handling facilitate the seamless management of such interactions and outcomes within the application.</p><pre>public class NotifyAssignedUserEventHandler : IEventHandler&lt;UserTaskCreatedEvent&gt;<br>{<br>    private readonly DatabaseContext _db;<br><br>    public NotifyAssignedUserEventHandler(DatabaseContext db)<br>    {<br>        _db = db;<br>    }<br><br>    public async Task Handle(UserTaskCreatedEvent @event, CancellationToken cancellationToken)<br>    {<br>        var userTask = await GetTaskWithAssignedUser(@event.UserTaskId, cancellationToken);<br>        if (userTask?.AssignedToUserId is null)<br>        {<br>            return;<br>        }<br><br>        var isAuthorAssignedToUserTask = userTask.AssignedToUserId == userTask.CreatedByUserId;<br>        if (isAuthorAssignedToUserTask)<br>        {<br>            return;<br>        }<br><br>        // TODO: Notify assigned user<br>        Console.WriteLine(<br>            $&quot;{userTask.CreatedByUser.FirstName} has assigned you the task &#39;{userTask.Title}&#39;&quot;);<br>    }<br><br>    private async Task&lt;UserTask?&gt; GetTaskWithAssignedUser(UserTaskId userTaskId, CancellationToken cancellationToken)<br>    {<br>        var result = await _db.UserTasks<br>            .Include(userTask =&gt; userTask.CreatedByUser)<br>            .Where(userTask =&gt; userTask.Id == userTaskId)<br>            .FirstOrDefaultAsync(cancellationToken);<br><br>        return result;<br>    }<br>}</pre><p>Asynchronous event processing is achieved through the utilization of a background service. In our particular scenario, we employ Hangfire to handle tasks asynchronously. This is accomplished by crafting a custom wrapper that inherits from IBackgroundJobBus.</p><pre>public interface IBackgroundJobBus<br>{<br>    Task Enqueue(Expression&lt;Action&gt; methodCall);<br>    Task Schedule(Expression&lt;Action&gt; methodCall, TimeSpan timeSpan);<br>}</pre><p>All these functionalities are managed within the Infrastructure layer, enabling the placement of background workers external to the API. This separation can alleviate some of the API’s workload and promote application scalability by allowing the creation of new instances alongside the API. <br>This can be effortlessly accomplished by utilizing the extensions established within our <a href="https://github.com/madslundt/NetCoreMediatrSample/tree/master/Infrastructure">infrastructure</a> to incorporate the required dependencies.</p><pre>builder.Services<br>    .AddDataModel(dataModelConnectionString)<br>    .AddCQRS()<br>    .AddHangfire(hangfireConnectionString)<br>    .AddEventHandlers()<br>    .AddHangfireWorker();</pre><h3>Controllers</h3><p>Controllers assume a pivotal role by not only establishing endpoints and routing incoming requests to the appropriate handlers but also by crafting responses that include the accurate status codes and corresponding data.<br>This is an example of the controller for user tasks containing creating of a user task and getting a user task by id.</p><pre>[ApiController]<br>[Route(&quot;/api/user-tasks&quot;)]<br>public class UserTaskController : ControllerBase<br>{<br>    private readonly IQueryBus _queryBus;<br>    private readonly ICommandBus _commandBus;<br><br>    public UserTaskController(IQueryBus queryBus, ICommandBus commandBus)<br>    {<br>        _queryBus = queryBus;<br>        _commandBus = commandBus;<br>    }<br><br>    [HttpPost, Route(&quot;&quot;, Name = nameof(CreateUserTask))]<br>    public async Task&lt;ActionResult&lt;CreateUserTask.Result&gt;&gt; CreateUserTask(<br>        [FromBody] CreateUserTask.Command command,<br>        CancellationToken cancellationToken<br>    )<br>    {<br>        var result = await _commandBus.Send(command, cancellationToken);<br><br>        return Created($&quot;/api/user-tasks/{result.Id}&quot;, result);<br>    }<br><br>    [HttpGet, Route(&quot;{userTaskId}&quot;, Name = nameof(GetUserTask))]<br>    public async Task&lt;ActionResult&lt;GetUserTask.Result&gt;&gt; GetUserTask(<br>        [FromRoute] string userTaskId,<br>        CancellationToken cancellationToken<br>    )<br>    {<br>        var query = new GetUserTask.Query<br>        {<br>            UserTaskId = userTaskId<br>        };<br>        var result = await _queryBus.Send(query, cancellationToken);<br><br>        return Ok(result);<br>    }<br>}</pre><p>This division organizes the project into six distinct sections:</p><ul><li>Api: The controllers responsible for managing API endpoints.</li><li>Components: The handlers that encapsulate the core business logic.</li><li>Data Model: The database entities that define the structure of the data.</li><li>Event Handlers: The handlers dedicated to processing events.</li><li>Events: The event models that encapsulate specific changes or occurrences.</li><li>Infrastructure: Setting up the necessary configurations and infrastructure components.</li></ul><p>This article provides only a glimpse of the implemented features within the sample project. If you’re interested in exploring further details, I encourage you to visit the corresponding <a href="https://github.com/madslundt/NetCoreMediatrSample/tree/master">GitHub repository</a>. There, you’ll find a more comprehensive overview of the project’s functionalities and implementation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b15992e09ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices with event sourcing using .NET Core]]></title>
            <link>https://medium.com/@madslundt/microservices-with-event-sourcing-using-net-core-33e3074171f5?source=rss-20be1ee61cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/33e3074171f5</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[event-sourcing]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Mads Engel Lundt]]></dc:creator>
            <pubDate>Fri, 28 Aug 2020 09:14:56 GMT</pubDate>
            <atom:updated>2020-08-28T09:31:49.748Z</atom:updated>
            <content:encoded><![CDATA[<h3>Microservices with event sourcing using .NET Core</h3><p>I spend some time last year implementing an example project on how to structure an API using microservices in .NET Core. In my summer vacation, I began looking into how I could improve and add event sourcing to the project</p><p>Before diving into the project details let’s get an overview of the project</p><p>The project’s main focus is implementing an API using a microservice structure. This means it can still be applied in a monolithic application but the theories from microservices can be applied to the application. <br>The next step was to avoid boiler code and code duplication as much as possible and focus on clean code. To have this, it is important to split the logic in different modules to reuse logic in other places. This also gives the option to only include modules that are needed in the application.</p><p>Most of this was already introduced in the project last year.<br>After some days of relaxing, I began to look into event sourcing. I looked at different libraries for implementing event sourcing into a .NET core application but I had a hard time finding a solution that could fit into my project.<br>I decided to give it a try and implement my version on how to include event sourcing into the microservice example project.</p><p>Link to <a href="https://github.com/madslundt/NetCoreMicroservicesSample">Github repository</a>.<br>Keep in mind that the repo is work in progress.</p><h3>Event sourcing</h3><p>Let’s start by describing what event sourcing is:</p><blockquote>“The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.” — <a href="https://martinfowler.com/eaaDev/EventSourcing.html">Martin Fowler</a></blockquote><p>This means that event sourcing never mutate states in the application but stores a sequence of events in an append-only event log called event store.</p><p>In this way, data is never lost and all changes persist.</p><h3>Application</h3><p>The example application is a simple movie database with user reviews.<br>To do so, the application needs to store entities such as movies, reviews, and users.<br>The application is quite simple and only support create and delete entities.<br>Authentication and authorization have not been added to the application.</p><p>Let’s start by getting an overview of how the application is structured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4eklXPD5bgkzG0VMHL_M5w.png" /><figcaption>Project showing the API gateway connected to 3 different services</figcaption></figure><p>Starting from the top of the overview we see that there is a request going into the API gateway.</p><p>The API gateway is the only public API and its job is to route traffic to the correct service.<br>In this example, Ocelot has been used because it is easy to set up and configure.</p><p>Each microservice is focused to be as independent as possible.<br>That means each microservice has its database, event handler, event store (if used), etc.</p><p>The only shared dependency between the micro services is the message broker.<br>The micro services can run without the message broker because events are stored within the micro service before being published to the message broker. However, the events can not be published to other micro services before the message broker is up and running. The important thing here is that events are never lost due to connection error to the message broker.</p><h4>User service</h4><p>User service is handling all users in the system.<br>It is possible to create and delete users in the system.</p><p>The service is hosting a database where states are mutating. That means read and write operations happen to the same database.<br>Event sourcing with an event store was left out of this service.</p><p>User service is still able to communicate with the other services through events even though it is not using event sourcing with an event store.<br>The only difference is that this service does not make use of aggregates to publish events.<br>Publishing events, however, happens every time the database changes because the database context requires an event before it can save changes to the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78e6879419fb431c89ee2160794f404c/href">https://medium.com/media/78e6879419fb431c89ee2160794f404c/href</a></iframe><p>To commit events this service makes use of Outbox.<br>Outbox is used as a landing zone for events before they are published to the message broker. The Outbox store is then hosted near or within the service to reduce the chance of connection errors and to reduce latency by waiting for the event being sent and received by the message broker.<br>The message broker will later receive the event by the service having a background worker looping through all events that have not been published to the message broker. Once an event has been successfully published to the message broker the event in the Outbox database is either flagged or removed so it is not going to be processed later.</p><blockquote>The major difference between event sourcing with an event store and Outbox is that event sourcing is meant to be a permanent and immutable store of domain events, while the Outbox database is meant to be highly ephemeral and only be a landing zone for domain events to be captured inside change events and forwarded to downstream consumers.</blockquote><p>Using the infrastructure of the project makes it easy to customize the service to only include what it needs</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c30903a826488e780cbaf6dd808a469c/href">https://medium.com/media/c30903a826488e780cbaf6dd808a469c/href</a></iframe><h4>Movie service and review service</h4><p>Movie service is handling all movies in the system.<br>It is possible to create and delete movies in the system.</p><p>Review service is handling all reviews added to movies.<br>It is possible to create and delete reviews in the system.</p><p>The services make use of the same dependencies as the user service but also makes use of event sourcing with an event store to not mutate data but keep all changes.<br>That means when a movie or review is removed the data is still kept but just not accessible to the outside.<br>This data can then later be restored so it is accessible to the user if needed.</p><p>To do so, a read and a write database are used for this.</p><p>The write database is an immutable store and all events published from the service are appended to the write database as event streams.<br>To do so concepts from DDD (Domain-Drive-Design) are applied to the services. With these concepts, it is possible to apply a command to an aggregate which then produces one or more events. An aggregate can populate (re-hydrate) its state by sequential application of an event stream.</p><p>The read database is mutable and may be updated when events are received by the service. This is done through update handlers who are subscribing to events.</p><p>The service becomes slightly more complex when adding event sourcing with an event store. <br>It is also important to keep in mind that there will be some inconsistency between the write- and the read database in a smaller time slot. This is due to the read database needs to wait for the event to be published to the message broker and received by the service again before updating the read database.</p><p>Using the infrastructure makes it easy to add event sourcing with an event store to the service</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e328a8bfec07069f1e86beea92df48b/href">https://medium.com/media/7e328a8bfec07069f1e86beea92df48b/href</a></iframe><h4>Conclusion</h4><p>This project was created to adapt different technologies into a Web API in a very clean and structured way.</p><p>The focus of the whole project is to have each micro service as independent as possible but still able to communicate with other services without having to depend on them.</p><p>Event sourcing with an event store has a lot to offer but comes with a cost of complexity and inconsistency in smaller time slots.<br>I believe that event sourcing has its purposes in some projects but you should be careful when and how to implement event sourcing into the project.</p><p>Link to <a href="https://github.com/madslundt/NetCoreMicroservicesSample">Github repository</a>.<br>Keep in mind that the repo is work in progress.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33e3074171f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>