2021-12-09

Tech Programing

程式人小天地

Constructor parameters and value object implementation choices

1 min read


I can’t wrap my head around one dilemma.

  1. If i model my ValueObjects as c# records i end up in problems because of with {Property = something} because in that case auto-generated init properties might circumvent some logic enforced in constructor, e.g.

  2. If i model them as regular classes, then i have to check if they are not null in constructor itself.

  3. If i model them as struct (which i lean towards the most atm) then i don’t have a null-check but still, i have a default value check (if its not allowed by an entity).

  4. Last option i can think of is to model them as struct with private isValid property initially set to false which gets set to true in my constructor and gets checked in getters.

Are there any other options I’m missing that would hopefully make null-checks in constructor unnecessary and at the same time be truly immutable?



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *