|
|
|
@ -0,0 +1,64 @@ |
|
|
|
|
|
|
|
using MultiTerm.Protocols.Types; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace MultiTerm.Protocols.Factories; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Can create <see cref="ICommunicationProtocol"/> instances during runtime. |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
public class CommunicationProtocolFactory : ICommunicationProtocolFactory |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
private readonly Func<IEnumerable<ICommunicationProtocol>> protocolFactory; |
|
|
|
|
|
|
|
private readonly Func<IEnumerable<IProtocolSettingsViewModel>> settingsViewModelFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Constructor of <see cref="CommunicationProtocolFactory"/>. |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="protocolFactory">function that gets all registered services with Interface <see cref="ICommunicationProtocol"/></param> |
|
|
|
|
|
|
|
/// <param name="settingsViewModelFactory">function that gets all registered services with Interface <see cref="IProtocolSettingsViewModel"/></param> |
|
|
|
|
|
|
|
public CommunicationProtocolFactory(Func<IEnumerable<ICommunicationProtocol>> protocolFactory, Func<IEnumerable<IProtocolSettingsViewModel>> settingsViewModelFactory) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.protocolFactory = protocolFactory; |
|
|
|
|
|
|
|
this.settingsViewModelFactory = settingsViewModelFactory; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
|
|
|
/// Creates a new <see cref="ICommunicationProtocol"/> and its corresponding <see cref="IProtocolSettingsViewModel"/>. |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="protocolType"><see cref="ProtocolType"/> of the new <see cref="ICommunicationProtocol"/></param> |
|
|
|
|
|
|
|
/// <returns>tuple of communication protocol and protocol settings viewmodel</returns> |
|
|
|
|
|
|
|
public Tuple<ICommunicationProtocol, IProtocolSettingsViewModel> Create(ProtocolType protocolType) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ICommunicationProtocol protocol; |
|
|
|
|
|
|
|
IProtocolSettingsViewModel settingsViewModel; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get all registered ICommunicationProtocol as IEnumerable |
|
|
|
|
|
|
|
var registeredProtocols = protocolFactory(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get all registered IProtocolSettingsViewModel as IEnumerable |
|
|
|
|
|
|
|
var registeredSettingsViewModel = settingsViewModelFactory(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// search for correct type of protocol implementation by ProtocolType |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
protocol = registeredProtocols.Where(x => x.ProtocolType == protocolType).First(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new NotImplementedException($"'{nameof(CommunicationProtocolFactory)}' cannot create CommunicationProtocol with {nameof(ProtocolType)} {protocolType}", ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// search for correct type of settings View Model implementation by ProtocolType |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
settingsViewModel = registeredSettingsViewModel.Where(x => x.ProtocolType == protocolType).First(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new NotImplementedException($"'{nameof(CommunicationProtocolFactory)}' cannot create ProtocolSettingsViewModel with {nameof(ProtocolType)} {protocolType}", ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return the filled up instances |
|
|
|
|
|
|
|
return new Tuple<ICommunicationProtocol, IProtocolSettingsViewModel>(protocol, settingsViewModel); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |