Click here to Skip to main content
15,891,704 members
Articles / Entity Framework

Pimpin' the Blog

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Aug 2009CPOL7 min read 10K   1   3
In this article I will describe to you why I want to change my blog, what I'm trying to achieve, how I plan to do that and whats in it all for you.Why?After writing only a few articles in Blogger and reading up on how to build a usable blog, I knew that at some point I would have to replace Blo

In this article I will describe to you why I want to change my blog, what I'm trying to achieve, how I plan to do that and whats in it all for you.

Why?

After writing only a few articles in Blogger and reading up on how to build a usable blog, I knew that at some point I would have to replace Blogger with something custom build. As time passed and I tried to do more and more things with my blog, I ran into more and more things that I couldn't do as I wanted to do them.

Recently, as some of you may have noticed, I got a comment from Sean Ewington, who is the Chief Technical Editor for The Code Project. He invited me to publish my blog on CodeProject.com (thanks, Sean). To do this I would simply add my blog's RSS feed to their system and add a category to my articles, stating that they should be available on CodeProject as well. Unfortunately that last step is not possible for me, as that would mean I would now have a CodeProject category on my blog, which is not what I want.

This latest thing on the list of things you can't do with Blogger, triggered me to finaly get to work on this and plan some new code.

What?

Some things I want to be able to do after the complete transformation has happened:

  • Customize my RSS feed
  • Easier to customize styling
  • Have more control on advertising (individual posts have specific ads, etc.)
  • Use Silverlight for at least some part of my blog
  • Have a better layout with three columns
  • Have an easier to maintain and easier to use navigation

How?

To get this all done requires some planning. It won't happen all in one go, obviously. First thing I should state is that I had a windows hosting account lying around for a while now and I will be leveraging that account for my blog 2.0. Here are the steps I plan to follow for getting up and running with a new platform.

  1. Get a database up and running to store content
  2. Get the content on the new platform to sync with the content on Blogger
  3. Build a new RSS feed and publish it trough my feedburner account (so if you have a subscription, you won't notice the change).
  4. Design a new layout for my blog
  5. Implement the new layout
  6. Test the two blogs side by side

After that I'm not sure. I might want to redirect all traffic coming to http://jvdveen.blogger.com/ to my new site, but I'm not sure what will happen with search engines and their spiders and I'm not sure if I'm even allowed to do this. However I'm still happy with the Blogger editor and I'm not sure if I want to build a completely new editor so I can then kill my Blogger account. This is something I'll be thinking about later on. If you have any suggestions, please let me know.

Why should you care?

Well, you don't have to care, however... as the title of this post suggests, I will be publishing about the whole process of creating my new blogging platform and then moving in. Not only might you learn something, I will also make available handy code in the process, that you might use yourself. So stick around, learn and benefit.

In this episode of Pimpin' the blog we are going to have a look at how I will sync my data between Blogger.com and my new hosting platform. This involves a not well known feature from .NET in relation to RSS and using Linq2SQL. Eventually we'll end up with a tool that reads by Blogger RSS Feed and stores it in a SQL Server 2005 Database (or any other compatible database for that matter).

Thoughts on synchronization

As I laid out in part #1 I'm far from ready to give up my Blogger account as I still have many things to replace before I can do so. However I don't feel much for keeping two stores for the same information synchronized by hand. I have better things to do with my time than that (altough not that much better :-) ).

The first thing that came to mind was actually RSS as it keeps all the blog aggregate sites up to date as well, so why not use that? Besides, the final trigger to do all this was to customize my RSS feed in the first place, so why not use it as a source?

As a good developer I also went to see if I had any alternatives. I could opt for a HTTP/HTML spider, but it would be awkward, messy and complex. I could try and automate the export process for Blogger blogs, but again, awkward, messy and complex.

Loading a feed

So RSS it is then. The entire process is relatively simple:

  1. Get the feeds xml content
  2. Parse the feed into articles, etc.
  3. Store the articles and related data in the database
The first step is easy. Just take an HttpWebRequest and point it at the feed. Here is the code:
>HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(FeedUrl);
if (UseProxy)
{
request.Proxy = newWebProxy(ProxyUrl);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

So as you can see. it first sets up the HttpWebRequest, so it can get to the RSS Feed (using a proxy if needed). Then it just gets the response stream which then contains the XML for the RSS Feed.

The next step got me thinking. The first solution that popped into my mind was to use Linq2Xml. However that would involve a lot of code to get to all the different parts of information I needed. I googled around, read some blogs, until I ran into someone mentioning the SyndicationFeed object that's new in .NET Framework 3.5. I figured I would give that a try to see how it works and I could always go back to parsing the feed myself.

Here is the code to actually load the response stream into a SyndicationFeed instance:

XmlReader reader = XmlReader.Create(responseStream);

Feed = SyndicationFeed.Load(reader);

Wow, that was easy, now wasn't it? Keep in mind however that you do need to add a reference to both System.ServiceModel and System.ServiceModel.Web to make this work.

What I ended up with is a class that would handle loading the feed into a SyndicationFeed object that handled everything I needed in under fifty lines of code!

So that tackled step two of the process. All that's left is to store it into the database. As I mentioned earlier, I chose to use Linq2Sql to handle this for me. Why? I have had extensive experience with Entity Framework and I do think that for large solutions it can be a good choice, however it does take a lot of effort to make it do what you want, which is not what I needed here.

I read up on Microsofts strategy on data access and why both Entity Framework and Linq2Sql are pushed and found out that Linq2Sql is actually meant to support RAD on smaller projects, or at least for smaller data access layers. As my data model only consists of three tables, I guess my project would qualify as small.

I'm not going to bother you with the details on how I stored my articles trough Linq2Sql and just go ahead and post a link to the code below.

The main program to control this is more interesting:

string feedUrl = ConfigurationManager.AppSettings[FeedUrlConfigKey];
RssFeedReader reader = newRssFeedReader(feedUrl);
reader.UseProxy = UseProxyTrueValue.Equals(
    ConfigurationManager.AppSettings[UseProxyConfigKey], 
StringComparison.OrdinalIgnoreCase);
if (reader.UseProxy)
{
reader.ProxyUrl = ConfigurationManager.AppSettings[ProxyUrlConfigKey];
}
reader.ReadFeed();

foreach (SyndicationItem feedItem in reader.Feed.Items)
{
List<string> categories = newList<string>();
foreach (SyndicationCategory category in feedItem.Categories)
{
categories.Add(category.Name);
}
StorageConnection.AddArticle(feedItem.Title.Text, feedItem.Summary.Text,
    feedItem.PublishDate.Date, 
feedItem.Id, categories.ToArray());
}

First I set up my RssFeedReader instance and call ReadFead on it. This results in a SyndicationFeed on which I iterate trough the Items collection. Then I get the categories and feed them into my StorageConnection class which makes sure everything is properly stored in the database. The StorageConnection class makes sure nothing is duplicated even if the same article is added more then once.

In part #3 of this series, we'll look into building a new RSS feed with some customizations, based on the data we've retrieved today.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KnowledgePlaza
Netherlands Netherlands
Since early 2001 I've been working full time as a software developer and since 2004 I've been working mostly with Microsoft technology.
I started out as a product developer, but after a few years I switched to a project company where my roles ranged from developer up to consultant and from team lead and coach to manager.
Eventually I switched jobs and focused on the consultant part and then I got back to building a product once again. Now I work in a job where I get to do both.

Comments and Discussions

 
QuestionList bloggers 1 Pin
pedro_66625-Mar-10 5:52
professionalpedro_66625-Mar-10 5:52 
AnswerRe: List bloggers 1 Pin
mrjvdveen17-Nov-10 21:28
professionalmrjvdveen17-Nov-10 21:28 
GeneralList bloggers Pin
pedro_66625-Mar-10 5:52
professionalpedro_66625-Mar-10 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.