Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
slides
1. Employee Class
// Fig. 10.9: Employee.cs
// Abstract base class for company employees.
using System;
public abstract class Employee
{
private string firstName;
private string lastName;
private string socialSecurityNumber;
// constructor
public Employee(string firstNameValue,
string lastNameValue, string ssn)
{
FirstName = firstNameValue;
LastName = lastNameValue;
SocialSecurityNumber = ssn;
}
public string SocialSecurityNumber // property SS Number
{
get
{
return socialSecurityNumber;
}
set { socialSecurityNumber = value; }
// end get
}
// property FirstName
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
// property LastName
public string LastName
{
get
{
return lastName;
}
set
2. {
lastName = value;
}
}
// return string representation of Employee
public override string ToString()
{
return FirstName + " " + LastName + "" + SocialSecurityNumber;
}
// abstract method that must be implemented for each derived
// class of Employee to calculate specific earnings
public abstract decimal Earnings();
} // end class Employee
Commission Employee
using System;
public class CommissionEmployee : Employee //Fig 10.4,CommissionEmployee
{
private decimal grossSales; // gross weekly sales
private decimal commissionRate;
// commission percentage
public CommissionEmployee(string first, string last, string ssn,
decimal sales, decimal rate):base(first,last,ssn)
{
// implicit calls to all base constructors starts here
GrossSales = sales; // validate gross sales via property
CommissionRate = rate; // validate commission rate via property
} // end five-parameter CommissionEmployee constructor
public decimal GrossSales // property GrossSales
{
get
{
return grossSales;
3. } // end get
set
{
grossSales = (value < 0) ? 0 : value;
} // end set
} // end property GrossSales
public decimal CommissionRate // Property CommisionRate
{
get
{
return commissionRate;
} // end get
set
{
commissionRate = (value > 0 && value < 1) ? value : 0;
} // end set
} // end property
//
public override decimal Earnings() // Method to calculate
earnings
{
return commissionRate * grossSales;
}
public override string ToString() // Method ToString
{
return string.Format(base.ToString()
, GrossSales, CommissionRate);
}
} // end class CommissionEmployee
4. BasePlusCommission
public class BaseplusCommissionEmployee : CommissionEmployee//Fig
10.4,CommissionEmployee
{
private decimal basesalary;
public BaseplusCommissionEmployee(string first, string last, string ssn,
decimal sales, decimal rate, decimal salary)
: base(first, last, ssn, sales, rate)
{
BaseSalary = salary;
} // end five-parameter CommissionEmployee constructor
public decimal BaseSalary
{
get { return basesalary; }
set { basesalary = (value < 0) ? 0 : value; }
}
//
public override decimal Earnings() // Method to calculate
earnings
{
return basesalary + base.Earnings();
}
public override string ToString() // Method ToString
{
return string.Format("{0}{1}{2}",base.ToString(),
"base salary", BaseSalary);
}
} // end class CommissionEmployee
5. Overtime
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class HourlyEmployee : Employee
{
private decimal hourp;
private decimal hourc;
public HourlyEmployee(string first, string last, string ssn, decimal
hourprice, int houercredit)
: base(first, last, ssn)
{
Hour = hourprice;
Credit = houercredit;
}
public decimal Hour
{
get { return hourp; }
set { hourp = (value < 0) ? 0 : value; }
}
public decimal Credit
{
get { return hourc; }
set { hourc = (value < 0) ? 0 : value; }
}
public override decimal Earnings()
{
if (hourc <= 40)
return hourp * hourc;
else
return (40* hourp) + ((hourc - 40) * hourp * 1.5m);
public override string ToString()
{
return String.Format("{0} {1}", base.ToString(), Earnings());
}
}
6. Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication149
{
class Program
{
static void Main(string[] args)
{
Employee e1 = new CommissionEmployee("ahmad", "yaseer", "19999",
1.5M, 3.6M);
Employee e2 = new BaseplusCommissionEmployee("Amer", "yaseen",
"19999", 1.5M, 3.6M, 240.0M);
HourlyEmployee O5 = new HourlyEmployee("hamzeh", "fadi", "19999",
10.0m, 5);
Employee e4 = O5;
e4.Earnings();
Console.WriteLine(e2.Earnings());
}
}
}