This project demonstrates a .NET implementation of the Factory Pattern to generate invoices in multiple file formats (PDF, TXT, and CSV). It uses QuestPDF to create professional-grade PDF documents and follows the Repository Pattern to ensure clean and maintainable data access.
- Factory Pattern: Simplifies the creation of invoices in various file formats.
- Invoice Generation: Supports generating invoices as PDF, TXT, and CSV files.
- QuestPDF Integration: Produces high-quality and customizable PDF invoices.
- Repository Pattern: Provides a structured and maintainable data access layer.
- Extensible Design: Easily add support for new file formats or invoice types.
- Clone the repository:
git clone https://github.com/MrEshboboyev/FactoryPattern.git
- Navigate to the project directory:
cd FactoryPattern
- Restore dependencies:
dotnet restore
The application uses the Factory Pattern to generate invoices in different formats. Below is an example of how to generate invoices in endpoint:
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 support a new file format:
- Create a new class implementing the
IInvoiceGenerator
interface. - Add the new file type to the
InvoiceFormat
enum. - Update the
InvoiceGeneratorFactory
to include the new type.
Example:
public class ExcelInvoiceGenerator : IInvoiceGenerator
{
public byte[] GenerateInvoice(Guid invoiceId)
{
// Logic to generate an Excel invoice
}
}
- PDF: Generated using QuestPDF.
- CSV: Comma-separated structured invoice data.
- TXT: Simple text-based invoice output.
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.
Happy coding! 🚀