Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This pull request focuses on enhancing code robustness, error handling, and reducing redundancy through the adoption of non-nullable reference types and initialization of properties with default values across various classes and DTOs. #30

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

phamtiendungcw
Copy link
Owner

The changes aim to improve null safety and streamline data handling in entity and DTO classes by initializing properties with default values and making navigation properties non-nullable. This effort reduces the risk of null reference exceptions and simplifies client-side code.

  • Initialization of string properties with string.Empty and collections with new List<T>() in entity classes like Address, Article, Category, etc., to prevent null references.
  • Introduction of nullable annotations in DTOs and modification of DTO validators to support conditional validation based on nullability, enhancing flexibility in partial updates.
  • Changes in GenericRepository to throw a KeyNotFoundException for better error handling.
  • Refactoring of DTOs to inherit from other DTOs, reducing code duplication and improving data structure clarity.
  • Modification in mapping profiles to include conditions that prevent null source members from being mapped, ensuring data integrity during updates.

This commit enhances the robustness of the application by initializing all string properties to `string.Empty`, setting navigation properties to non-nullable states using the null-forgiving operator (`null!`), and initializing collections to new instances across various entities including `Address`, `Article`, `Category`, `Comment`, `Discount`, `Notification`, `Order`, `OrderDetail`, `Payment`, `Product`, `ProductColor`, `ProductImage`, `ProductOption`, `ProductReview`, `ProductTag`, `Shipment`, `ShoppingCart`, `ShoppingCartItem`, `Supplier`, `Supply`, `Tag`, `User`, `WishList`, and `WishListItem`. These changes aim to eliminate `NullReferenceException` errors by ensuring properties and collections are never `null`, thereby simplifying null checks and enhancing code safety. This approach aligns with modern C# practices, leveraging the nullability feature to make the codebase more expressive and safer by making null handling intentions explicit.
This commit represents a significant refactoring effort across the application, focusing on improving the handling of nullable properties, simplifying data transfer objects (DTOs), and enhancing validation logic. Key changes include:

- Initialization of string and collection properties in various DTOs to ensure non-null defaults or to explicitly handle null values, enhancing the robustness of data handling.
- Simplification of DTO structures by removing unnecessary properties, collections, and usings, aiming for cleaner, more maintainable code.
- Introduction of DTO inheritance for a more hierarchical and efficient data model, reducing code duplication and leveraging polymorphism.
- Adjustment of validators to reflect DTO changes, removing specific collection validations, and simplifying validation rules to align with the updated DTO properties.
- Command and profile mapping updates to initialize properties with default values or mark them explicitly as non-nullable, ensuring data integrity and preventing null value errors.
- General cleanup including the removal of unnecessary using directives and the standardization of namespace declarations with curly braces.

These changes collectively enhance the application's data handling capabilities, improve maintainability, and align the codebase with current best practices for nullability and DTO management.
@phamtiendungcw phamtiendungcw merged commit be1c511 into master Jun 21, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant