Clean Architecture - Where to put business calculations when entities are autogenerated db first efcore?

by Achilles   Last Updated April 16, 2019 08:05 AM

I'm trying to switch to clean architecture from a traditional layered architecture approach and trying to figure out where to put business logic. Please consider the below scenario -

Employee class (exists in Efcore db first scaffolded in core)-

class Employee {
    public int Id {get; set;}
    public int TimeWorked {get; set;}
    public int ZoneCode {get; set;}
    public datetime JoiningDate {get; set;}
    public decimal Reimbursement {get; set;}
class SomeMasterData { //Could be a value object
    public int ZoneCode {get; set;}
    public decimal OvertimeAllowedForZoneCode {get; set;}
class SomeOtherMasterData {  //Could be a value object
    public int OvertimeMultiplier {get; set;} //and whatever other props

Business logic related to the employee -

int CalculateReimbursement(Employee emp, SomeMasterData masterData, , SomeOtherMasterData masterData2) {
    // Use data from Employee (fields like zone code, joining date etc) and
    // data from SomeMasterData to calculate the reimbursement amount for the employee
    // The below business logic is random, created for this question
    if(emp.ZoneCode == masterData.ZoneCode && emp.JoiningDate > 'some date') {
        return masterData.OvertimeAllowedForZoneCode * masterData2.OvertimeMultiplier * emp.TimeWorked;
    else //More business logic based on many entities and value objects

My question is - Considering Clean Architecture, where to put the CalculateReimbursement method? Below are a few options -

  1. In domain services in the core - But is this what domain services are for?
  2. In the Employee class - But that class is autogenerated by Efcore db first scaffolding so I can't change it. Should I create another Employee partial class with this method?
  3. In some sort of "helper" class within the core - if so, what do I call it?
  4. Somewhere else that makes more sense keeping Clean Architecture in mind?

Answers 1

You could use the models created by EF as purely models made for persisting your data. You should map these to an actual Employee domain model class that can hold all your business logic and that would be truly your domain models.

April 16, 2019 07:56 AM

Related Questions

Updated April 12, 2019 08:05 AM

Updated June 14, 2019 10:05 AM

Updated March 26, 2019 16:05 PM

Updated March 26, 2019 09:05 AM

Updated June 28, 2019 22:05 PM