Wednesday, 30 December 2015

Cloning Reddit to learn ASP.NET 5 - Part 1

I've always found it a bit boring to follow a book or video to try to learn a new, or old technology, so I thought I would try a different approach this time.

I would set myself a project, which while perhaps not that much better than a Bicycle store at least it would be more interesting for me.

So I'm doing a clone of Reddit...obviously it won't be a fully functional clone of Reddit but it should have most of the functionality, which will create some interesting challenges.

I don't know how many posts there will be as I am making it up as I go along.

DISCLAIMER

I'm using this as a learning experience, so there is likely a lot that will be wrong, which I will try to amend when I realize that it is wrong so please bear that in mind if you're reading this. It also means that some things might get changed dramatically but I guess that's part of the learning process, right?

Pre-Requisites

  • Visual Studio 2015 (A free edition can be downloaded from this page)
  • ASP.NET 5 RC 1, which can be downloaded from here

We start by creating a new Web Application Project in Visual Studio, which we'll call Reddit:


Ensure that you select Web Application but rather than use the Out of the Box Authentication we'll use no authentication.



We are going to need to use a database to store all the data, controversially, I'm not using a NOSQL DB, although EF7 will support them

Let's add Entity Framework to the project.

We have to edit the project.json file to do this. The added parts in bold. 
"dependencies": {
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },

This will pull the relevant DLLs into the project. It's worth pointing out that nuget will let you choose which version to use, so there is no need to memorize the version numbers :)

At this point we can use database migrations but we don't have any models yet... IF you've used EF 6, Migrations are done differently for EF 7. In essence, the command is (note that it needs to be run from the src folder):
dnx ef <command> <options>
Before adding any models, we need to be able to access the database.  First, let's add a folder called Models and then a simple Repository Interface that I'll call IRedditRepository, along with an implementation RedditRepository and a RedditContext class, which is the actual DB Context.

RedditContext.cs
using Microsoft.Data.Entity;

namespace Reddit.Models
{
    public class RedditContext : DbContext
    {
        public RedditContext()
        {
            Database.EnsureCreated();
        }
    }
}
IRedditRepository.cs

namespace Reddit.Models
{
    public interface IRedditRepository
    {
        UserDetails GetUserDetails();
    }
}
UserDetails is a Data Model and will be defined in the next post, so don't worry too much about these yet.

RedditRepository.cs

using Microsoft.Extensions.Logging;
using System;

namespace Reddit.Models
{
    public class RedditRepository : IRedditRepository
    {
        private readonly RedditContext ctx;
        private readonly ILogger<RedditRepository> logger;

        public RedditRepository(RedditContext ctx, ILogger<RedditRepository> logger)
        {
            this.ctx = ctx;
            this.logger = logger;
        }

        public UserDetails GetUserDetails()
        {
            throw new NotImplementedException();
        }      
    }
}
Other than having a broken project we've not achieved much, yet :(

Stayed tuned for part 2.

No comments:

Post a Comment