-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
I was reading about the C# 8 language feature that allows you to specify whether a reference type variable expects null values or not, right here:
https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/upgrade-to-nullable-references
I liked the idea of getting compile-time warnings based on whether a type was intended to be nullable or not. I immediately started migrating my projects to C# 8 and enabling nullable reference types, but found a few cases where I got stuck. I would like the documentation to explain the correct pattern to follow in the following cases, please?
-
Using Entity Framework Core you are forced to have a parameterless constructor on the type even if you know it's bad practice. This is to allow EF to construct the type from the database for you. It is acceptable to have just a private constructor but it has to be there so you still see warning CS8618 about non-nullable properties not being instantiated properly. What should you do?
-
Using the IOptions pattern for dependency injection you are forced to have a parameterless constructor on the type even if you know it's bad practice. I guess this is to allow the object to be scaffolded from appsettings.json and injected into your constructor by the runtime. Whatever the reason, it's mandatory. Unlike in Entity Framework, you can't make it private. It has to be public so you still see warning CS8618. What should you do?
-
Using AutoMapper also requires types to have a parameterless constructor.
Hope the feedback is helpful and hope to see some guidance on how to solve these issues.
Thank you!
Benjamin
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 0dfe2629-7f0f-5ab7-6bf3-af84b050c425
- Version Independent ID: 17533d4f-fae1-0883-8838-4ba7c0727bc7
- Content: Design with nullable reference types
- Content Source: docs/csharp/tutorials/upgrade-to-nullable-references.md
- Product: dotnet-csharp
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn