Author: Ricardojb | Posted on: 6/23/2008 1:59:27 PM | Views : 321

I will try to explain, what I want to do is to have a sort of singleton pattern but at the object, not the class level.

Let's say for example I have a Customer class. I definitely want to have multiple Customer instances, but I want to make sure that each customer only will have one instance at any given time.

 Of course, I have to define what makes an object unique.

Let's say that I have a CustomerId property which is an integer (and corresponds to a primary key on a database table).

 The solution I thought was:

1) Make the Customer class's constructor private.

2) create a static variable of type Dictionnary<int, Customer>

like this

private static dictionnary<int, Customer> ExistingCustomers;

this variable could be initialized in an static constructor, and it would be initially an empty dictionnary.

3) create a GetCustomer method taken a customerId as a parameter)
...

Go to the complete details ...