Now, let??™s show this same example using C# instance fields. This is an example
of how to do object-oriented programming in NModel.
namespace Payroll2
{
class Employee : LabeledInstance
{
static Set allEmployees = Set.EmptySet;
int salary;
[Action]
static void CreateEmployee([Domain("new")] Employee emp)
{ allEmployees = allEmployees.Add(emp); }
[Action]
[Domain("allEmployees")]
void DeleteEmployee()
{ allEmployees = allEmployees.Remove(this); }
[Action]
[Domain("allEmployees")]
252 Modeling Objects
void SetSalary(int x)
{ this.salary = x; }
public override void Initialize()
{ this.salary = 0; }
}
}
If you compare this version to the previous example, you will notice that we have
introduced a C# instance field for salary. Also, instance methods have replaced some
of the static methods for the actions.
The method of instance creation differs as well. The Domain attribute that supplies
parameter values for the CreateEmployee is given the string "new" as its argument.
The NModel framework provides a parameter generator function for each class that
inherits LabeledInstance.
Both versions of this example have exactly the same possible traces. For example,
Payroll1 and Payroll2 may both have the trace:
CreateEmployee(Employee(1))
SetSalary(Employee(1), 200)
CreateEmployee(Employee(2))
SetSalary(Employee(2), 400)
DeleteEmployee(Employee(1))
If you were to inspect the state, you would see that the modeling tool maintains
a field map for you for the salary field.
Pages:
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334