I am developing a system that has a simple Entity Framework object that supports a domain that has fields that I need to update based on a number of rules. I want to gradually implement these rules (in a flexible style) and, since I use EF, I am skeptical about putting each rule in a domain object. However, I want to avoid writing “procedural code” and using anemic domain models. All this also needs to be checked.
As an example, an object:
class Employee { private string Name; private float Salary; private float PensionPot; private bool _pension; private bool _eligibleForPension; }
I need to create rules such as "if the salary is above 100,000, and _eligibleForPension is false, then set _eligibleForPension as true" and "if _pension is true, then set _eligibleForPension as true".
There are about 20 of these rules, and I'm looking for advice on whether to implement them in the Employee class or in something like the EmployeeRules class? My first thought was to create a separate class for each rule that inherits from the Rule, and then apply each rule to the Employee class, possibly using the Visitor template, but I would have to expose all the fields to the rules to do this so that he feels wrong. The presence of each rule in the Employee class, although not entirely correct. How will this be implemented?
The second problem is that the actual employees are Entity Framework entities supported by the database, so I don’t feel happy adding logic to these “objects” - especially when I need to mock objects for unit testing each rule. How could I mock them if they have rules that I test on the same object?
I thought about using AutoMapper to convert to a simpler domain object before applying the rules, but then I need to manage updates by fields myself. Any advice on this?
Pcurd
source share