Jeremy Davis
Jeremy Davis
Sitecore, C# and web development
Page printed from: https://blog.jermdavis.dev/tags/c/4

Posts tagged C# (Page 4)

Listen to what your tests are telling you...

One of the things that the masters of Unit Testing say is that tests should always be repeatable and deterministic. No matter when, or how many times you run a test, if the System Under Test stays the same, the results of the test should stay the same. So I found myself a bit confused recently when some tests went from reliable to unreliable results. Turns out the tests were right, and they were pointing out a silly bug I'd introduced by accident...

C# ~2 min. read

Edge cases of caching

Most of the time when you need to cache things in Sitecore, it's handled for you with the built in frameworks for data and UI layer caches. Usually your problems are solved by tuning the sizes of the caches and configuring the right caching settings on your UI components. But what happens when you've got bits of data which you want to cache that are neither components or items?

When is a reference to null not a null reference?

I was having a conversation with a programmer new to C# recently, who was rather confused by something similar to the following bit of code. He'd been looking at the source for a library on GitHub, and he didn't understand why this could run without errors:

To him, it seemed to be risking a NullReferenceException, as you can't call a method on a null object.

C# ~2 min. read

My favourite confusing exception

Spending a lot of time developing websites with Sitecore means I've reported my fair share of bugs to support. One particular issue that I came across in 6.6 a while back stands out as my "favourite" issue I've reported – just because the error stops a lot of developers in their tracks thinking "how on earth can this happen?". If you wrote this code, would you think it could crash?

Well it can crash. And to a less experienced developer it can be a very confusing error:

Programmatic WFfM submissions

Recently a client of mine came up with some requirements that involved submitting data to the Web Forms for Marketers database via code. Having done a bit of Googling, I came across a Stack Overflow answer on the same subject which seemed to offer a solution. Implementing this code did indeed allow submitting data – however it didn't trigger Save Actions. So while the data will go to the configured WFfM data store, emails or CRM integrations won't get triggered.

That was an issue for my client, who wanted to have email notifications as well. So starting from the Stack Overflow responses, I looked into how the WFfM save actions can be correctly triggered via code.

Patterns for navigation controls - Language versions

I was going to stop at three posts on simple patterns for navigation (Parts one,two and three are here) – but earlier this week I realised I'd not covered an important topic in navigation for Sitecore-based websites: How you can navigate between different language versions of your sites and pages. So this week, I'll start looking at language navigation.

There are two common patterns for how you might structure your content in Sitecore if you have a multi-lingual site. Depending on what sort of content you're dealing with, you might choose to:

(Technically, there's also a third option, where you have multiple content trees which may also have multiple languages – but that's just a combination of the two ideas I'll present)

The core navigation controls for these sites work in exactly the same way as the ideas I've presented in previous posts. But if you have multiple languages, then you need some code to display links to let the users swap between languages. And you need slightly different code for these two scenarios. So this week I'll look at the first of these:

Patterns for navigation controls - Varied styles

Continuing the theme of simple approaches to navigation components, this week I want to look at a slightly more complex scenario for the top level navigation in a site. Sometimes you need the mark-up for primary navigation to vary between different items in your navigation. Perhaps you have some pages which need a different style of display because of their purpose on the site. And generally in this sort of scenario you need editors to have some control over which items get these different views.

Patterns for navigation controls - Breadcrumbs

Last week I started looking at some simple ideas for the top level navigation for Sitecore websites. This week, I'll continue that theme with some simple examples of the more localised navigation that you might use on content pages.

Patterns for navigation controls - Basics

Most websites need some sort of navigation UI. If you have a site with more than a handful of pages then you're likely to need to create some components for rendering sets of links that show the structure of your site. So continuing my theme of simple patterns for Sitecore code, here is the first of a few posts on some approaches to navigation.

Another nice feature in C#6.0

Ok, so it's technically Christmas and I should really be eating too many mince pies and watching repeats of early 80s sitcoms instead of blogging. But somehow I can't stop technology catching my eye.