Advertisement

#7 Seeding the Domain Database

In this article we will focus on adding some seed data to our domain database. We will make it so that whenever our application is started our seed method is invoked so that the data is added automatically for us if needed. We will also create the razor page that we will use to display our article.

The read page and its model

  • WebUi
    • Pages
      • Articles
        • Read.cshtml.cs

We will start by creating a razor page that we will use to read the articles that we are creating. The first step is to create the properties and populate them within the get method of our model (a).

Read.cshtml.cs

using System;
using WebUi.Domain;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebUi.Pages.Articles
{
    public class ReadModel : PageModel
    {
        public string Author { get; set; }
        public DateTime Created { get; set; }
        public int Id { get; set; }
        public string Title { get; set; }

        public void OnGet()
        {
            var article = new Article
            {
                Author  = "Christopher R. Jamell",
                Created = DateTime.Now.ToUniversalTime(),
                Id      = 1,
                Title   = "The Article Title"
            };

            Author  = article.Author;
            Created = article.Created;
            Id      = article.Id;
            Title   = article.Title;
        }
    }
}
(a) The starting properties and how they will be populated in our read page model.
  • WebUi
    • Pages
      • Articles
        • Read.cshtml

Now that we have properties to display it is time to display them. For right now we will just print everything out so that we can make sure that everything is working correctly (b).

Read.cshtml

@page
@model ReadModel

<div>@Model.Author</div>
<div>@Model.Created</div>
<div>@Model.Id</div>
<div>@Model.Title</div>
(b) Printing out the properties of our model to make sure that everything is working correctly.

Preparing to seed our database

  • WebUi
    • Domain
      • IDomainRepository

For testing purposes we need to have entries added to our database and we will not want to have to make them by hand over and over. To facilitate this we are going to add a synchronous save method which starts by updating our repository interface (c).

IDomainRepository.cs

...
namespace WebUi.Domain
{
    public interface IDomainRepository : IDisposable
    {
        ...
        int SaveChanges();
        ...
    }
}
(c) Declaring the synchronous save changes method.
  • WebUi
    • Domain
      • DomainRespository.cs

With the interface being updated we now have to add the method to our repository class (d).

DomainRespository.cs

...
namespace WebUi.Domain
{
    public class DomainRepository : IDomainRepository
    {
        ...
        public int SaveChanges()
        {
            return _domainContext.SaveChanges();
        }
        ...
    }
}
(d) Time to implement the synchronous save changes method.

Seeding our database

  • WebUi
    • Domain
      • DomainSeeder.cs

Now we are ready to add test data to our domain database. We will do this by adding a static class that we will pass an instance of our repository to and it will take care of adding test article(s) to (e).

DomainSeeder.cs

using System;
using System.Linq;

namespace WebUi.Domain
{
    public static class DomainSeeder
    {
        public static void Seed(IDomainRepository repository)
        {
            Articles(repository);
        }

        private static void Articles(IDomainRepository repository)
        {
            if (repository.Articles.Any())
            {
                return;
            }

            var article = new Article
            {
                Author = "Christopher R. Jamell",
                Created = DateTime.Now.ToUniversalTime(),
                Title = "The Article Title"
            };

            repository.Articles.Add(article);
            repository.SaveChanges();
        }
    }
}
(e) The class and method that we can call in order to add test data to our database.
Advertisement

Time to add that seed data

  • WebUi
    • Startup.cs

Now that we have the class that will actually seed our database it is time to call it. We will do this when our application first starts up (f).

Startup.cs

...
namespace WebUi
{
    public class Startup
    {
        ...
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IDomainRepository domainRepository)
        {
            if (env.IsDevelopment())
            {
                ...
                DomainSeeder.Seed(domainRepository);
            }
            else
            {
                ...
            }
            ...
        }
    }
}
(f) Invoking the Seed method within the Startup class will make sure that we have data in our database when our application starts up.

Updating our read page

  • WebUi
    • Pages
      • Articles
        • Read.cshtml.cs

Now that we have data in our database we can update our read page to take advantage of it by injecting the repository into to model and making a database call (g).

Read.cshtml.cs

...
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace WebUi.Pages.Articles
{
    public class ReadModel : PageModel
    {
        private readonly IDomainRepository _repository;

        public ReadModel(IDomainRepository repository)
        {
            _repository = repository;
        }
        ...
        public async Task OnGetAsync()
        {
            var article = await _repository.Articles.FirstAsync();

            Author      = article.Author;
            Created     = article.Created;
            Id          = article.Id;
            Title       = article.Title;
        }
    }
}
(g) Updating our read page model to make a database call for the article instead of hard coding it.

We are not going to keep the article content in the database

  • WebUi
    • Articles
      • Intro1.cshtml

We will split the content of our article into two separate parts. We will have both an introduction as well as a main content part. Both of these will be separate files that we store on disk as opposed to within the database. We start by creating the intro for our article (h). For the intro we will just use the introduction for another article that I have previously written. It is important that we use actual content since we will include this along with information in the database to populate our search index in future videos.

Intro1.cshtml

By the time we are done with this article we will have created the example components for the rest of our
controls. We will also have registered them and provided access to them through an update to our navigation.
The navigation update will also include us make use of the iteration capability of pug to create our anchors
in a much more DRY fashion. We will also create a feature module that will make importing our form components
into an angular module much easier.
(h) The intro page for our test article.
  • WebUi
    • Articles
      • Content1.cshtml

Next up is the main content of the article. For our purposes here the main content will not be indexed so we will just use some lorem text for it (i).

Content1.cshtml

<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Culpa repudiandae ipsam omnis illo sit esse eius magni
cum rerum eum. Pariatur, omnis? Harum rerum nihil ullam aut voluptas ipsa libero?</p>
(i) Adding a little lorem text to use for our main content.

Adding the intro and content to our read page

  • WebUi
    • Articles
      • Read.cshtml.cs

Now that we have an intro and content for our article we need to add them to our read page. As usual we will start by adding a couple of properties to our model which will contain the path to both of the pages (j).

Read.cshtml.cs

...
namespace WebUi.Pages.Articles
{
    public class ReadModel : PageModel
    {
        ...
        public string ContentPath { get; set; }
        ...
        public string IntroPath { get; set; }
        ...
        public async Task OnGetAsync()
        {
            ...
            ContentPath = $"Content{Id}.cshtml";
            IntroPath   = $"Intro{Id}.cshtml";
        }
    }
}
(j) Adding the paths to the intro and content pages to our read page model.
  • WebUi
    • Articles
      • Read.cshtml

With the paths added to our model we can use the partial tag helpers to display the contents of the pages on screen (k).

Read.cshtml

...
<h1>Intro</h1>
<partial name="@Model.IntroPath"/>
<h1>Content</h1>
<partial name="@Model.ContentPath"/>
(k) We can use the partial tag helper to display the intro and content of our article.
Exciton Interactive LLC
Advertisement