What are the classes needed for a blog?

When deciding the classes needed for a project, you need to consider the basic entities you are going to have in your project.

For a blog, you need somewhere to store the blog text and, if you want to use categories or tags, somewhere to store each of these.

I decided to have a Blog class and a Category class. Each Blog can have many different Categories and each Category can belong to many different Blogs. So there is a Many-To-Many relationship between the two entities.

If you were coding a Many-To-Many relationship outside of Microsoft's Entity Framework, you would need to create a new class to link the two items together, but this is taken care of for you.

Class for Blog

    public class Blog
    {
        [Required]
        public int BlogId { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime PublishDate { get; set; }
        public BlogStatus Status { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string Blogtext { get; set; }
        public string ImageLocation { get; set; }

        public virtual ICollection Categories { get; set; }
    }

Each Blog has:

  • An Id to link with other classes and to identify the individual blog
  • A publish date to indicate when it should appear on the website
  • A status code (Draft, Hidden, Publish) to give greater control over the publishing
  • The name of the author of the blog (in case I have guest authors)
  • The actual text to appear in the blog
  • A link to an initial image
  • A collection of Categories. this is how to tell Entity Framework to manage a Many-To-Many relationship.

Class for Category

public class Category
    {
        [Required]
        public int CategoryID { get; set; }
        [Required]
        public string Name { get; set; }

        public virtual ICollection Blogs { get; set; }

        public Category()
        {
            Blogs = new List();
        }
    }

Each Category has:

  • An Id
  • The Name of the Category
  • The collection of Blogs