In ASP.NET Core Web API, a repository is a design pattern used to manage data access logic, providing a clean separation between the data access layer and the business logic layer. This abstraction makes it easier to manage data sources (like databases) and unit test your application.
Here are some key advantages and reasons for using the Repository Pattern.
Here’s a comprehensive guide on implementing the repository pattern in an ASP.NET Core Web API.
First, define the entity that represents the data in your application. For example, if you have a Material entity, it might look like this.
namespace GenerateToken.Data
{
public class Material
{
public int Id { get; set; }
public string? ShortText { get; set; }
public string? MaterialGroup { get; set; }
}
}
Create an interface that defines the two methods for data operations.You can also add more methods. This interface abstracts the data access logic.
public interface IMaterialService
{
Task<Material?> GetMaterialDetails(int id);
Task<IEnumerable<Material>> GetMaterials();
}
Create a class that implements the repository interface. This class will contain the actual data access logic, typically using Entity Framework Core.
public class MaterialService : IMaterialService
{
private readonly ApplicationDbContext context;
public MaterialService(ApplicationDbContext context)
{
this.context = context;
}
public async Task<IEnumerable<Material>> GetMaterials()
=> this.context.Materials;
public async Task<Material?> GetMaterialDetails(int id)
=> this.context.Materials.FirstOrDefault(x => x.Id == id);
}
Make sure you have a DbContext that includes a DbSet for your entity.
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Material> Materials { get; set; }
}
In your Program.cs for .NET 8, register the repository interface and implementation with the dependency injection container.
builder.Services.AddScoped<IMaterialService, MaterialService>();
Finally, create a controller that uses the repository to perform operations.
using GenerateToken.BLL.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace GenerateToken.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize (Roles="HR")]
public class MaterialsController : ControllerBase
{
private readonly IMaterialService materialService;
public MaterialsController(IMaterialService materialService)
{
this.materialService = materialService;
}
[HttpGet()]
[Route("GetMaterials")]
public async Task<IActionResult> GetMaterials()
{
var materials = await materialService.GetMaterials();
return Ok(materials);
}
[HttpGet()]
[Route("GetMaterialDetails")]
public async Task<IActionResult> GetMaterialDetails(int id)
{
var materials = await materialService.GetMaterialDetails(id);
return Ok(materials);
}
}
}
You can easily test the repository by mocking the IMaterialService interface, allowing you to isolate tests from the database.
You can also download full code to understand more better way. Please follow the below link.
Download Repository Sample code