This project demonstrates the implementation of the Facade Design Pattern in C#. The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. It simplifies the interaction with complex systems by creating a higher-level interface that makes the system easier to use.
The project is structured as follows:
- Contains the core entities of the application:
Product,Customer, andOrder. - These classes represent the data models used in the application.
ProductService: Handles operations related toProduct.CustomerService: Handles operations related toCustomer.OrderService: Handles operations related toOrder.- These services interact with the fake database to fetch and manipulate data.
OrderFacade: Acts as a unified interface that simplifies order placement and retrieval.- Internally uses
ProductService,CustomerService, andOrderService.
EndPointsWithFacade: Implements order-related operations (Place Order,Get Orders) using the facade.EndPointsWithOutFacade: Implements the same operations without the facade, demonstrating the complexity it simplifies.
- Allows customers to place an order for a product.
- Validates whether the customer and product exist before creating the order.
- Fetches the list of orders with detailed information about the customer and product.
- Direct calls to multiple services increase code complexity.
- Developers need to understand the implementation details of each service.
- A single
OrderFacadesimplifies the interaction between services. - Encapsulates the complexity of coordinating
ProductService,CustomerService, andOrderService.
- .NET 6.0 or higher.
- Visual Studio or any C# IDE.
- Clone the repository:
git clone <repository-url>
- Navigate to the project directory.
- Run the application:
dotnet run
- Use Swagger UI to test the endpoints:
- Access Swagger at:
https://localhost:<port>/swagger.
- Access Swagger at:
-
POST /api/facade/
Place an order.
Query Parameters:customerId(int)productId(int)quantity(int)
-
GET /api/facade/
Get the list of orders.
-
POST /api/withoutfacade/
Place an order without using the facade.
Query Parameters:customerId(int)productId(int)quantity(int)
-
GET /api/withoutfacade/
Get the list of orders without using the facade.
POST /api/facade?customerId=1&productId=1&quantity=2
Response:
"Order placed successfully!"GET /api/facade
Response:
[
{
"id": 1,
"productId": 1,
"customerId": 1,
"quantity": 2,
"customer": {
"id": 1,
"name": "John Doe"
},
"product": {
"id": 1,
"name": "Laptop",
"price": 1000.0
}
}
]- C#
- ASP.NET Core Minimal API
- Swagger for API documentation
Develop By AliCharper
Refactor by Mohammad.
Feel free to reach out for feedback or collaboration!