Good,
a few days ago, Jorge wrote an article gutting the functioning of a Singleton with.NET. Then came Joseph and also did a bit of light on the matter; and I thought to myself,
But they that speak? programming a Singleton by hand, are crazy or what?
We are going to from Edu defined to UNITY that makes the news"because I believe that I have not created an object with"new"for quite some time. For those who do not know UNITY, because in a nutshell is a dependency injector created by the team of Patterns & Practices by Microsoft which is now in its 2.0 version and, to the dark beer, you’re liking when hold you you the taste.
We see a very, very simple example in which we will use a class called "ClaseChorra" to validate instances that are created the same.
The Singleton model is based on the simple example which explains Jorge in his post.
1: class ClaseChorra
2: {
3: private static ClaseChorra claseChorra;
4: public System.Guid Id { get; set; }
5: public ClaseChorra()
6: {
7: Id = System.Guid.NewGuid();
8: }
9: public override string ToString()
10: {
11: return Id.ToString();
12: }
13: public static ClaseChorra GetSingletonInstance()
14: {
15: return claseChorra ?? (claseChorra = new ClaseChorra());
16: }
17: }
Before proceeding > > Disclaimer: is now in Spain Luernes (Monday + Friday) so either I am working much with examples, is what usually happens after a hard week of work.
Does not need to explain that if I think cholón objects, as explained Jorge in his post, because I have very mixed errors.
For example the following code let me a single instance with the same ID for each run of the same:
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: var c1 = ClaseChorra.GetSingletonInstance();
6: var c2 = ClaseChorra.GetSingletonInstance();
7: c2.Id = Guid.NewGuid();
8: Console.WriteLine("c1: {0}", c1);
9: Console.WriteLine("c2: {0}", c2);
10: Console.ReadLine();
11: }
12: }
The result is:
1: c1: 34baaf27-87de-42f7-b2ad-046e35ee65fa
2: c2: 34baaf27-87de-42f7-b2ad-046e35ee65fa
Well well, here the example of George. But how are we would with UNITY?. Ideally, read the article from MSDN "Using Lifetime Managers" where explains different options to work with record types and objects in Unity. I’ll leave a couple of examples.
For example, in the following piece of code, I’m defining a UnityContainer so that it is responsible for creating the objects I need for me (remember the words of the Edu > "UNITY is the news"). Using the container I think instances c1 and c2 and then show the same values.
1: static void Main(string[] args)
2: {
3: var myContainer = new UnityContainer();
4: myContainer.RegisterType<ClaseChorra>();
5: var c1 = myContainer.Resolve<ClaseChorra>();
6: var c2 = myContainer.Resolve<ClaseChorra>();
7: c2.Id = Guid.NewGuid();
8: Console.WriteLine("c1: {0}", c1);
9: Console.WriteLine("c2: {0}", c2);
10: Console.ReadLine();
11: }
The result are 2 different of the same class instances:
1: c1: dea932a8-7ce6-4f44-aa6c-89cb04bed186
2: c2: 011f211a-9a83-463d-81ea-566ef0dbbdab
Now, if I wanted that long to create an object of type ClaseChorra, it is the same instance for all, because simply I tell UnityContainer LifeTimeManager type should be used. There are several options, but for the next example will use a PerThread, which helps us to have a single instance of the object by thread.
1: static void Main(string[] args)
2: {
3: var myContainer = new UnityContainer();
4: myContainer.RegisterType<ClaseChorra>(new PerThreadLifetimeManager());
5: var c1 = myContainer.Resolve<ClaseChorra>();
6: var c2 = myContainer.Resolve<ClaseChorra>();
7: c2.Id = Guid.NewGuid();
8: Console.WriteLine("c1: {0}", c1);
9: Console.WriteLine("c2: {0}", c2);
10: Console.ReadLine();
11: }
As well you will be assuming, now the c1 and c2 classes are the same:
1: c1: 84b8087b-48f5-4519-9362-b5239603ac0a
2: c2: 84b8087b-48f5-4519-9362-b5239603ac0a
So now you know, or you get to encode all the Singleton pattern super sure in your code and leave that "UNITY makes the news por tí" ![]()
Greetings @ Home
The Bruno
Sources:


Leave a comment