Exercise: Auto-Generated Code Simulation

Utilize partial classes and partial methods to safely extend auto-generated boilerplate code.

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 Validate in the generated code.

  • Invoke Validate() from inside the existing Process() method.

  • Implement the Validate method in the custom developer file to check if Total < 0.

  • Print a warning if the total is negative, otherwise print a validation success message.

Constraints

  • You must use the partial keyword on the Invoice class in both locations.

  • You must use the partial keyword on the Validate method.

  • You must use a file-scoped namespace named Business.Models in 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

Utilize partial classes and partial methods to safely extend auto-generated boilerplate code.

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 Validate in the generated code.

  • Invoke Validate() from inside the existing Process() method.

  • Implement the Validate method in the custom developer file to check if Total < 0.

  • Print a warning if the total is negative, otherwise print a validation success message.

Constraints

  • You must use the partial keyword on the Invoice class in both locations.

  • You must use the partial keyword on the Validate method.

  • You must use a file-scoped namespace named Business.Models in 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.

C# 14.0
using Business.Models;
Invoice myInvoice = new Invoice { Id = 1001, Total = -50.0m };
myInvoice.Process();