Search code examples
c#algorithmrecurrence

Implementing non-date driven recurrence


I am developing an application that essentially prompts users when they need to perform Preventative Maintenance on various pieces of equipment. Each piece of equipment (call it a tool) has a different recurrence pattern. Some will be based on time, such as every 90 days, while others are based on other factors such as usage. For example, a particular tool might need PM after it has been checked out X number of times.

All tools are assigned to an individual so when that user signs into the application I need to display a list of the tools that require PM at that time. As PM is completed, those items drop off the list and as other circumstance, such as the date changes or the tool is checked back into the crib, tools will be added.

I am starting with the design of the service method that will return this list and am looking for some help with the approach I use to resolve what items are in the list. For inputs I will have the user's identifier and the current date. From that I need to query the list of tools and determine based on their recurrance pattern, the last time PM was performed and the events that have taken place since the last PM, whether that item should appear in the list.

Hopefully that makes sense. I appreciate any thoughts and guidance on how I should approach this logic.


Solution

  • It sounds to me like each Tool is going to have it's own logic for determining whether PM is required or not. So I would define an IEquipment interface with a couple enums, something like this:

    public enum RecurrenceType
    {
        //Values
    }
    
    public enum RecurrenceFrequency
    {
        //Values
    }
    
    public interface IEquipment
    {
        bool IsPMRequired();
    
        RecurrenceType RecurrenceType { get; }
        RecurrenceFrequency RecurrenceFrequency { get; }
        //You may want to choose something other than object, or eliminate this property
        object RecurrenceValue { get; set; } 
    }
    

    Then each type of equipment would implement the interface, and the logic to determine whether or not PM is required could be implemented:

    public class Tractor : IEquipment
    {
        public bool IsPMRequired()
        {
            //Implement logic here specific to a Tractor
            throw new NotImplementedException();
        }
    
        public RecurrenceType RecurrenceType
        {
            get { throw new NotImplementedException(); }
        }
    
        public RecurrenceFrequency RecurrenceFrequency
        {
            get { throw new NotImplementedException(); }
        }
    
        public object RecurrenceValue
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
    

    Then your logic to use these objects would be something like:

    List<IEquipment> equipment = new List<IEquipment>();
    //Populate equipment
    List<IEquipment> display = equipment.Where(e => e.IsPMRequired()).ToList();
    //Display the equipment that needs PM
    

    It might also make sense to have a base class for Equipment where you put common method/property definitions.