Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 3
page 5 of 8
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 14540/ 189

Fetch Data

As I stated in the previous sections of this article, usually developers prefer to use a proxy class to hide the inner data workings that lead to loading data so I create a class and name it KBlogDataContext to define a few methods that will load the data objects for me and I can apply them in my controller action methods easily.

Methods included in this class use my LINQ to SQL objects and LINQ statements to load data. The code for this class is shown in Listing 1.

Listing 1

using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
 
namespace KBlog.Models
{
    public class KBlogDataContext
    {
        public List<Post> GetCategoryPosts(string categoryName)
        {
            KBlogDBDataContext context = new KBlogDBDataContext();
            return context.Posts.Where(
                p => p.Category.Title == categoryName).ToList();
        }
 
        public List<Post> GetRecentPosts(int count)
        {
            KBlogDBDataContext context = new KBlogDBDataContext();
            return context.Posts.Take(
                count).OrderByDescending(p => p.PublishedDate).ToList();
        }
 
        public Post GetPost(int id)
        {
            KBlogDBDataContext context = new KBlogDBDataContext();
            return context.Posts.Where(p => p.ID == id).Single();
        }
 
        public List<Category> GetCategories()
        {
            KBlogDBDataContext context = new KBlogDBDataContext();
            return context.Categories.ToList();
        }
    }
}

Note that there are some simple LINQ expressions that I used for this example, but I don't step in their details because they're easy to read and understand and move us beyond the scope of this article series.

There are four methods defined here:

·         GetCategoryPosts: Returns a list of posts for a specified category based on the category name.

·         GetRecentPosts: Returns recent N posts of the blog to be shown in the homepage.

·         GetPost: Returns a single post based on its identifier.

·         GetCategories: Returns a list of all categories.

Even though I could get rid of this class I strongly recommend you follow the same approach in your applications to have good encapsulation for your data operations and have simpler and more readable code in controllers.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 3 and type the answer here:

User Comments

Title: Busby SEO Test   
Name: info@gmail.com
Date: 12/22/2008 12:07:27 AM
Comment:
thank you very much
Title: Question   
Name: Michael
Date: 12/6/2008 6:31:47 AM
Comment:
Hi, how about if our sample url is '/2008/12/06/post-title-here' just like what Wordpress did, is it possible?
Title: Nevermind   
Name: Nick Kirkes
Date: 4/26/2008 7:07:31 PM
Comment:
Just realized I am missing the DBDataContext.
Title: context.Posts?   
Name: Nick Kirkes
Date: 4/26/2008 7:06:44 PM
Comment:
Thanks for the article.

I'm following your guidance, but I can't build as the "context.Posts" don't exist (same for categories). Am I missing something?
Title: anxiously awaiting...   
Name: Emiel
Date: 4/24/2008 3:06:02 AM
Comment:
Here's another one waiting for part 4...
When, when, WHEN?
Title: And the part 4?   
Name: lxx
Date: 4/17/2008 11:42:43 AM
Comment:
I wait for your next artical.Thanks.
Title: Reply to Mike   
Name: Keyvan Nayyeri
Date: 3/28/2008 8:53:51 AM
Comment:
Mike,

I'll send it to editorial team in the next three days so I expect it to be published in the next couple of weeks.
Title: Expectation?   
Name: Mike Mayers
Date: 3/28/2008 8:45:08 AM
Comment:
Is there any prevision about the release of the 4th part? Can't wait to see it. Very nice.
Title: Very good!!!!!   
Name: Marcelo
Date: 3/27/2008 4:35:06 PM
Comment:
Very good tutorial! Am waiting anxiously for the "View" part!!! Thank you.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/8/2009 7:35:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search