Exercise: Auto-Generated Code Simulation
Problem statement
In enterprise environments, database scaffolding tools often auto-generate data models. If a developer edits these generated files, the scaffolding tool overwrites their custom logic during the next run. You have an auto-generated Invoice model. You need to inject a custom business rule to flag invoices with a negative total without modifying the auto-generated file.
Task requirements
Provide the signature for an optional partial method named
Validatein the generated code.Invoke
Validate()from inside the existingProcess()method.Implement the
Validatemethod in the custom developer file to check ifTotal < 0.Print a warning if the total is negative, otherwise print a validation success message.
Constraints
You must use the
partialkeyword on theInvoiceclass in both locations.You must use the
partialkeyword on theValidatemethod.You must use a file-scoped namespace named
Business.Modelsin both class files.You must organize the solution across three distinct files: the execution script, the generated model, and the custom developer logic.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
For partial classes to merge, they must share the exact same namespace. Using a file-scoped namespace like
namespace Business.Models;in both files is the cleanest way to do this.The signature in the generated file should look exactly like this:
partial void Validate();Partial methods are optional; if you don't implement the body in the custom file, the compiler will simply remove the call from the generated code.
Exercise: Auto-Generated Code Simulation
Problem statement
In enterprise environments, database scaffolding tools often auto-generate data models. If a developer edits these generated files, the scaffolding tool overwrites their custom logic during the next run. You have an auto-generated Invoice model. You need to inject a custom business rule to flag invoices with a negative total without modifying the auto-generated file.
Task requirements
Provide the signature for an optional partial method named
Validatein the generated code.Invoke
Validate()from inside the existingProcess()method.Implement the
Validatemethod in the custom developer file to check ifTotal < 0.Print a warning if the total is negative, otherwise print a validation success message.
Constraints
You must use the
partialkeyword on theInvoiceclass in both locations.You must use the
partialkeyword on theValidatemethod.You must use a file-scoped namespace named
Business.Modelsin both class files.You must organize the solution across three distinct files: the execution script, the generated model, and the custom developer logic.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
For partial classes to merge, they must share the exact same namespace. Using a file-scoped namespace like
namespace Business.Models;in both files is the cleanest way to do this.The signature in the generated file should look exactly like this:
partial void Validate();Partial methods are optional; if you don't implement the body in the custom file, the compiler will simply remove the call from the generated code.