The repository class
We’ll use a repository class for persisting the quotes customers undergo our utility. In an actual utility, the repository class would work together with a datastore. For our instance, we’ll simply use an in-memory checklist. Since our utility is small, we are able to put the repository class straight into our root listing for now.
Right here’s the repository class:
// QuoteRepository.cs
utilizing QuoteApp.Fashions;
namespace QuoteApp
{
public class QuoteRepository
{
non-public static Listing _quotes = new Listing()
{
new Quote { Id = 1, Textual content = "There is no such thing as a attempt. Do or don't.", Writer = "Yoda" },
new Quote { Id = 2, Textual content = "Try to not be a hit, however relatively to be of worth.", Writer = "Albert Einstein" }
};
public Listing GetAll()
{
return _quotes;
}
public void Add(Quote quote)
{
// Easy ID technology (in actual app, use database ID technology)
quote.Id = _quotes.Any() ? _quotes.Max(q => q.Id) + 1 : 1;
_quotes.Add(quote);
}
}
}
We’ll use a static block to declare and populate a _quotes Listing. Utilizing that knowledge, we offer two strategies: GetAll() and Add(). GetAll() merely returns the Listing, whereas Add inserts the brand new Quote into it. We use a easy increment logic to create an ID for the brand new Quote.
