Creating an immutable value object in C# - Part V - Using a library

-

Other posts:

Tags

12 Comments

Comments

Community Blogs

2008-01-16T18:44:56Z

For some rea­son, there’s been a lot of buzz lately around im­mutabil­ity in C#. If you’re in­ter­ested in

Charlie Calvert's Community Bl

2008-01-21T01:45:40Z

Welcome to the XXXIX is­sue of Community Convergence. The big news this week is that Microsoft has be­gun

Nice work!
One ques­tion about stealing” the base class - what about in­tro­duc­ing an­other layer of ab­strac­tion and in­stead of de­clar­ing your type like this:
pub­lic class DateSpan: Record<DateTime, DateTime, bool> {…}
do some­thing like this:
pub­lic class DateSpan: ValueObject<Record<DateTime, DateTime, bool>> {…}
The ValueObject class would then ex­pose the record to as a pro­tected prop­erty and pass Equals, ToString… to the Record ob­ject. It would make the de­c­la­ra­tions a lit­tle bit more com­pli­cated, but your biggest draw­back is not a prob­lem any­more.

I think I’m miss­ing some­thing. Aren’t you still steal­ing the base class with the ValueObject class?
I.E. you can­not de­clare DateSpan to in­herit from a MySpan class.

Well, I was think­ing about ValueObject more in terms of a type sys­tem base class, where you can put your ad­di­tional be­hav­iour and not spoil the Record class. You are right that still you can­not have a deeper hi­er­ar­chy of types with this so­lu­tion.
BTW: don’t you think it would be nice to have a lan­guage sup­port for im­mutable types? Something like:
pub­lic read­only class MyClass…
where all fields are read­only fields of read­only types?

Yeah, you are right.
We talked about hav­ing first class read­only classes a bunch of times, but we never came up with a pro­posal we are happy with. We are still dis­cussing the topic.

Isn’t code gen­er­a­tion a much bet­ter op­tion at this time, while 1st class read­only classes aren’t avail­able?

It is an op­tion. I would­n’t say it is bet­ter. It has pros and cons (i.e. read­i­bil­ity, amount of code, man­tain­abil­ity).
As for me, I pre­fer li­brary so­lu­tions to code­gen when­ever avail­able (and roughly us­able).

I think you guys in im­mutable space have frankly lost it. It is not new, it is com­mon prin­ci­ples that do not ap­ply in all fields, and just be­cause func­tional at­tempts are again pop­u­lar you are hit­ting on at­tempt­ing to solve a prob­lem with a wrong tool: .net type sys­tem.
If you think mag­i­cally you will some­how par­al­lelise code and al­go­rithms, you are in for a big immutable sur­prise’.
No, sil­ver, bul­let..

All these posts say is: iff you need an im­mutable class, here are a bunch of op­tions of how to do it in C#.
I don’t think I’m claim­ing any­thing more than that. Am I?

Luca Bolognese's WebLog

2008-04-08T17:06:25Z

Previous posts: Part I - Background Tuples are a way for you not to name things. In Object Oriented lan­guages