Category Archives: Uncategorized

Implementing Password History using a Custom Membership Provider

I recently had to add login functionality to one of our intranet sites, which incorporated quite a few constraints. Certain sections had to be restricted to admin users, and other sections had to be accessible without logging in at all. All passwords had to contain at least one number, upper case letter, lower case letter, symbols, and had a minimum and maximum length. The list went on! In the last week or so, I’ve written over 300 tests around this!

Most of the rules were relatively easy to accomplish using a combination of Forms Authentication, the default SQL Membership provider, and the various login-related controls that come out of the box in Web Forms. The rules that couldn’t be configured in the membership section of the web.config could still be accomplished easily enough using the various hooks built in to the login controls (e.g. adding various additional checks using the ChangePassword.OnChangingPassword event, unapproving users if their last login was too long ago, etc.).

The rule that presented the most difficulty, however, was the one that prevented users from using any of their most recent passwords. Having already implemented most of the other functionality using the System.Web.Security.SqlMembershipProvider, I now had a few options: Continue reading

Mathematical Expression Parser

I haven’t been blogging for long now, and it’s been quite a while since my last blog. I’ve been working on the same problem for a while. I decided to move away from the Project Euler exercises in favour of doing an exercise that was put to me in an interview a few years ago. It was quite an interesting problem, and I was only asked to work through one aspect of the problem in the interview. It occurred to me that it would make a good kata and subsequent blog, so here goes…

Define an object graph to represent a mathematical expression.

I’ve elaborated on this original problem to also

create a parser that can take a mathematical expression inputted by a user as a string, and evaluate it.

Continue reading

Project Euler Exercise 4

Ok – so I decided to skip blogging about exercise 3 – you can find it on my github account though. I’m trying to spend about 20 minutes a day doing these code katas, but I only have time to blog about once a week, so I’ll typically stick to blogging about the most recent / interesting one.

So I’m going to focus on my second attempt at exercise 4. I’m happy to say that this was “pure TDD”, in that I wrote the test not having any idea how I was going to implement it! I focussed purely on the design and how I wanted it to look to consumers of these components. I decided I’d try a Fluent Interface style – and I’m pretty happy with the result! So the first test I wrote is as follows:

[Test]
public void CanFindMaximumPalindromeUsingFluentSyntax()
{
	var result = Find.Maximum(number => number.IsPalindrome()).ThatIsProductOf.TwoNumbersEachLessThanOrEqualTo(99);
	Assert.That(result, Is.EqualTo(9009));
}

Continue reading

Project Euler Exercise 2

I need to start spending more time reading requirements!

Project Euler Exercise 2 is stated as follows:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

My first 2 implementations of this completely missed the “even-valued” aspect of this task – I just added up everything in the sequence. Let’s move swiftly on from that! After having re-read the problem, I started again and wrote the following test:

[Test]
public void CanGetTotalOfAllEvenNumbersInFibonacciSequenceUpToAndIncluding4Million()
{
	var fibonacci = new FibonacciSequence(4000001);
	var result = fibonacci.GetValues().Where(i => i % 2 == 0).Sum();
	Assert.That(result, Is.EqualTo(4613732));
}

Continue reading

Project Euler Exercise 1

Ok, so this is the first of what I hope to be a whole series of blogs about my attempts at each of the Project Euler exercises. I’m doing them all in C# – the main thing I’m trying to get from doing these exercises is more TDD practise.

Project Euler Exercise 1 is stated as:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Take 1 – Iterator / Visitor Pattern

I decided to use something approximating a Visitor / Iterator pattern – this was a pattern I saw on JP Boodhoo’s Nothin’ But .NET course recently.

Continue reading

Introduction

Hi!

Hmmm…where do I start? I guess I should explain who I am, what I plan to blog about, and why I’ve chosen to start blogging.

I’m a software developer from Ireland, working in Bracknell, UK, about 30 miles west of London. I’ve been living in the UK for about 9 years now, originally working on engineering applications in VB6 (yeah, I know!) and started working in C#  in 2005. In my spare time, I enjoy jogging, having just recently done my 4th marathon. About 4 years ago, I was lucky to join a very experienced team that’s always had a keen interest on improving process and adopting best practices. We maintain an e-commerce site in ASP.NET and various back-end supporting services. The level of experience around me was quite intimidating at first – there’s nothing like feeling like the stupidest person on the team to spur you on to better things!

Over the years, the more I learnt, the more that feeling went away, and though I continued to learn and enjoy learning, to some degree I became a little complacent. Then recently, I went on JP Boodhoo’s Nothin’ But .NET course – it was a real eye-opener. It was incredibly frustrating to think about just how much more progress I could have made over the years had I been a bit more focused about my learning, setting more explicit targets, and made an effort to figure out how best to learn – I’m not sure how many books I’ve read that have ultimately made very little impact on my various programming skills. Frustrating as it was though, this course was a very positive experience – I, like everyone else on the course, was completely humbled by JP’s expertise and attitude, and really inspired to make quite a lot of changes to help me learn more quickly and effectively.

Off the back of that course, I’ve just read Andy Hunt’s Pragmatic Thinking and Learning. One of the many suggestions from this book was to start my own blog – so here we are.

I don’t think I had the confidence in years gone by to start a blog. Blogging seems like something that requires a lot of honesty about your own abilities (or lack of!), and the potential “feedback” I might get about my thoughts, ideas, grasp of various technologies, is something that I’m trying not to think about too much! All I can say is that I don’t claim to be an expert or anything near it; that this blog is, on one level, a very selfish endeavour to help me clarify my own thoughts on any given subject – but if I can help you understand something better as a result, all the better!

So, my plan to start with, to get into the swing of this blogging lark, is to work through the Project Euler code katas. My focus in these katas is to approach them from a Test Driven Design point of view, using NUnit and Rhino Mocks  – while I’ve been writing unit tests coupled with Rhino Mocks on and off for a few years, I think I’ve only really taken unit testing seriously for approximately 6 months now. TDD, however, is something that I’ve known about for a long time but have never really been hugely comfortable with. Having already done the first Project Euler exercise, I know that even with the best intentions, my TDD approach seems to deteriorate / revert quite quickly into old habits, with tests ultimately being rewritten when forced to change signatures of systems under test. I expect these things just require practice – more on that in the posts to follow.

Regardless, I’ll be posting the finished solutions in GitHub here – see the CodeKatas solution. I expect I’ll be coming back to do some katas again – while I’m reasonably happy with the end result of my attempt at exercise 1, I want to do them again in pure TDD-style.

My blog post about my approach to Project Euler Exercise 1 should follow this post fairly shortly. After blogging about Project Euler, I hope to blog a bit about NUnit, Rhino Mocks, JQuery, Ajax, MVC. We’ll see what shiny new thing comes along after that.

Looking forward to hearing your comments!