{{ helloWorld.std.rndJoke() }}
I like to think that this couldn't be any more original - Thursday, October 5, 2017
Whilst I'm here, I'll use this as an opportunity to outline how this site has been put together.
It's a .Net Core 2.0 MVC app using razor to render the views. The site (at time of writing) is hosted in AWS on an EC2 instance, running Ubuntu server, with an Nginx instance acting as a reverse proxy to the app proper.
I've set up my standard Controller > Model > View (Why is it ordered MVC, anyway?) stuff for the home page (and any other, more structured and long lasting projects I might do), but I've opted to go for the new Razor Pages for this blog section. This decision was made mostly to defer my choice of data storage, as I can't be arsed with it, but also to allow me some flexibility over the functionality of each page.
These short sighted and lazy decisions led me to wondering how to implement the index page, for the blog section.
To my mind, there were two options:
- Manually keep an index file up to date, further discouraging me from writing blog posts, and inviting people to call me soooo 2000-late.
- Use some kind of common interface for blog post pages, allowing me to use type reflection to count the entries, and to display a tag line and possibly an image for each post.
namespace Seb.Web.Interfaces { public interface IBlogPage { Uri FeatureImage { get; } DateTimeOffset CreatedOn { get; } string Title { get; } string Caption { get; } } }
and is implemented thusly:
namespace Seb.Web.Pages.Blog { public class HelloWorldModel : PageModel, IBlogPage { public string FeatureImage => "/images/blog/helloworld.jpg"; public DateTime CreatedOn => new DateTime(2017,10,5); public string Title => "{{ helloWorld.std.rndJoke() }}"; public string Caption => "I like to think that this couldn't be any more original"; } }
This allows me to then pull each type which implements the blog post interface, and add it to the page model for the blog, like this:
public class IndexModel : PageModel { public ListBlogPages { get; set; } public IndexModel() { BlogPages = System.Reflection.Assembly .GetExecutingAssembly() .GetTypes() .Where(t => t.GetInterfaces().Contains(typeof(IBlogPage))) .Select(p => (IBlogPage)Activator.CreateInstance(p)) .ToList(); } }
Whilst I normally think this sort of antisocial linq/reflection bastard-child code should be hidden away deep in a helper somewhere, or in a gross dependency injection framework somewhere, it solves my problem handily, for now.
I might go into all of this in more details, once I've done some more work on the site.
Ciao
Seb