JD2022-TU1/main/tools/services/JD.Adobe.Service/TargetMemories/SimpleTargetMemory.cs

57 lines
No EOL
1.7 KiB
C#

using System;
using Plume.Core.Logging;
using Shuttle.Servers;
namespace JD.Adobe.Service.TargetMemories
{
public class SimpleTargetMemory : ITargetMemory
{
protected ILogger Logger { get; }
public event Action<TargetAppInfo> OnTargetChanged;
public bool HasTarget => Target != TargetAppInfo.Empty;
public TargetAppInfo Target { get; private set; }
public SimpleTargetMemory(ILogger logger)
{
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
Target = TargetAppInfo.Empty;
}
public void SetTarget(TargetAppInfo target)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
if (target.ConnectionId == null)
{
throw new ArgumentException($"Cannot set target because property '{nameof(target.ConnectionId)}' of {target.GetType().Name} was null.", nameof(target));
}
Logger.Debug($"Attempting to memorize target '{target.ShuttleId} ({target.ConnectionId})'...");
if (target.Equals(Target))
{
Logger.Debug($"'{target.ShuttleId} ({target.ConnectionId})' was already the target.");
return;
}
Target = target;
Logger.Debug($"'{target.ShuttleId} ({target.ConnectionId})' was internally set as the target. Raising event...");
OnTargetChanged?.Invoke(Target);
Logger.Debug($"Done setting '{target.ShuttleId} ({target.ConnectionId})' as the new target!");
}
public void ClearTarget()
{
SetTarget(TargetAppInfo.Empty);
}
}
}