Often in ASP.NET application we see a code which looks like this one:
1 2 3 4 5
| if (Cache["SomeData"] != null)
{
string name = ((SomeClass) Cache["SomeData"]).Name;
//.....
} |
Experienced developer, even if he is not a paranoiac, will find possible problem immediately — NullReferenceException. That’s because of caching implementation in ASP.NET. In ideal case an object, that has been cached, will stay there up to application restart, but in real world it could be deleted between two calls: by the garbage collector when memory is over (because cache uses weak references WeakReference); by another thread to refresh cached data.
So the code I have mentioned before works in 99% of all cases, but sometimes you will get errors in your log, which can not be reproduced easily. Here is right cache usage approach:
1 2 3 4 5 6
| SomeClass someClass = Cache["SomeData"] as SomeClass;
if (someClass != null)
{
string name = someClass.Name;
//.....
} |
Do not relax your vigilance, it’s exactly what they are waiting for! (about bugs)
How often you are laughing out loud when looking on the someone’s code? Today I found great code in my current project and I can’t hold posting this to my blog. So,
1 2 3 4 5 6 7 8 9 10 11
| if (Request.QueryString.HasKeys())
{
string[] keys = Request.QueryString.AllKeys;
foreach (string k in keys)
{
if (k == "memberpagemode" && (string)Request.QueryString.GetValues(k).GetValue(0) == "edit")
{
pSett.ChangeFormViewMode(FormViewMode.Edit);
}
}
} |
And how do you search through the hash for a key with specified value?
In my current project we decided to build a Facebook application. This is really great platform with many interesting ideas inside, which usually means that you will spend a much time to make your application working as expected. Today I wanna talk about user profiles. Any Facebook application could add some action links, which will be displayed under the user’s picture, and some content for wide or narrow column. Of course, you can use FBML syntax, especially fb:if-... tags set to choose which content to show on specific profiles to concrete users.
Read the rest of entry »
Sometimes we need to do some tasks, that libraries developers even have not foreseen. One of such cases is to make synchronous AJAX call (Asynchronous JavaScript And XML). Below you could find quick solution.
Read the rest of entry »
Not so long ago I faced a problem of displaying count of some entities in singular or plural form (“No comments” or “less than a minute ago”, “1 comment” or “a minute ago”, “2 comments” or “2 minutes ago”). It’s very easy for English (there are three variants only), but I’m working on application that should be localized for several cultures. For example, in Russian we have at least 4 forms (“Нет комментариев”, “1 комментарий”, “2 комментария”, “5 комментариев”) and not so obvious rules for plural forms (“11 комментариев”, “111 комментариев”, but “21 комментарий”). I don’t speak any other language, but I think that several of them might be even more complicated than Russian. Here you can find my thoughts about such strings localization.
Read the rest of entry »