mercredi 13 décembre 2017

Replace static methods within a DDD model

I need to price resources (meeting room, desks...). One of the requirements is to return several pricing "strategies" to the users so he/she could select the best option.

  • RegularPricingStrategy
  • HalfDayPricingStrategy
  • DiscountPricingStrategy
  • (...)

Each strategy works in a similar way. It tries to break the booking into time slots and price each of them individually.

  • From 08:00AM to 08:30AM it's this price
  • From 08:30AM To 16:30 it's this price
  • (...)

    My current implement relies on a based class called PricingStrategy

    public abstract class PricingStrategy { protected Booking Booking { get; } protected Resource Resource { get; } protected TecTacClient Client { get; }

    protected PricingStrategy(
        Booking booking,
        Resource resource,
        Client client)
    {
      (...)
    }
    
    public abstract int PricingStragyId { get; }
    public abstract bool IsEligible { get; }
    public abstract Task<Pricings.Pricing> CalculateAsync();
    
    

    }

Eventually each specialized strategy will be implemented in a class inheriting from PricingStrategy

public class EntitlementPricingStrategy : PricingStrategy
{
    private readonly List<Entitlement> _entitlements;
    public override int PricingStragyId { get; } = 0;

    public EntitlementPricingStrategy(
        Booking booking, 
        Resource resource, 
        TecTacClient client,
        IEnumerable<Entitlement> entitlements) 
        : base(booking, resource, client)
    {
        _entitlements = new List<Entitlement>(entitlements ?? Enumerable.Empty<Entitlement>());
    }

    public override bool IsEligible => true;

    public override async Task<Pricings.Pricing> CalculateAsync()
    {
        var pricing = new Pricings.Pricing(PricingStragyId, "HKD");

        return await Task.FromResult(pricing);
    }
}

Now I'm looking for a good way to price each time slot. Currently I use tons of simple static methods. Each of them get a booking, extract the time slots they can price and returns a collection of PricingSteps.

    public override async Task<Pricings.Pricing> CalculateAsync()
    {
        var currencyCode = Resource.Pricings.First().CurrencyCode; //TODO: One day we have different pricing per currency
        var pricing = new Pricings.Pricing(PricingStragyId, currencyCode);

        pricing.AddSteps(EntitlementPricingCalculator.MeetingRoom(Booking, Resource, _entitlements));
        pricing.AddSteps(HourlyPricingCalculator.NonOperatingHours(Booking, Resource));
        pricing.AddSteps(HourlyPricingCalculator.OfficeHours(Booking, Resource));

        return await Task.FromResult(pricing);
    }

I was wondering if there is a better way (DDD oriented) to calculate time slots so I could get rid off the static methods.

Thx

Aucun commentaire:

Enregistrer un commentaire