I believe the problem is how you’ve defined the Id
property on your Person
class:
public class Person : IPerson
{
public string id => Guid.NewGuid().ToString();
}
This means: every time the code asks for the id
– it gets a newly created Guid – so of course you keep getting new/different Guid’s every time you look at your person!
You need to get the id
once – in the Person
constructor – and then return that:
public class Person : IPerson
{
private string _id;
public void Person()
{
_id = Guid.NewGuid().ToString();
}
public string id => _id;
}
I’m pretty sure once you’ve done this, the singleton will work – you keep getting back the same instance of Person
every time you look at it.
CLICK HERE to find out more related problems solutions.