by Mikael Henriksson
29. April 2009 11:32
First of all I’d like to thank Luca Passani for all his great work with wurfl. He’s done a good job. The database is updated at least once a month and the community I suppose has done a good job with the different API’s. I can however not use the .NET API’s. They are not thread safe and I thought I’d share with you how I improved the old .NET API.
I started of with renaming pretty much everything since the names did not talk to me. We can call the in-memory wurfldatabase the WurflHolder and the class to navigate the holder is called WurflNavigator. The implementation details was changed a bit as well since it did not give me the correct results but that is a different blog post. Why did I make it a Singleton class you ask? Well I tried with a static class first but it caused issues all over the planet. I had people getting detected with a completely wrong mobile phone. Also the logs was totally not right. It basically cause the whole application to go wrong. I though maybe this is a good place for the Singleton pattern and I was right. I am not sure my Singleton implementation is completely thread safe but it is sufficient for now.
Let’s start with the Singleton instance:
public class Singleton<T> where T : class, new()
{
protected Singleton()
{
}
public static T Instance
{
get { return SingletonCreator<T>.CreatorInstance; }
}
private sealed class SingletonCreator<S> where S : class, new()
{
private static readonly S instance = new S();
public static S CreatorInstance
{
get { return instance; }
}
}
}
I think the code speaks for itself. Basically if it’s not created I create it at runtime. The reason for making it generic is that I actually use it for two more classes. Then the API needs a couple of modifications, first in Global.asax we need to load the xml document:
Singleton<WurflHolder>.Instance.Load(HostingEnvironment.MapPath("/wurfl.xml"));
Then the default constructor for the Navigator needs to be changed to something like:
public WurlfNavigator()
{
try
{
_wurflFileNotLoaded = Singleton<WurflHolder>.Instance.WurflFileNotLoaded;
if (!Singleton<WurflHolder>.Instance.IsLoaded)
{
ThrowException(_wurflFileNotLoaded + ". Be sure that the creation of _wurfl have ran OK",
"prepareNavigatorModel");
}
_userAgentAndId = Singleton<WurflHolder>.Instance.UserAgentAndId;
_idAndFallback = Singleton<WurflHolder>.Instance.IdAndFallback;
_idAndCapabilities = Singleton<WurflHolder>.Instance.IdAndCapabilities;
_capabilityNames = Singleton<WurflHolder>.Instance.CapabilityNames;
}
catch (Exception ex)
{
ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log.Error("WurlfNavigator", ex.InnerException);
}
}
This totally stops all randomness in the entire website. maybe not the best thing to keep it in memory but hey, it’s better than total unreliability!
by Mikael Henriksson
12. April 2009 20:31
I am not sure this is liked by many (if any) but I hate having to write too much. Back to being lazy and I love to take shortcuts. One thing I really like doing is to shorten like follows:
public ActionResult Index()
{
return View(new Repository().GetAll<News>());
}
Personally I feel this is almost as readable as the alternative but saves my keyboard a few years in the long run:
public ActionResult Index()
{
var repository = new Repository();
var list = repository.GetAll<News>();
return View(list);
}
106712d9-ff2c-4a83-8fc5-cbbfe75aac41|0|.0
Tags: MVC
MVC
by Mikael Henriksson
7. April 2009 11:28
I can think of various way to hack and slash the HtmlHelpers or jQuery to do this for me but there is a much easier approach though it took me a long time to find! I thought this would be accomplished without the @ before class but I suppose there is a reason for this…
<%= Html.ActionLink("Print",
"PrintInvoice",
new { item.InvoiceID },
new { @class = "document" })%>
4df98858-0504-403d-a44c-50aa79911dd7|0|.0
Tags: MVC
MVC
by Mikael Henriksson
1. April 2009 02:06
I have been (for ages) looking for some type of software that allows me to be “in-sync” with files etc all over the planet and I finally have found it! It’s called Gladinet Cloudand I REALLY love it!! :) It allows me (so far) to connect to my Windows Live SkyDrive, Google Documents, Google Picasa, Amazon Storage and any type of regular network drive I want. The first beta was a bit of huzzl! setting up but beta 2 was a breeze! When these guys go live with this product I am simply buying it because it is worth the money and as I responded to Ayendes post about when some dude tried to get someone else to hack NHibernate Profiler: –“Some software is just too good to NOT buy!!” Guys at Gladius, I am looking forward to your premium version and I hope you make it cheap enough for me to buy but expensive enough for you to make a fortune :)
Good work guys! And again please go to Gladinet Cloudand check it out for your selves!
5bee3a45-67f7-4fbc-869c-973151a44593|0|.0
Tags: Tools
Tools