ASP.NET Localization using the Strategy Pattern

Ever wondered how to do pluggable and testable language selection using ASP.NET? Since I’m in a pattern-happy mood I thought I’d implement this using the Strategy design pattern.

My goal with this was to retrieve the culture’s name in which the user would like to see the webpage. This can come from two sources (in my case): the Accept-Language HTTP header and a custom cookie I send to the user when he/she clicks on my  little flags. So the first thing is to define an interface for our strategy:

Let’s see what do we have here. This interface has two methods and a property. I think GetCultureName is obvious, this is why we do this in the first place, it’ll return the culture’s name as a string. IsApplicable is slightly more interesting from a reusability perspective. You see we’ll have to find out if our algorithm is usable at runtime (e.g. the Accept-Language header exists and it isn’t empty, or we have our cookie in the request).  This method will tell us if we could use the current implementation. I’ll give more details right away. Cardinality tells us the order of the algorithms. We can set only one culture to use, and it’s possible that there are more algorithms that can be used on a request, and we need to determine an order in which we’d use them. So if there are more strategies that are applicable, we select the one with the highest cardinality.

So now we need to define the implementations. I’ll provide three: one for cookies, one for the Accept-Language header and a fallback, mostly static strategy if neither of the previous ones are usable. So here they come:

Above is the cookie implementation. As you can see it depends on the ICookieStorage interface, which is just a wrapper around HttpContext’s cookie operations, to help unit testing. I’ll left it as an exercise for you 🙂

Our next implementation is for the Accept-Language header, again there’s a dependency which just uses HttpContext, and again for testing reasons. The concrete algorithm that selects the first language from the list can be improved, but that’s enough for my purposes.

And as I promised, our static fallback strategy. It doesn’t require much explanation, it just covers the case when no other strategy is usable. Of course I could just throw an exception and leave the culture as the default one, but it’s much cleaner this way (as I believe).

OK, the strategies are ready, but they won’t do anything useful without someone to employ them. In a moment you’ll understand why IsApplicable was needed so badly. Here’s the class that does the coordination:

Now a little fact about strategy: it lets you select an implementation of an algorithm at runtime. It ain’t  gonna select itself. A while ago I used to write Factories for this purpose. You could imagine how nice a LocalizationStrategyFactory looks like and what it does. But I read an article somewhere (sorry, I’ve forgotten it and can’t give credit where it belongs), which said that strategies can tell themselves if they’re any good in a case. And that’s exactly what the IsApplicable method does. Basically my DI container (Castle Windsor) resolves all implementations of the ILocalizationStrategy to an array, then with the simplest of LINQ commands I select the first algorithm with the highest cardinality to do the job. And that’s all, simple, clean and last but not least, testable.

Last thing to do is to set the whole thing up in global_asax. Just hook into Application_BeginRequest, and pass the resolved CultureInfo to CultureInfo.DefaultThreadCurrentUICulture (or Thread.CurrentThread.CurrentUICulture, if you are below .NET 4.5).

Advertisements

Browser navigation with AJAX pages

Recently I came to this issue, and after a lot of googling around I found a simple and powerful solution so I thought it would be beneficial for everyone to know about it. It’s called jQuery Address, and you can find it on the page of Asual. Let’s download it now.

My main problem was that I’m hijaxing almost all of my links in my ASP.NET MVC application, and doing this prevented me from using the browser’s back and forth buttons. I could easily live with this, but my designer couldn’t so I had to come up with a solution. I must warn everyone that it was just pushed together in a hassle, and you could almost see the duct tape holding it together, but it works (for now). Continue reading “Browser navigation with AJAX pages”

70-523 Passed

Hello everybody, it’s been a while since I’ve last posted anything here. As usual, I have multiple reasons – the most important is the lack of time. My company has moved into its first office and it took quite a lot of time to set up everything running smooth, and we’ve a lot of work to do (which is certainly a good thing). Also, I’m currently visiting two universities which take my weekends. But don’t worry, I hadn’t given up my plans on certifications, there will be posts.

As the title says, I have passed the 70-523 exam, which transitions my ASP.NET 3.5 MCPD to Web Developer 4 MCPD. It means a nice WCF cert too (I have passed the exams 70-515 and 70-516, so I don’t receive MCTSs for those).

OK, let’s talk about the exam itself; I know that everyone clicked here is interested on that. So it had four parts for the four respective exams it includes: one for WCF, ASP.NET 4.0, ADO.NET 4.0 and the Web Developer MCPD exam. The time was more than enough, and the questions were moderately hard – but because I passed the ASP.NET and the ADO.NET part before, I knew what to expect on those parts. Read my posts on the 70-561 and 70-562 exams, see what’s new in .NET 4.0 (particularly the Entity Framework) and you are half way on passing this one.

The WCF part was tricky, I have little experience on that subject, but after reading the questions a few times and eliminating the two obviously bad answers, you could catch the logic behind the concept.

The last part was the MCPD exam which I expected to be a little bit harder. There were questions I didn’t know of course, but I could find out the answer for most of them with little more than two years of ASP.NET experience. Of course, you had to go deeper than just ASP.NET – you should know the .NET Framework in general, and a great deal of SQL Server. There were questions about Silverlight, security, application deployment. From the syllabus, I expected some architectural questions, too, but there wasn’t that many (although you better know some architectural patterns, too but they weren’t referenced by their names).

Anyway, this exam wasn’t that bad, but you should have a wider range of knowledge than it was required for its predecessor. I really don’t know what the next one will be – maybe the MCTS on Windows development, but nowadays I did a great deal of Windows 7 client configuration, so it is possible to have a try on 70-680, too. Whichever one I chose, you’ll have your deal of preparation posts for them.

70-515 Passed

I couldn’t take the pressure of not being certified in .NET 4.0, and with the upgrade exams delayed until December, I felt an urgent need to take the 70-515 ASP.NET 4.0 exam, which I passed successfully today.

The exam changed a bit (it was much more fun) since the previous version, and it was a real pleasure. I didn’t really use any learning sources, because I work with everything covered in the objectives on a daily basis.

Anyway, if you have passed the 70-562, then study a little MVC, and you are ready to go.

70-564 Passed

Today I rescheduled my exam (the Prometric staff was by all means flexible and helpful), so thirty minutes after my phone call in the morning I could begin it. As I knew what to expect (and the revision done in the previous two days) it wasn’t a big challenge to pass. This one also completes my MCPD in ASP.NET 3.5, which feels very good.

But there are many exams ahead for me, the next one will be ADO.NET, since I found out that I did a crappy job in the working with data sections of both ASP.NET exams. Now I dig myself deeper into the topic. There will be a brief pause in my posts, because the end of the semester is coming, and I’ll need to study some sociology too, which will be painful and irritating after all these .NET studies.

So next exam to clear out is the 70-561: ADO.NET Application Development. The Training Kit is just about to arrive (I was always optimistic). Oh, and if you’re interested in the exam itself, consider reading my fail post.

Establish an error-handling strategy

Dealing with errors is an important part of every application. Web applications aren’t an exception, in the contrary, they should be even better in graceful error handling, because of their huge amount of users.

We have three possible locations of caching errors in an ASP.NET application. The first one is the source of the error itself, by wrapping our code, which is likely to fail, in a try-catch block. This is a very good solution for the problem, since the error gets treated on the spot of its occurrence. You can decide to bubble the error forth, and catch it in the Page_Error event, or even further, in the Application_Error event, defined in global.asax. There’s a fourth opportunity, but it isn’t the best solution: define custom error pages in the customErrors section of your web.config. They are part of the apologize, not the solution for the problem, so you should restrict the use of them. Even worse, when you are on a custom error page, you have no way to get and deal with the error lead you there.

Before writing error-handling code, make sure that you do everything to prevent errors from happening. The best way to do so is to use a massive amount of validation. String.Empty and the Exists method should be very close friends of you.

But if the worse happened, you should fail elegantly, and without showing any inner workings of your application. Hold the detailed error messages to server administrators, and give user-friendly and meaningless errors to the everyday users. Also, you should log the exception (Health monitoring comes handy for this task).

Continue reading “Establish an error-handling strategy”

Write HttpModules and HttpHandlers

There are two options of extending the page-processing behavior of ASP.NET. These are HttpHandlers and HttpModules. I’ve already written a post about the former, which you can find here. HttpHandlers are parts of the ASP.NET infrastructure which are  responsible for processing a given request. Each request is served by exactly one handler. You can create your custom handlers by implementing the IHttpHandler or IHttpAsyncHandler interface.

Modules on the other hand provide a way to work with every given request. There are multiple modules working in ASP.NET for each request, for example, they are responsible for authentication, sessions, caching, and much more. Even better, they don’t override the default execution of the page life-cycle, so your forms will show up as usual. With the help of the IHttpModule interface, you can register events of the page life-cycle such as BeginRequest, or PostLogRequest. You can then add your own code into these events.

But what is the appropriate use of them?

Continue reading “Write HttpModules and HttpHandlers”