Logo

dev-resources.site

for different kinds of informations.

An example of Consumer-Driven Development

Published at
1/7/2025
Categories
programming
backend
productivity
learning
Author
samuelko123
Main Article
https://dev.to/samuelko123/consumer-driven-development-3k87
Author
11 person written this
samuelko123
open
An example of Consumer-Driven Development

It's my first time sharing my experience.
Let me know if there's anything I can improve.

Introduction

"Consumer-Driven Development" is a made-up terminology.

In this mindset, we define what the "consumer" needs first, then orchestrate the application to satisfy the need.

The "consumer" here can be perceived as the presentation layer, such as API controllers.

Example

The existing architecture

We have an application.
It calls an API endpoint periodically and logs the data:

Component Diagram

The diagram is drawn using PlantUML.

PlantUML Text
@startuml Example App skinparam componentStyle rectangle cloud "API Endpoint" cloud "Elastic" rectangle "Application" { [Gateway] [API Client] [Repository] [Cron Job] } [API Endpoint] -> [Gateway] [Gateway] --> [API Client] : HTTP Response [API Client] --> [Repository] : Product DTO [Repository] --> [Cron Job] : Product [Cron Job] -> [Elastic] : Logs @enduml 
Enter fullscreen mode Exit fullscreen mode

The new business requirement

The API endpoint provides a field called status.
It can be Approved, Disapproved, Pending.

We want to log the total number of Approved products.

Solution #1 - The data-flow approach

Conceptual diagram

PlantUML Text
@startuml skinparam componentStyle rectangle skinparam defaultTextAlignment center component [Repository] as "**Step 3**\nRepository" component [CronJob] as "**Step 4**\nCronJob" [API Client] -> [Repository] : **Step 1**\nProductDTO [Repository] -> [CronJob] : **Step 2**\nProduct @enduml 
Enter fullscreen mode Exit fullscreen mode

Step 1:
We want to obtain the data from the API endpoint.
Therefore, we create ProductDTO.Status as a text field.

Step 2:
We want meaningful data in our domain object.
Therefore, we create Product.Status as an enum.

enum Status { Approved, Disapproved, Pending, } 
Enter fullscreen mode Exit fullscreen mode

Step 3:
Let Repository do the translation.
(Note: Exception handling is omitted.)

var products = productDtos.Select(dto => { return new Product() { Status = Enum.Parse<Status>(dto.Status) }; }); 
Enter fullscreen mode Exit fullscreen mode

Step 4:
Finally, we can log the number of approved products in CronJob.

logger.Information( "Number of approved products: {ApprovedProductCount}", products.Count(product => product.Status == Status.Approved)); 
Enter fullscreen mode Exit fullscreen mode

Solution #2 - The consumer-driven approach

Conceptual diagram

PlantUML Text
@startuml skinparam componentStyle rectangle skinparam defaultTextAlignment center component [Repository] as "**Step 2**\nRepository" component [CronJob] as "**Step 1**\nCronJob" [API Client] -> [Repository] : **Step 2**\nProductDTO [Repository] -> [CronJob] : **Step 1**\nProduct @enduml 
Enter fullscreen mode Exit fullscreen mode

Step 1:
The consumer, CronJob, needs to know whether a product is approved.
Therefore, we create Product.IsApproved as a boolean field.

logger.Information( "Number of approved products: {ApprovedProductCount}", products.Count(product => product.IsApproved)); 
Enter fullscreen mode Exit fullscreen mode

Step 2:
The provider, Repository, will figure out how to populate the new IsApproved field.
To do so, we create ProductDTO.Status as a text field.

var products = productDtos.Select(dto => { return new Product() { IsApproved = dto.Status == "Approved" }; }); 
Enter fullscreen mode Exit fullscreen mode

Result

In solution #1, the enum value Status.Disapproved and Status.Pending are unused. We spent extra effort to handle exceptions when translating the text field into enum.

In solution #2, the use of boolean field makes the code much simpler. One could argue that it lacks data validation and scalability, but that is not within the requirement for today.

Conclusion

Using Consumer-Driven Development, we ensure that our effort is spent to meet the requirement, and nothing else.

Happy to hear your thoughts. πŸ˜‰

productivity Article's
30 articles in total
Productivity tools and practices enhance efficiency and help individuals and teams achieve more in less time.
Favicon
How to set better 2025 Goals
Favicon
Logs, Why Do I Need You?: On the Importance of Mapping User Actions Within a System
Favicon
Claude vs Gemini vs ChatGPT vs Mistral vs Perplexity vs CoPilot: The AI Showdown
Favicon
Top 10 Workflow Automation Tools for Developers in 2025.
Favicon
WakaTime User? Get your WakaTime Wrapped 2024! πŸš€
Favicon
Cobra effect and what we can do to mitigate it
Favicon
Is AI Making Technical Writers Obsolete?
Favicon
AI Basics: Understanding Artificial Intelligence and Its Everyday Applications
Favicon
An example of Consumer-Driven Development
Favicon
Metas 2025 ✨
Favicon
Mastering Cloud Containerization: A Step-by-Step Guide to Deploying Containers in the Cloud
Favicon
How to Implement Project Time Tracking Without Micromanaging
Favicon
Simplify Your Admin Panel Setup with Basemulti: A Developer-First Solution
Favicon
[Boost]
Favicon
Mastering AWS Lambda Performance: Advanced Optimization Strategies for 2025
Favicon
Anonymous Feedback in Retros: When, Why, and How
Favicon
Lessons from Law 1 of "The 48 Laws of Power"
Favicon
I Wish I Knew These Git Commands Earlier!
Favicon
Maximizing Productivity: The Benefits of Planning Your Day as a Software Engineer
Favicon
Check out this post to present your code demos without stress
Favicon
Unlock Laravel Code Generation (Part 3): Automating Validation from Your Database Schema πŸ€―πŸš€
Favicon
OKRs (Objectives and Key Results) com KPIs (Key Performance Indicators)
Favicon
Searching for a Word
Favicon
Your Most Important Skill for 2025
Favicon
πŸš€ Tips for Every Level of Software Developer: How to Thrive in 2025
Favicon
Revolutionizing Global Trade: iCustoms Joins TradeTech Accelerator
Favicon
App for sharing time slots for meetings
Favicon
A Practical Approach to Problem-Solving in JavaScript
Favicon
New Year , New ME
Favicon
The future of algorithmic trading is here, and it’s powered by AI. πŸš€

Featured ones: