Creating a Repository
Let's add a repository folder to apply the repository pattern to access the context method.
Create a two more folders-Contract and Implementation - under the repository folder.
Create the interface ILibraryRepository.cs under the Contract folder.
ILibraryRepository.cs
namespace API_Demo.Repository.Contract
{
public interface ILibraryRepository
{
IEnumerable GetAllAuthor();
}
}
Let us create a class under the Implementation folder to execute the function.
LibraryRepository.cs
namespace API_Demo.Repository.Implementation
{
public class LibraryRepository: ILibraryRepository
{
readonly LibraryContext _libraryContext;
public LibraryRepository(LibraryContext context)
{
_libraryContext = context;
}
public IEnumerable GetAllAuthor()
{
return _libraryContext.Authors.ToList();
}
}
}
The above method will return a complete list of records from the GetAllAuthor() author table.
Let's configure the repository using dependency injection. Startup.CS Open the file, add the following code to the ConfigurationServices method
services.AddScoped, LibraryRepository>();