I am tasked with creating a payment module which consists of different types of payment methods.
There are different kind of objects that can go in each which can slightly change how some formulas are calculated. IE if a certain property is present, no tax is taken out. This is how I am currently going about it.
public abstract class Payment
{
private readonly PersonToPay _personToPay;
public Payment (PersonToPay personToPay)
{
_personToPay = personToPay;
}
public decimal PayFrom {get; set;}
public decimal PayTo {get; set;}
public decimal PayRate {get; set;}
public decimal Gross {get; set;}
public decimal Tax {get; set;}
public void CalculateGross()
{
return PayRate * [days in period]
}
public void CalculateTax()
{
if (_personToPay.IsTypeOfX)
Tax = 0;
else
Tax = PayRate * .1;
}
public virtual int GetDaysToPay()
{
int totalDays = 0;
for (var x in _personToPay.ListOfItems)
{
if (x == someTypeOfValue)
totalDays++;
}
return totalDays;
}
}
I am trying to take all of the functionality in this class and pass it to other classes but since all of the functionality is tied to the 'PersonToThePay' object that is injected, how can I do as such in the derived classes? An example would be the following:
public PaymentMethodA : Payment
{
private readonly PersonToPay _personToPay;
public PaymentMethodA(PersonToPay personToPay)
{
_personToPay = personToPay; // this personToPay would set the parent classes PersonToPay above to this
}
public int PaymentMethodAProperty;
public override int GetDaysToPay()
{
return PaymentMethodAProperty * 2;
//do PaymentMethodA logic here
}
}
I was thinking I could just make the PersonToPay a property but if you can't make a payment without a payee, doesn't it make more sense for it to be injected? I was also looking into the strategy pattern for this but to me the template seems to make more sense. Although the functionality is largely the same now, I am trying to make it as adaptable to change as possible hence the separate classes.
Any insight into what would be best to do here would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire