Sobes.tech
Junior — Middle

What types of facades exist in the .NET ecosystem and how are they used?

sobes.tech AI

Answer from AI

In the .NET ecosystem, the "Facade" pattern is used to simplify interaction with complex subsystems by providing a simple interface.

Types of facades:

  1. Classic facade — wraps a complex system in a single class with simple methods.
  2. Facade for libraries and APIs — simplifies working with external libraries, hiding implementation details.
  3. Facade for application layers — combines multiple services or repositories, providing a unified interface.

Usage:

  • Simplifies client code.
  • Hides complex logic and dependencies.
  • Improves support and extensibility.

Example of a facade for database operations:

public class DatabaseFacade
{
    private readonly UserRepository _userRepo;
    private readonly OrderRepository _orderRepo;

    public DatabaseFacade()
    {
        _userRepo = new UserRepository();
        _orderRepo = new OrderRepository();
    }

    public User GetUser(int id) => _userRepo.GetById(id);
    public void SaveOrder(Order order) => _orderRepo.Save(order);
}

The client interacts with DatabaseFacade without worrying about repository details.