Skip to content

SKahasun/Repository_Factory_Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Repository Factory Pattern in C#

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.

πŸ“‹ Table of Contents

🎯 Overview

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.

πŸ—οΈ Design Patterns Used

Repository Pattern

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

Factory Pattern

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

πŸ“ Project Structure

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

✨ Features

  • 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

πŸš€ Getting Started

Prerequisites

  • .NET Framework 4.x or higher (or .NET Core/5+)
  • Visual Studio 2019 or later (or any C# IDE)

Installation

  1. Clone the repository:
git clone https://github.com/SKahasun/Repository_Factory_Pattern
  1. Open the solution in Visual Studio:
cd Repository_Factory_Pattern
  1. Build the solution:
dotnet build
  1. Run the application:
dotnet run

πŸ’‘ Usage Examples

Creating a Repository

// 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>();

CRUD Operations

// 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);

πŸ›οΈ Code Architecture

Base Entity

All entities inherit from BaseEntity, which provides a common Id property:

public abstract class BaseEntity
{
    public int Id { get; set; }
}

Generic Repository Interface

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);
}

Repository Factory

The factory encapsulates the creation of repository instances:

public interface IRepoFactory
{
    IRepo<T> CreateRepo<T>() where T : BaseEntity;
}

πŸŽ“ Learning Outcomes

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

πŸ”„ Extending the Project

This project can be extended in several ways:

  1. Database Integration: Replace in-memory storage with Entity Framework or Dapper
  2. Async Operations: Convert all operations to async/await pattern
  3. Unit Testing: Add comprehensive unit tests using xUnit or NUnit
  4. Specification Pattern: Add query filtering capabilities
  5. Unit of Work Pattern: Implement transaction management
  6. Logging: Add logging functionality using ILogger
  7. Validation: Implement entity validation before CRUD operations

πŸ‘€ Author

Sheikh Ahasunul Islam

πŸ™ Acknowledgments

  • 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!

About

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.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages