Monday 15 June 2015

entity framework - How to avoid anemic domain model with business logic in the form of rules -


I am preparing a system in which a simple entity framework is backed domain object based on a series of fields I need to update the rule - I want to apply these rules gradually (in a tight style) and as I am using EF, I doubt about putting each rule in a domain object . However, I want to write "procedural code" and avoid using anemic domain models.

As an example, the object is:

  class employee {private string name; Private float pay; Private float pensionpot; Private bull pensions; Private child-rights; } I need to make rules such as "If salary is more than 100,000 and the qualifying qualification is wrong, then set the right form as correct" and "If the obligation is right, then correct Set the Eligible Force as ".  

There are about 20 such rules and I would like to advise whether they should be implemented in the staff or anything like the staff? My first thought was to create a separate category for each rule that was received from "Rule" and then implementing each rule in the Employee class, possibly using Visitor Pattern, but to expose all the areas of rules to do this In order to do so it seems wrong, despite having every rule on the working class, it does not seem quite right. How can this be implemented?

The second concern is that the actual employee units are supported for framework organizations DB, so I am not happy to add logic to these "institutions" - especially when I need to duplicate the object for each unit of rules If I follow the rules, then I can test the same thing.

Before I apply the rule, I'm considering using Automapper to convert to a simple domain object, but then need to manage the updates of my own fields. Any advice?

One way is to make the code an employee of the internal code . The advantage of this approach is that the area can be private. In addition, the invocation of rules can be applied only by the employee class, to ensure that they are always applicable when necessary: ​​

  class employees {string id; String name; Float pay; Float pensionpot; Bull Pension; Boolean character forward; Public Zero Change Salary (Float Pay) {this.salary = salary; ApplyRules (); } Public Zero MakeAgigibleForPension () {this.eligibleForPension = true; ApplyRules (); // may or may not be necessary) Zero Application Form () {rules.ForEach (rule = & gt; rule. Apply (this)); } Read Only Stable List & lt; IEmployeeRule & gt; Rule; Stable Employee () {Rule = New List & lt; IEmployeeRule & gt; {New incremental PensionEligibilityRule ()}; } IEmployeeRule {Apply zero (employee employee); } Class Salary Practice Disability Rules: IEmployeeRule {Apply Public Zero (Employee Employee) {If (Employee Gold> 100000 & Employee! Eligible Forward) {employee.MakeEffectForPension (); }}}}   

There is a problem here that all rules must be enforced in the employee class. This is not a major problem as the rules involve business conjunctions related to employee pension And so they are together.

No comments:

Post a Comment