-
Notifications
You must be signed in to change notification settings - Fork 0
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 (
GetByIdAsync,CreateAsync,UpdateAsync, andDeleteAsync) to handle CRUD operations forRegion.
- Created a repository interface and its implementation (
-
Implementation Details:
-
GetByIdAsync: Retrieves a single entity by its ID using
FirstOrDefaultAsync. -
CreateAsync: Adds a new entity using
AddAsyncand saves changes withSaveChangesAsync. - 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
RemoveandSaveChangesAsync.
-
GetByIdAsync: Retrieves a single entity by its ID using
-
Controller Refactor:
- Replaced direct database context (
DbContext) calls in the controller with calls to the repository methods. - Ensured methods like
GetByIdAsync,CreateAsync,UpdateAsync, andDeleteAsyncin the repository are asynchronous. - Properly mapped Data Transfer Objects (DTOs) to domain models before sending them to the repository and vice versa.
- Replaced direct database context (
-
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
Asyncto 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.