Outline the useful resource
In REST (an acronym for Representational State Switch), a useful resource is outlined as a chunk of information that may be accessed utilizing a novel URI (Uniform Useful resource Identifier). Usually, the useful resource is described utilizing a mannequin object. On this instance, our useful resource will likely be an occasion of the Article class.
Within the venture we simply created, create a brand new file named Article.cs and write the next code in there to create our useful resource sort.
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
inner readonly string Writer;
}
Create a repository for the useful resource
In an ASP.NET Core software a repository is used to supply entry to the information retailer. On this instance, we’ll create an article repository that consists of two sorts — the IArticleRepository interface and the ArticleRepository class that implements this interface.
Create an interface named IArticleRepository in a file having the identical title with a .cs extension and change the default generated code with the next code.
public interface IArticleRepository
{
public Article GetArticle(int id);
public Record GetArticles();
}
Subsequent create the ArticleRepository class. The ArticleRepository class implements the strategies of the IArticleRepository interface as proven within the code snippet given under.
public class ArticleRepository : IArticleRepository
{
public Article GetArticle(int id)
{
throw new NotImplementedException();
}
public Record GetArticles()
{
throw new NotImplementedException();
}
}
Register the repository within the Program.cs file
It’s best to now register the ArticleRepository class as a service utilizing the next line of code within the Program.cs file.