20.2 C
New York
Monday, March 31, 2025

Construct a server-side net app with .NET, C#, and HTMX



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 will put the repository class instantly 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 Record<Quote> _quotes = new Record<Quote>()
        {
            new Quote { Id = 1, Textual content = "There isn't a attempt.  Do or don't.", Creator = "Yoda" },
            new Quote { Id = 2, Textual content = "Try to not be a hit, however reasonably to be of worth.", Creator = "Albert Einstein" }
        };

        public Record<Quote> GetAll()
        {
            return _quotes;
        }

        public void Add(Quote quote)
        {
            // Easy ID era (in actual app, use database ID era)
            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 Record. Utilizing that knowledge, we offer two strategies: GetAll() and Add(). GetAll() merely returns the Record, whereas Add inserts the brand new Quote into it. We use a easy increment logic to create an ID for the brand new Quote.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles