Hello,
This is not entirely related to Code generation but I have a question for understanding the right architecture for using LLBLGen.
Basically lets say I have a Employee table and a Department table. Employees belong to a department. So Employee table has a foreign key called DepartmentID. However DepartmentID is Nullable.
Now when WinForms code is generated (or even by hand), when we create a form and show Departments in a Combo box. To populate we fetch all the departments as a List and bind it to ComboBox DataSource. However the problem is there is no way to specify NULL.
One of the ways I have come up with is, when binding, I create a NullEntity:
Department nullDept = new Department();
nullDept.ID = -1;
deptList = departmentService.GetAll();
deptList.Add(nullDept);
// Databind.
Now on Save button's handler:
btnSave_Clicked() {
if(empForm.Employee.DepartmentID == -1) empForm.Employee.DepartmentID = null;
// Save here.
}
So my question is:
-
Is this the right way? Or is there any other way?
-
Maybe the code generators should define a static NullEmployee and NullDepartment so that we can reuse these. I can do these myself if this is the right pattern.
Basically this way we need to manually detect for null entity or NULL value. If there was a better way so that I can do something like this (pseudocode):
deptList = GetAllIncludingNull();
// .....
// At save, simply call Save and it should detect NullEntity.
Save(employee);
Regards
Jack