A demonstration of the Repository Pattern combined with the Factory Pattern in C#. This project showcases how to implement a generic repository with factory-based instantiation for managing entities in a clean, maintainable, and scalable way.
- Overview
- Design Patterns Used
- Project Structure
- Features
- Getting Started
- Usage Examples
- Code Architecture
This project demonstrates a clean architecture approach to data management using two fundamental design patterns:
- Repository Pattern: Abstracts data access logic and provides a collection-like interface for accessing domain objects
- Factory Pattern: Encapsulates the creation logic of repository instances
The implementation uses generic types to create a flexible, reusable data access layer that works with any entity type.
The Repository Pattern mediates between the domain and data mapping layers, acting like an in-memory collection of domain objects. Benefits include:
- Centralized data access logic
- Improved testability
- Separation of concerns
- Consistent API for data operations
The Factory Pattern provides an interface for creating repository instances without specifying their concrete classes. Benefits include:
- Loose coupling
- Centralized object creation
- Easier dependency injection
- Improved maintainability
Repository-Factory-Pattern/
β
βββ Models/
β βββ BaseEntity.cs # Abstract base class for all entities
β βββ Department.cs # Department entity
β βββ Student.cs # Student entity
β
βββ Repositories/
β βββ IRepo.cs # Generic repository interface
β βββ GenericRepo.cs # Generic repository implementation
β
βββ Factories/
β βββ IRepoFactory.cs # Factory interface
β βββ RepoFactory.cs # Factory implementation
β
βββ DITest/
β βββ TastClass.cs # Test/demonstration class
β
βββ Program.cs # Application entry point
- Generic Repository Implementation: Works with any entity that inherits from
BaseEntity - CRUD Operations: Full Create, Read, Update, Delete functionality
- In-Memory Storage: Uses
IList<T>for data storage (easily replaceable with database context) - Type Safety: Strongly-typed operations with compile-time checking
- Factory-Based Creation: Centralized repository instantiation
- Dependency Injection Ready: Designed with DI principles in mind
- .NET Framework 4.x or higher (or .NET Core/5+)
- Visual Studio 2019 or later (or any C# IDE)
- Clone the repository:
git clone https://github.com/SKahasun/Repository_Factory_Pattern- Open the solution in Visual Studio:
cd Repository_Factory_Pattern- Build the solution:
dotnet build- Run the application:
dotnet run// Initialize the factory
IRepoFactory factory = new RepoFactory();
// Create a repository for Department
IRepo<Department> deptRepo = factory.CreateRepo<Department>();
// Create a repository for Student
IRepo<Student> studentRepo = factory.CreateRepo<Student>();// CREATE - Add single entity
var dept = new Department { Id = 1, name = "Computer Science" };
deptRepo.Add(dept);
// CREATE - Add multiple entities
deptRepo.AddRange(new Department[] {
new Department { Id = 2, name = "Mathematics" },
new Department { Id = 3, name = "Physics" }
});
// READ - Get all entities
var allDepartments = deptRepo.GetAll();
// READ - Get single entity by ID
var department = deptRepo.Get(1);
// UPDATE - Modify existing entity
department.name = "Computer Engineering";
deptRepo.Update(department);
// DELETE - Remove entity by ID
deptRepo.Delete(3);All entities inherit from BaseEntity, which provides a common Id property:
public abstract class BaseEntity
{
public int Id { get; set; }
}public interface IRepo<T> where T : BaseEntity
{
List<T> GetAll();
T Get(int id);
void Add(T entity);
void AddRange(IEnumerable<T> entities);
void Update(T entity);
void Delete(int id);
}The factory encapsulates the creation of repository instances:
public interface IRepoFactory
{
IRepo<T> CreateRepo<T>() where T : BaseEntity;
}By studying this project, you will learn:
- How to implement the Repository Pattern in C#
- How to use the Factory Pattern for object creation
- Generic programming with constraints in C#
- Separation of concerns in application architecture
- Dependency injection principles
- CRUD operation implementation
This project can be extended in several ways:
- Database Integration: Replace in-memory storage with Entity Framework or Dapper
- Async Operations: Convert all operations to async/await pattern
- Unit Testing: Add comprehensive unit tests using xUnit or NUnit
- Specification Pattern: Add query filtering capabilities
- Unit of Work Pattern: Implement transaction management
- Logging: Add logging functionality using ILogger
- Validation: Implement entity validation before CRUD operations
Sheikh Ahasunul Islam
- GitHub: @SKahasunul
- Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four)
- Microsoft .NET Documentation
- Clean Architecture principles by Robert C. Martin
β If you found this project helpful, please consider giving it a star!