How to create immutable objects in C#?

后端 未结 8 475
有刺的猬
有刺的猬 2020-12-23 20:26

In a question about Best practices for C# pattern validation, the highest voted answer says:

I tend to perform all of my validation in the constructor

8条回答
  •  一个人的身影
    2020-12-23 21:17

    How exactly would this work if you want to validate in the constructor of your Entity Framework generated model class?

    It wouldn't work in this context because EF requires the properties of the entity class be public otherwise it can't instantiate it.

    But you're welcome to use immutable objects further in your code.

提交回复
热议问题