I have designed a class containing some information about a given object which will be registered in an SQL Server database. I would like to make this object (deeply) immutable, but I also must assure that it gets registered only once. If this object implements the following pattern, can it still be considered immutable?
public class NewClass
{
private bool registered;
public string SomeProperty { get; private set; }
public NewClass Register()
{
if (registered)
{
throw new NotImplementedException(/*arguments*/);
}
/* Register on DB here... */
registered = true;
return new NewClass(somePropertyFromDB);
}
public NewClass(string someProperty)
{
registered = false;
SomePropery = someproperty;
}
}
I would say that except for the boolean field registered
the object is immutable, but this fields leaves me some doubts because it will actually change on the first time the Register
method is executed... Can anyone please tell me how can I solve this problem and still make the object be immutable?