Power of Builder + Facade Patterns Using an Item API

Building Robust APIs in .NET Core

DotNet Full Stack Dev
4 min readOct 2, 2024

When designing APIs in .NET Core, we aim for simplicity, flexibility, and scalability. Two important patterns that can help achieve these goals are the Builder and Facade patterns. In this blog, we’ll explore how these two design patterns can work together to improve your API’s design, using an Item API as an example.

Why Design Patterns Matter?

Design patterns help us standardize solutions to recurring problems, making the code more maintainable, flexible, and easier to read. Let’s dive into how Builder and Facade patterns can aid in API development.

Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. Uncover more by visiting our https://dotnet-fullstack-dev.blogspot.com reach out for further information.

What Is the Builder Pattern?

The Builder Pattern is a creational pattern that allows for the step-by-step construction of complex objects. It separates the creation logic from the actual product class, enabling flexible object creation without constructor overloads.

Think of it as assembling furniture. You have a manual (the builder) that instructs you on how to assemble parts step-by-step.

Builder Pattern in Action: Creating an Item Object

Imagine you’re working on an Item API where the client needs to create items with various optional parameters. Instead of passing all parameters through a constructor or setters, we can use the Builder Pattern to simplify object creation.

public class Item
{
public string Name { get; private set; }
public string Description { get; private set; }
public decimal Price { get; private set; }

private Item() { }

public class ItemBuilder
{
private Item item = new Item();

public ItemBuilder WithName(string name)
{
item.Name = name;
return this;
}

public ItemBuilder WithDescription(string description)
{
item.Description = description;
return this;
}

public ItemBuilder WithPrice(decimal price)
{
item.Price = price;
return this;
}

public Item Build()
{
return item;
}
}
}

How It Works:

  • The ItemBuilder provides methods like WithName(), WithDescription(), and WithPrice() to build the Item object step by step.
  • Once all fields are set, Build() is called to return the fully constructed Item object.

This pattern allows the API consumer to create items flexibly and avoids constructor clutter.

What Is the Facade Pattern?

The Facade Pattern is a structural pattern that provides a simplified interface to a complex system. Imagine it like the dashboard of a car — instead of interacting with each engine component individually, you interact through simplified controls like the steering wheel, pedals, and buttons.

Applying the Facade Pattern to Simplify Item Operations

In our Item API, there could be multiple complex services like ItemService, DiscountService, and InventoryService. To simplify interaction, we’ll create a Facade that provides an easy-to-use interface for clients.

public class ItemFacade
{
private readonly ItemService _itemService;
private readonly DiscountService _discountService;
private readonly InventoryService _inventoryService;

public ItemFacade(ItemService itemService, DiscountService discountService, InventoryService inventoryService)
{
_itemService = itemService;
_discountService = discountService;
_inventoryService = inventoryService;
}

public ItemDTO GetItemDetails(int itemId)
{
var item = _itemService.GetItemById(itemId);
var discount = _discountService.GetDiscountForItem(itemId);
var stock = _inventoryService.GetStockForItem(itemId);

return new ItemDTO
{
Name = item.Name,
Description = item.Description,
Price = item.Price - discount.Amount,
StockAvailable = stock
};
}
}

How It Works:

  • The ItemFacade simplifies the interaction between the client and complex services by providing a unified GetItemDetails() method.
  • Internally, it coordinates between multiple services like ItemService, DiscountService, and InventoryService, but the client doesn't need to worry about those complexities.

Combining Builder and Facade: A Seamless API Experience

Here’s where the magic happens. When used together, the Builder Pattern simplifies object creation, while the Facade Pattern streamlines complex operations. This combination can greatly enhance the design of your Item API by keeping the client-side code clean and intuitive.

How the API Flow Looks:

  1. Client Uses Builder: The client uses the ItemBuilder to create an item easily, setting only the properties they need.
  2. Client Interacts with Facade: Once the item is created, the client interacts with the ItemFacade to retrieve additional details like discount and stock information, without worrying about how those details are fetched.

Benefits of Using Builder and Facade Together

  • Flexibility: The Builder Pattern provides flexibility by allowing step-by-step construction of objects, while the Facade Pattern hides complex service interactions.
  • Maintainability: By separating responsibilities, your code becomes easier to maintain. The Facade simplifies service interaction logic, and the Builder keeps object creation clean.
  • Scalability: Adding new features or services becomes easier without affecting existing code. You can extend the Facade or Builder without changing the client code.

Conclusion: Enhancing API Design with Design Patterns

Both the Builder and Facade patterns are powerful tools in the developer’s toolkit, especially in the world of API development. They enhance code maintainability, readability, and scalability while providing a clean interface for clients to work with.

By implementing these patterns in your Item API, you’ll make your API more user-friendly and adaptable to future requirements. The result? A well-architected, efficient API that’s ready to handle complex scenarios while keeping the client’s code clean and simple.

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet