Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the sake of closure, I thought I'd update the post with the results of my effort although I am giving credit for the answer to both Brandon and Florian for sending my on the right path.</p> <p>I ended up with an interface, as Brandon suggested, called IHaveRecurrence defined as follows:</p> <pre><code>public interface IHaveRecurrence { DateTime? LastOccurrence { get; } RecurrenceType RecurrenceType { get; } Int32 RecurrenceValue { get; } Boolean IsDue(); } </code></pre> <p>My MaintainableTool (base) class now implements this interface. The IsDue method is implemented by delegating to the Recurrence class:</p> <pre><code>public Boolean IsDue() { return Recurrence.IsDue(this); } </code></pre> <p>Recurrence is an abstract base class as suggested by Florian. I have several subclasses such as DailyRecurrence, WeeklyRecurrence, etc. Each of the subclasses corresponds to one of the values in the RecurrenceType enumeration and implements the appropriate logic to determine if PM is due based on the RecurrenceValue and LastOccurrence properties via the IHaveRecurrence interface.</p> <p>Recurrence uses the internal RecurrenceFactory class to resolve which subclass to use:</p> <pre><code>internal sealed class RecurrenceFactory { public Recurrence GetRecurrence(RecurrenceType type) { switch (type) { case Daily: return new DailyRecurrence; : } } } </code></pre> <p>And Recurrence is implemented as:</p> <pre><code>public abstract class Recurrence : IDisposable { public static Boolean IsDue(IHaveRecurrence recurringObj) { using (var recurrence = RecurrenceFactory.GetRecurrence(recurringObj.RecurrenceType)) { return recurrence.GetIsDue(recurringObj); } } protected abstract Boolean GetIsDue(IHaveRecurrence recurringObj); } </code></pre> <p>Then, for instance, the DailyRecurrence class is implemented as:</p> <pre><code>public sealed class DailyRecurrence : Recurrence { protected override Boolean GetIsDue(IHaveRecurrence recurringObj) { if (recurringObj.LastOccurred.HasValue) return recurringObj.LastOccurred.AddDays(recurringObj.RecurrenceValue) &lt;= DateTime.Now; return true; } } </code></pre> <p>I like this model as it is highly extensible, encapsulates the logic required to resolve each recurrence pattern and keeps my business object clean.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload