❔ Generic factory with dependency injection

Hello, I am trying to create a generic factory. However, I don't want to pass T as it creates much more boilerplate code:
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();
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();
Is this something that is possible?
4 Replies
Anton
Anton2y ago
what would that do?
Determinism
DeterminismOP2y ago
I guess I'm really asking if the way it is Is fine
mikernet
mikernet2y ago
Just create a non-generic extension method that calls all the generic methods?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server