Can I use nullable types?

I set all fields to types with null values, where the database database fields correspond to null values. Good? Bad?


Long version:

This works fine, I just feel like I can abuse that I can make the material null.

This is basically an internal employee management application that is linked to our SQL database. I use a three-layer structure: from presentation to database: business objects β†’ business logic β†’ data access.

For my Employee object, I have the following fields:

public class Employee : Person { private EmployeeTitle? _employeeTitle = null; //enum private EmployeeType _employeeType; //enum private DateTime _startDate; private DateTime? _endDate = null; private EmployeeInsuranceRecord _employeeInsuranceRecord = new EmployeeInsuranceRecord(); //... } public class EmployeeInsuranceRecord { private int? _employeeInsuranceRecordID = null; private string _alienRegistrationNumber = null; private string _healthInsuranceNumber = null; private string _unemploymentInsuranceNumber = null; private string _welfarePensionNumber = null; private DateTime? _insuranceAcquiredDate = null; //... } 

And in my database:

  • EmployeeTitleID allows null. For example, an employee can start in a trial period without a given job name.

  • EmployeeTypeID does NOT allow null. Even if the employee is a guest or during the trial period, he must be explicitly selected for the purpose of recording.

  • StartDate does NOT allow null. If someone enters the database, we need to know when they started.

  • However, EndDate allows null. Anyone who is currently working does not yet have EndDate.

  • I create an instance of the EmployeeInsuranceRecord object for the Employee instance, but each field inside the EmployeeInsuranceRecord is NULL because the EmployeeInsuranceRecords goes into a separate table associated with the identifier, so an Employee can exist without an EmployeeInsuranceRecord (usually for people just starting out, still in the process of initial work ) By creating an instance of EmployeeInsuranceRecord using get-go, I can save the presentation layer cleaner without instantiating any classes. This does not affect performance, given the small scale we work with.

+4
source share
5 answers

One annoyance of zeros is the coding overhead to keep checking the null value before dereferencing / consuming the value - you probably need to use a bunch of static extension methods to make your life easier.

But it is much better to use null than kluge "magic value", for example DateTime.MinValue, etc. - This not only corrupts the data, but can also cause problems when saving the database if you forget to return it to zero.

As you described, some of the values ​​become mandatory because they depend on the state (for example, EndDate is required when an employee resigns / leaves / leaves the job). This should be divided strategy verification.

+3
source

Creating fields with a value of zero is necessary to represent your business data. What you do looks good to me.

+2
source

You are correctly modeling the database. I would even say that if you were n’t using types with a null value here, you would introduce errors into the code.

+2
source

You will need to use nullable types if the columns in the database table are defined as nullable. Otherwise, you cannot map your model properties to database columns.

Also, there is no difference in performance aspect, and you don’t have to worry about using types with zero or non-empty value.

0
source

You should only allow null if the property or field is optional. Otherwise, you just need to specify a way to specify a value or set a default value (which you can also do for a field, which is optional by the way). If the code you specify reflects a situation where many fields are optional, then this is just fine. Although I was a little surprised why you have a null enumeration ....

0
source

Source: https://habr.com/ru/post/1383582/


All Articles