It's been a little while coming, but I'm finally launching my new blog. It's built from scratch in ASP.NET MVC. Why go to all the trouble to build a blog engine from scratch when there are a gazillion great engines already out there? That's a very good question, let me break it down for you.
Own Your Content
I'll thank Scott Hanselman for really driving this point home. There's a lot to be said for owning your entire infrastructure from code to database to content. I've blogged on other platforms like WordPress and TypePad, but I generally become dissatisfied when I want to customize things. Want to tweak the CSS? That'll be an extra fee. Want to add your Twitter feed? That'll be an extra add-on to buy or install. How about some ads? Of course we'll serve ads, and we'll keep the revenue too. Etc.
Now this doesn't necessarily mean that you should go out and roll an entire blog engine. In many cases it's enough just to get a domain, install one of the many excellent blog engines that are out there, and use that. At least you'll own the stack and you won't be riding on someone else's infrastructure.
A Learning Exercise
Remember the days when a blog was just a bunch of static HTML pages hosted on GeoCities? Nowadays social sites need to implement a variety of features and protocols to be relevant. Comments (including moderation), RSS feeds, social links, etc. all go in to making a modern blog. I was curious how all of this worked under the hood, so I decided to put my head down and figure it out the best way there is - by doing. Which also means that this blog is built for me. It's not riding on a generic blogging platform and there are probably many ways that I've architected things that wouldn't work for other blogs. That said, I'd love it if some of what I've build could be re-used.
I was originally inspired by Phil Haack to open source my blog. Since then I've seen several other people do this and I love the idea. It lets the blogging community learn from the collective ideas of all the other bloggers out there. See a feature on a blog that you think is cool? Just hop over to the repository and see how it works. To that end, this blog is available on GitHub. Clone me, fork me, go nuts.
So that's why I created this new blog, but what about how it works?
Database, Or Lack Thereof
The most obvious backend capability of any blog is the storage and retrieval of blog articles. Most blogs have an archive page, a summary or recent articles, tags, etc. and while you could conceivably rig all that up manually it would get unwieldy very quickly, which is where a database comes into play. I've been intrigued by the file-based database concept used by some recent blogging/content/static site engines such as Jekyll. The only thing I don't like about many of those tools is the way that the content is separate from the code. I am obsessed with eliminating magic strings and strong-typing everything. The notion that a blog article would like to another blog article using a plain-text
a tag keeps me up at night. What if you wanted to change the link? What if you deleted the post? What I wanted was a way to create my blog database from the file system but still maintain all the power and flexibility that my code gives me.
To satisfy this, I wrote a little library I'm calling RazorDatabase. RazorDatabase takes all of the Razor views that are derived from a specified
WebViewPage class, renders them at compile time, and stores the rendered content and any view properties in an in-memory collection (with caching to disk for quick loading on site startup). I'll write more about RazorDatabase in a future blog post once I've gotten it closer to general availability.
Because the blog posts are just Razor views similar to any other, the easiest way to write them is from within Visual Studio. I could write them externally, but they'd still have to be included in the project and compiled with the site to get the compile-time rendering to work correctly. This suits me just fine - while I've enjoyed Windows Live Writer in the past, I'm also quite comfortable with HTML and don't mind writing up my posts from my IDE.
Other non-blog pages on the site like About are just regular ASP.NET MVC actions and views. Because the blog posts are also just actions and views (albeit ones that get loaded into an in-memory collection), the two can reference each other without problems. I really like the way I don't have to worry about whether I'm on a content page or a blog post - all the Razor syntax and code I'm used to using is available in either place.
Did I mention I hate magic strings? T4MVC is one of my all-time favorite libraries. It provides strongly typed HTML helpers and exposes other bits of code to help eliminate the magic string problem. I highly recommend you check it out if you're not already familiar with it.
This site is automatically deployed on Azure Websites every time I commit to the master branch in the repository. I love the way this works. No publish process, no uploading - just code, push, and be done.
Now that I've got full control of my platform and I've spent all this time creating it, I guess I'm out of excuses for not blogging. Expect to see more frequent posts from me going forward (or at least that's the plan).