0

I'm adding Immutable objects support to my Micro ORM called "Symbiotic"

In the case of a create, I need to pass back a newly created version of the value passed in because the object is immutable and I need to update the newly created identity. For non-immutable values I just set the identity property, but obviously I can't do that now. So I'm thinking of adding a new method called CreateImmutable() with a ref for the value and setting the value with the newly created immutable object with the new identity property value and possibly a changed RowVersion property also. The current method return value is the record changed count, so I want the leave that as is.

I know I could take the easy route and force private property setters, but I would rather support pure immutable types.

Does anyone have any thoughts or potential unforeseen issue on my approach?

current method:

public int Create(object value)

proposed new method 1:

public int CreateImmutable(ref object value)

proposed new method 2:

public int CreateImmutable(object value, out object results)
  • 3
    I prefer that CreateImmutable actually return an object. That is the "conventional" way of dealing with immutables. Modifying a parameter in-place is suggestive of mutability, not immutability, and if your method always returns 1, there's not much value in returning an int. – Robert Harvey Jul 11 at 18:47
  • 1
    If you still want to go this route and preserve your return int, I would prefer using out instead of ref (assuming that I understand correctly what you're trying to do). – Robert Harvey Jul 11 at 18:49
  • thanks for the info. Out requires more dev work but does preserve the original value (not sure it's needed?) ref is simpler yet does not preserve original, but the original variable is also ready for more use with the new Id. I agree with you on the return, I will re-evaluate that. ref: stackoverflow.com/questions/1516876/… – Eric Schneider Jul 11 at 20:20
  • Also the current api call convention is that the passed-in object is modified. If CreateImmutable returns the new object, it's different than the passed-in object, that seems deceptive to me. A ref or Out is more obvious. Thoughts? – Eric Schneider Jul 11 at 20:25
  • 1
    Right. So your "proposed method 2?" Clear as a bell what it does. – Robert Harvey Jul 11 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.