This project demonstrates a sophisticated .NET implementation of the Factory Pattern to generate invoices in multiple file formats (PDF, TXT, and CSV). It showcases the true power and flexibility of the Factory Pattern in enterprise-level applications.
- Advanced Factory Implementation: Fully-featured factory with logging, validation, and extensibility
- Decoupled Architecture: Client code is completely decoupled from concrete implementations
- Centralized Object Creation: All object instantiation logic is centralized in the factory
- Easy Extensibility: Adding new formats requires minimal code changes
- Dependency Injection Integration: Seamless integration with .NET DI container
- NSwag Integration: Comprehensive API documentation with Swagger UI
- Interactive API Testing: Built-in Swagger UI for testing endpoints
- Detailed Endpoint Documentation: Rich documentation for all API endpoints
- PDF Generation: Professional-grade PDF documents using QuestPDF
- CSV Generation: Structured comma-separated data format
- TXT Generation: Simple text-based invoice output
The Factory Pattern implementation in this project demonstrates several advanced concepts:
- Interface-Based Design: All generators implement the IInvoiceGenerator interface
- Factory Interface: The IInvoiceGeneratorFactory defines the contract for creating generators
- Concrete Factory: The InvoiceGeneratorFactory implements the factory logic with additional utility methods
- Dependency Injection: All components are registered with the .NET DI container for proper lifecycle management
- Clone the repository:
git clone https://github.com/MrEshboboyev/FactoryPattern.git
- Navigate to the project directory:
cd FactoryPattern
- Restore dependencies:
dotnet restore
Start the application:
cd FactoryPattern.API
dotnet run
The application will start on http://localhost:5143
by default.
GET /api/invoice/{id:guid}/{format}
Generates an invoice in the specified format using the Factory Pattern.
Parameters:
id
: The unique identifier for the invoice (GUID)format
: The format in which to generate the invoice (Pdf, Txt, Csv)
Example:
GET /api/invoice/123e4567-e89b-12d3-a456-426614174000/Pdf
GET /api/invoice/formats
Returns a list of all invoice formats supported by the Factory Pattern implementation.
The application uses the Factory Pattern to create appropriate invoice generators based on the requested format:
// Client code is completely decoupled from concrete implementations
var generator = invoiceGeneratorFactory.CreateInvoiceGenerator(format);
var invoiceData = generator.GenerateInvoice(id);
var contentType = generator.GetContentType();
var fileName = $"Invoice_{id}.{format.ToString().ToLower()}";
return Results.File(invoiceData, contentType, fileName);
To add support for a new file format:
-
Create a new class implementing the IInvoiceGenerator interface:
public class ExcelInvoiceGenerator : IInvoiceGenerator { public byte[] GenerateInvoice(Guid invoiceId) { // Implementation for generating Excel invoice } public string GetContentType() => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; }
-
Add the new file type to the InvoiceFormat enum:
public enum InvoiceFormat { Pdf, Txt, Csv, Excel // New format }
-
Update the InvoiceGeneratorFactory to include the new type:
public IInvoiceGenerator CreateInvoiceGenerator(InvoiceFormat invoiceFormat) { return invoiceFormat switch { InvoiceFormat.Pdf => _serviceProvider.GetRequiredService<PdfInvoiceGenerator>(), InvoiceFormat.Txt => _serviceProvider.GetRequiredService<TxtInvoiceGenerator>(), InvoiceFormat.Csv => _serviceProvider.GetRequiredService<CsvInvoiceGenerator>(), InvoiceFormat.Excel => _serviceProvider.GetRequiredService<ExcelInvoiceGenerator>(), // New format _ => throw new ArgumentException("Invalid/Unsupported invoice format", nameof(invoiceFormat)) }; }
-
Register the new generator in Program.cs:
builder.Services.AddTransient<ExcelInvoiceGenerator>();
- Single Responsibility Principle: Each generator focuses solely on its specific format
- Open/Closed Principle: Easy to extend without modifying existing code
- Loose Coupling: Client code depends on abstractions, not concrete implementations
- Testability: Each component can be easily unit tested
- Maintainability: Changes to generation logic are isolated to specific classes
- Scalability: New formats can be added without affecting existing functionality
The project includes comprehensive API documentation powered by NSwag:
- Start the application
- Navigate to
http://localhost:5143/swagger
to access the Swagger UI - Explore and test all available endpoints
Contributions are welcome! Follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m "Add feature"
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
This project is licensed under the MIT License. See the LICENSE
file for more details.
- QuestPDF - For excellent PDF generation capabilities
- NSwag - For API documentation generation
- Factory Design Pattern - For the pattern inspiration
Happy coding! 🚀