Design Question - How do you break the dependency between classes using an interface?

I apologize in advance, but this will be a long question.

I am stuck. I am trying to learn unit testing, C # and design patterns - all at once. (Perhaps this is my problem.) As such, I read Art of Unit Testing (Osherove) and Clean Code (Martin) and Head First Design Patterns (O'Reilly).

I'm just starting to understand the delegates and events (which you will see if you want to troll my SO questions lately). I'm still not quite getting lambdas.

To contextualize all this, I gave myself a training project that I call goAlarms. I have an Alarm class with the members you expect (NextAlarmTime, Name, AlarmGroup, Event Trigger, etc.).

I wanted the Alarm Timer to be extensible, so I created the IAlarmScheduler interface as follows:

public interface AlarmScheduler
{
    Dictionary<string,Alarm> AlarmList { get; }
    void Startup();
    void Shutdown();
    void AddTrigger(string triggerName, string groupName, Alarm alarm);
    void RemoveTrigger(string triggerName);
    void PauseTrigger(string triggerName);
    void ResumeTrigger(string triggerName);
    void PauseTriggerGroup(string groupName);
    void ResumeTriggerGroup(string groupName);
    void SetSnoozeTrigger(string triggerName, int duration);
    void SetNextOccurrence (string triggerName, DateTime nextOccurrence);
}

This IAlarmScheduler interface defines the component that will raise the Trigger, which will go into my Alarm class and raise the Trigger Event of the alarm itself. This is essentially a Timer component.

I found that the Quartz.net component is great for this, so I created the QuartzAlarmScheduler class that implements IAlarmScheduler.

Everything is fine. My problem is that the Alarm class is abstract, and I want to create many different alarms. For example, I already have a Heartbeat alarm (triggered every (interval) of minutes), AppointmentAlarm (triggered by the set date and time), Daily Alarm (triggered every day in X) and, possibly, others.

Quartz.NET .

- . , Alarm ( ), - Quartz. , Quartz , , Alarm. , , Quartz, TriggerUtils.MakeMinutelyTrigger, . TriggerUtils.MakeDailyTrigger .

, . , TriggerUtils.Make *, . , , .

, , Make, , , , , . QuartzAlarmScheduler , , , .

, , .

+3
4

, AlarmFactory, IAlarmScheduler. .

, , IAlarmScheduler Quartz, , IAlarmScheduler . , factory .

+1

. . ASP.NET HttpContext. google HttpContextWrapper ASP.NET MVC, , , Microsoft System.Web.Abstractions.

. , , .

AlarmSchedulerWrapper, IAlarmScheduler . AlarmSchedulerWrapper IAlarmScheduler. QuartzAlarmScheduler, IAlarmScheduler. , .

AlarmScheduler, (AlarmSchedulerDouble). AlarmSchedulerDouble ( "" ).

AlarmScheduler, , , AlarmSchedulerBase. AlarmSchedulerWrapper AlarmSchedulerDouble .

. , , , . . IAlarmTriggers, , , Quartz, double . , , , . , .

0

, , , , , :)

, , , , , . , . . , "", (, IHaveAMethodThatYouShouldCall TheMethodToCall).

IAmAnAlarmAndWhenAlarmSchedulerThinkItIsTheTimeHeWillLetMeKnow, ItIsTheTime.

AlarmScheduler IAmAlarmSchedulerThatWillNotifyAlarmWhenItIsTheTime RegisterAlarm (IAmAnAlarmAndWhenAlarmSchedulerThinkItIsTheTimeHeWillLetMeKnow alarm).

, Alarm IAmAlarmSchedulerThatWillNotifyAlarmWhenItIsTheTime RegisterAlarm, . AlarmScheduler, , Alarm ItIsTheTime, .

, Alarm, AlarmScheduler Alarm.

, .

0

. AlarmScheduler - , . ITrigger .

AlarmScheduler , ITrigger. , AlarmScheduler ITrigger, , .

, ITriggers .

  • , .
  • Dependency Injection, .
  • AlarmFactory , :

ITrigger CreateMyCustomTriggerType1(param1, param2, param3)

ITrigger CreateMyCustomTriggerType2(param1)

- Enums . enum - Switch, , . , switch . Switch FACTORY, .

0

Source: https://habr.com/ru/post/1748079/


All Articles