activa's blog

.NET, Web, Mobile and more stuff I can't stop talking about

Browsing Posts in Patterns

We all love unit tests (do we?). Yes we do (repeat 100 times). And we all love mocking for making life easier, but sometimes we just want to write some simple unit tests that don’t require any mocking at all, because the code you are testing is easy to test without having to resort to [...]

Jeff Atwood (Coding Horror) wrote an interesting blog post about detecting hyperlinks in blocks of “regular” text. I was suprised by the wave of negative comments he received, mainly because Jeff tried to solve a complex problem with a “simple” regular expression. I don’t agree with these comments at all. Software developers are too obsessed [...]

This is another one in the series “heck, I never thought of that”… Like most of these articles, if you already knew this trick, ignore me… Let’s say you have a generic class with a new() constraint on the type parameter. This means that you are allowed to create new objects of the generic type, [...]

Like many tips & tricks concerning programming languages, what I will present here will be so utterly obvious to some C# developers, but could be an eye-opener to others. How often do you write something like this? if (token == "A") tokenNumber = 1; else if (token == "B") tokenNumber = 4; else if (token [...]

Every decent programming book tells you to write readable code. Few people do. Code is read a lot more often than it is being written. Fact. Programming style is all about personal taste. Agreed. On the other hand, well… let’s just take a very small piece of code from the BCL written by Microsoft: Type [...]

Constructing related .NET objects (an “object graph”) from XML files is a familiar challenge to most developers out there. Although it is not hard to write something like that: just read the tokens and fill the appropriate objects. In most cases, the code produced is not very readable and hard to maintain, because the problem [...]

What’s worse than: Global variables? Badly chosen variable names? ASP.NET WebForms ? Leaky abstractions? … enter your worst coding nightmare … I’ve got one word for you: Dependency Injection (well, that’s two words actually) Why? Remember 20 years ago? All of a sudden, WYSIWYG word processors were all the rage. For a good reason. WHAT [...]

One of the most powerful features in .NET is the ability to use reflection to do all kinds of neat stuff at runtime, like Find out what properties, fields, methods, etc. are in a class Create objects without knowing their type at compile time Call methods by name (even private ones) Check attributes on classes, [...]

The other day I was checking out the Microsoft Web Client Software Factory, but I wasn’t very impressed. Too complicated, too “heavy”, too … Anyway, I did stumble upon something interesting: it includes a class that allows you to define a class member that will survive page requests (in other words, it is stored in [...]

Yesterday, an old article resurfaced on dotnetkicks.com about validating Guid strings. Several comments hinted at the possibility of letting the .NET Framework convert it to a Guid and catching the FormatException. WTF !? I must be very old-fashioned, because I am still very religious about (mis)using exceptions in normal application flow. Exceptions were designed for exceptional [...]