1) Code code must do the conversion
- For example, an input line (from txtDateOfBirth) to DateTime or DateTime?
2) Business logic must verify business rules
- For example, the user must be at least 16 years old
3) The method should be
InsertUser(User user) {} orInsertUser(string firstName, string lastName, DateTime or DateTime? dateOfBirth) {}
You can see that the .Net Framework uses mostly strong type parameters instead of a string and an object.
If you do not want to pass values with a null value, you can use the following approach, which is used in DotNetNuke .
public class Null { public static int NullInteger { get { return -1; } } public static decimal NullDecimal { get { return decimal.MinValue; } } public static DateTime NullDate { get { return DateTime.MinValue; } } ... }
Edited . At your request, I added code by code. This is not really a template; it's just a person’s preference.
public string FirstName { get { return FirstNameTextBox.Text; } } public string LastName { get { return LastNameTextBox.Text; } } public DateTime DateOfBirth { get { DateTime d; return DateTime.TryParse(DateOfBirthTextBox.Text, out d) ? d : Null.NullDate; } } protected void SaveButton_Click(object sender, EventArgs e) { try { var user = new Users() { FirstName = this.FirstName, LastName = this.LastName, DateOfBirth = this.DateOfBirth }; UserService.InsertUser(user); ... } Catch (Exception ex) {
source share