internal static class MonitoredValueInstanceFactoryExtension
{
public static void AddMonitoredValueInstanceFactory<T>(this IServiceCollection services)
{
services.AddTransient<IMonitoredValue<T>, MonitoredValue<T>>();
services.AddTransient<Func<IMonitoredValue<T>>>(x => x.GetRequiredService<IMonitoredValue<T>>);
services.AddSingleton<IMonitoredValueInstanceFactory<T>, MonitoredValueInstanceFactory<T>>();
}
}
internal sealed class MonitoredValueInstanceFactory<T> : IMonitoredValueInstanceFactory<T>
{
private readonly Func<IMonitoredValue<T>> _factory;
public MonitoredValueInstanceFactory(Func<IMonitoredValue<T>> factory)
{
_factory = factory;
}
public IMonitoredValue<T> Create(string location, Subscription sub, ushort namespaceIndex = 2, bool callbackOnInitialize = true)
{
var factory = _factory();
MonitoredValue<T> mi = new(sub.DefaultItem)
{
DisplayName = location,
StartNodeId = new NodeId(location, namespaceIndex),
CallbackOnInitialize = callbackOnInitialize
};
sub.AddItem(mi);
return factory;
}
}
// Program.cs, how it currently needs to be called
services.AddMonitoredValueInstanceFactory<string>();
services.AddMonitoredValueInstanceFactory<float>();
services.AddMonitoredValueInstanceFactory<int>();
services.AddMonitoredValueInstanceFactory<short>();
services.AddMonitoredValueInstanceFactory<bool>();
services.AddMonitoredValueInstanceFactory<ushort>();
// I would prefer to call it like this with no type
services.AddMonitoredValueInstanceFactory();