Putting in place the Data Access Layer
Let's create a new folder at the root of the project called
Data Access Layer (DAL). As we only have one domain-specific model, we will need only one context that enables us to have access to the database for this type. Add a class to the DAL
folder called ArticleContext.cs
.
Let's start by adding the following code into the file:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using Chapter3.Models.News; namespace Chapter3.DAL { public class ArticleContext : DbContext { public ArticleContext() : base("DefaultConnection") { } public DbSet<Article> Articles { get; set; } public IEnumerable<Article> GetArticles() { return Articles.OrderByDescending(a => a.PublishedDate); } } }
As you can see, we expose a public Articles
property that Entity Framework will ensure gets set up. Secondly, we expose a public way of getting all articles...