Skip to content

Add Other Methods To Repository

Shady Ashraf Abdelhameed edited this page Jan 31, 2025 · 1 revision

Repository Setup:

    • Created a repository interface and its implementation (SQLServerRegionRepository).
    • Defined methods (GetByIdAsyncCreateAsyncUpdateAsync, and DeleteAsync) to handle CRUD operations for Region.
  1. Implementation Details:
    • GetByIdAsync: Retrieves a single entity by its ID using FirstOrDefaultAsync.
    • CreateAsync: Adds a new entity using AddAsync and saves changes with SaveChangesAsync.
    • UpdateAsync: Updates an existing entity by first checking its existence, then modifying properties, and saving changes.
    • DeleteAsync: Removes an entity by ID if it exists, using Remove and SaveChangesAsync.
  2. Controller Refactor:
    • Replaced direct database context (DbContext) calls in the controller with calls to the repository methods.
    • Ensured methods like GetByIdAsyncCreateAsyncUpdateAsync, and DeleteAsync in the repository are asynchronous.
    • Properly mapped Data Transfer Objects (DTOs) to domain models before sending them to the repository and vice versa.
  3. Additional Notes:
    • Adopted nullable annotations to account for scenarios where an entity might not be found.
    • Updated naming conventions to reflect asynchronous methods (e.g., appending Async to method names).
    • Utilized LINQ expressions for querying the database.

This approach encapsulates data access logic within the repository, adhering to the Single Responsibility Principle and improving code maintainability and testability.

Clone this wiki locally