unclosed

we can talk about everything

System.Lazy + Singleton

leave a comment »

With the new .NET Framework 4.0 that Microsoft released there is a new class under the System namespace called Lazy. And reading how it works and what’s the purpose to of this new feature you can find some interesting cases to use.

One case that I think that it will make the code safer and cleaner it’s when you need to use Singleton. With this class you won’t need to handle the lazy initialization of your class.
I created a little Singleton class to show how the basic implementation will be using the Lazy feature as you can see below:

public class Singleton
{
   private static readonly Lazy _instance = new Lazy(() => new Singleton());
   public static Singleton Instance { get { return _instance.Value; } }

   private Singleton() { }
}

Pretty simple, huh?

Just a fast explanation about the code,  the _instance variable will hold the class instance created by the Lazy class, with the property Instance you can retrieve the instance when you need and the private constructor is there to not allow someone to create an instance using the default constructor.

An illustration how you’ll be retrieving the instance of that class, in somewhere in you code you’ll have a call like this:

var singleton = Singleton.Instance;

Or

Singleton singleton = Singleton.Instance;

Off course you’ll have the rest of your class, but using a structure like this is much more clean that the conventional Singleton class, I think you already realized that the Lazy class is tread-safe.

You can found more information about the System.Lazy in this link: MSDN

I got disappointed with the wordpress.com today, when you use the service you can’t use plugins, so my illustration code will be just as plain text.

Written by Rafael

July 30, 2010 at 5:20 pm

Posted in Development

Tagged with

First month of ThoughtWorks

leave a comment »

Well today is making one month since I’ve joined ThoughtWorks, I didn’t have a blog so couldn’t talk about this before. So it’s one month away from my sister, family and friends, because the office is a little far from home but I’m liking it and there’s no limit to where I can go…

I’ve never worked in a company with this openness like ThoughtWorks, you can talk about everything with everyone, this is quite nice and all the diversity in the workplace is amazing we can learn a little about other cultures without too much effort. The office we may say that’s a little messy but it is the place that you feel comfortable to work and this is the basic to you go to the work without that bad feeling (maybe just this is not enough but …), there’s post-its in everywhere you look, some guns and some other things to the workers have a bit of fun during the day. All this make everyone be more interactive with each other and the communication will getting better day by day helping us to “be agile”, since we already have the post-its and a good integration between the workers we can join it with the agile practices to do our work everyday.

It’s just the first month and I don’t know what is expecting me in the future but until now I’m enjoying.

Written by Rafael

July 1, 2010 at 1:01 am

Posted in Work

Tagged with

Hi! ;)

leave a comment »

Well today I was thinking about “create or not a blog” and I realized even if nobody come to read what a write here I’ll kind of learn something or have a historic of some activity that I made and thought that will be interesting to share with everyone.

So I created the blog, otherwise you won’t be here losing your time reading this simple post that took me almost a hour to finish.

Just for your I’m not even close to be a good writer, so don’t expect long texts, posts every day or something like that, okay? The purpose of this blog is share my experiences, curiosities and knowledge.

It’s this for now! This have a feed so add it to your reader. =)

Written by Rafael

July 1, 2010 at 12:11 am

Posted in Misc

Follow

Get every new post delivered to your Inbox.