반응형
An Interface is a kind of contract that specifies what the application needs. Let's see how we can use an interface with the repository pattern.
Project Configurations
Creating a Project with MVC Pattern
Implementing MVC Pattern
Creating Repository
Using the Interface
Creating an Interface
Create a folder to hold an interface class
Add an interface in the folder
Add needed methods
Inheritance
Go to the repository and add the interface as a parent, and implement the methods you created in the interface
Adding the Interface as a Service
We need to add the interface in the Program.cs as a service to use it. Add the code below
builder.Services.AddScoped<IItemRepo, ItemRepo>();
Call the Interface in the Controller Class
A repository pattern without an interface looks as below, replace the repository with the interface
private readonly IItemRepo _repo;
public StoreController(IItemRepo repo)
{
this._repo = repo;
}
Call the method
[HttpGet]
public ActionResult<List<Item>> GetItems()
{
return _repo.GetItems();
}
In this writing, we have seen how we can use the MVC repository pattern in .NET with EntityFramework
728x90
반응형
'Backend > .NET' 카테고리의 다른 글
Relational DB - Getting Data including Data from Other Entities (Repository Pattern) (0) | 2023.04.12 |
---|---|
Application Architecture - Generic Repository (0) | 2023.04.09 |
Adding Configurations When Creating a Database (2) | 2023.03.31 |
Application Architecture - Repository (0) | 2023.03.30 |
Application Architecture - MVC (0) | 2023.03.30 |