You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.8 KiB
34 lines
1.8 KiB
using Microsoft.Extensions.DependencyInjection;
|
|
using MultiTerm.Protocols.Factories;
|
|
using MultiTerm.Protocols.Serial;
|
|
|
|
namespace MultiTerm.Protocols.Helpers;
|
|
|
|
public static class ServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// Service Extension to add all implementations of <see cref="ICommunicationProtocol"/> to Dependency Injection.
|
|
/// Also adds <see cref="CommunicationProtocolFactory"/> to Dependency injection, which allows instanciation of <see cref="ICommunicationProtocol"/>
|
|
/// and <see cref="IProtocolSettingsViewModel"/> during runtime.
|
|
/// </summary>
|
|
/// <param name="services">existing services collection</param>
|
|
public static void AddCommunicationProtocolFactory(this IServiceCollection services)
|
|
{
|
|
// add all protocol implementations to the services collection
|
|
services.AddTransient<ICommunicationProtocol, SerialProtocol>();
|
|
// TODO extend
|
|
|
|
// add all settings view model implementations to the services collection
|
|
services.AddTransient<IProtocolSettingsViewModel, SerialProtocolSettingsViewModel>();
|
|
// TODO extend
|
|
|
|
// add a function to the services collection, which is used by the CommunicationProtocolFactory
|
|
// to get a list of all registered implementations of ICommunicationProtocol
|
|
services.AddSingleton<Func<IEnumerable<ICommunicationProtocol>>>(x => () => x.GetService<IEnumerable<ICommunicationProtocol>>()!);
|
|
// to get a list of all registered implementations of IProtocolSettingsViewModel
|
|
services.AddSingleton<Func<IEnumerable<IProtocolSettingsViewModel>>>(x => () => x.GetService<IEnumerable<IProtocolSettingsViewModel>>()!);
|
|
|
|
// add the factory itself to the services collection
|
|
services.AddSingleton<ICommunicationProtocolFactory, CommunicationProtocolFactory>();
|
|
}
|
|
}
|
|
|