Posts

Showing posts from February, 2012

Write your own blog engine, part 4

Adding posts I finally arrive at the "raison d'ĂȘtre" of a blog: adding posts. I want to be able to add posts and have them be returned in the next GET call to the home page. I also want to be able to add posts out of order by explicitly indicating the date/time of the new post (so that it gets sorted somewhere else than the top position). I'll start with an acceptance test for the first part: [TestMethod] public void AddingAPostReturnsItOnTheHomePage() { var guid = Guid.NewGuid(); var POST_DATA = string.Format("Title={0}&Content=This is a test", guid); var cookie = Post("/Account/LogOn", "Username=user&Password=pass"); Post("/Home/AddPost", POST_DATA, cookie); Get(); var articles = root.SelectNodes("/html/body/article").ToList(); Assert.IsTrue(articles.Any()); var topArticle = articles.First(); var header = topArticle.SelectSingleNode(&qu

Write your own blog engine, part 3

Authenticating the user I can't let everyone add posts to my blog; that means having a method to distinguish between a visitor and the owner of the blog. One way to do that would be to check the IP of the incoming connection and verify that it belongs to a list of known IPs; however, that would limit me to a fixed number of computers. What if I'm visiting someone and come up with a new idea for a blog post? No, the correct way of handling this is to add a login screen and verify that the user and password are correct. I am going to use forms authentication so the result of the login process would be a cookie that would re-authenticate the user on subsequent page views. The process will be: user hits an URL that requires authentication user gets redirected to the /Account/LogOn page (and the previous URL gets saved) user enters credentials and clicks the Submit button the credentials are verified; if they are not valid, the user gets redirected to the home page if the cr

Write your own blog engine, part 2

Displaying posts The next feature I'm going to work on is displaying the most recent five posts. The request isn't specific enough but I will assume that I should display them in descending order – most recent first, then the one before that and so on. I will further assume that each post will be contained inside an article tag and contain a date, title and post content. You will be tempted to jump in and create a database and start thinking about the necessary fields. As weird as it sounds, it's far too soon. Yes, a database will be needed – the posts are data and they have to be persisted even when the application is not running – but we don't need it just yet. YAGNI, remember? You ain't gonna need it. YAGNI means a bit more than what it says. It means: don't rush making decisions; you might need this but you might not; wait until the last responsible moment . It is also tempting to put all the logic required – find the posts somehow, sort them in desce

Write your own blog engine, part 1

Introduction I've decided that I need to write another book about TDD programming. The project I've chosen is a blogging engine, per Rob Conery's suggestion - it should be simple enough that I don't get bogged down in details, but complex enough not to be perceived as a toy object. What is the minimal feature set? I've decided to start with these features: Display the blog title Display the most recent five posts - date, title and content Login (especially since it's required by the next features) Add a new post Delete an existing post I will develop this blog engine using ASP.NET MVC 3 and the tests will use the MSTest framework. Start by creating a solution with two projects, an empty MVC 3 application (I've called it MyBlogEngine.MVC) and a test project (MyBlogEngine.Tests). Make sure to add a reference to the blog engine from the test project. I will try to make sure the design of the application stays clean at all times. In particular:

Taking a walk

[Inspired by http://lifeistheteacher.wordpress.com/2012/01/07/my-only-experience-in-a-public-high-school-and-a-call-to-action/ ] Frank was walking around, window-shopping while waiting for the time to come to perform at the near middle school. He was a sixteen-year old guitarist who was visiting a friend and got an offer to perform on a Friday in the friend's class. Frank was homeschooled so classes bored him to tears; he preferred to discover what was interesting in this part of the town instead of listening to a teacher drone about a subject he had absolutely no interest in. Unfortunately, close to the time he was actually preparing to go back to the school, he got stopped by two cops who started asking questions about who he was and what he was doing there. It was clear they believed him to be a truant student but even so, Frank thought, why was it their business? "I'm a musician; I'm performing at the school here in about half an hour." "Right, kid, p