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?
No, it’s definitely not immutable. The
registeredvalue changes over the lifetime of the object.To make it immutable, you mustn’t allow the
registeredfield to change over the lifetime of the object – instead, makeRegisterreturn a new object which has atruevalue forregisteredI’d also remove the private setter for
SomeProperty, just having a getter and a readonly variable.So something like this:
Some notes:
RegisteredFooandUnregisteredFoo; that might make it easier to understand code using this classRegistertwice, so making this immutable doesn’t really help in terms of idempotency. As there’s a natural side-effect (talking to the database) it’s hard to make this truly functional.