I am using the built in dependency injector/splat injector inside ReactiveUI.
I have constructors where I want to pass along their applicable data repository.
In other frameworks it just uses reflections with the interfaces, and uses the GetServices call to fulfill those constructor requirements. Eg at the moment I have this helper extension method for creating my class:
/// <summary>
/// Helper class for having a object's constructor automatically assigned by a "GetService" request.
/// </summary>
/// <param name="resolver">The resolver.</param>
/// <param name="type">The type to register.</param>
public static void Register<TConcrete, TInterface>(this IMutableDependencyResolver resolver)
where TConcrete : class
{
var concreteType = typeof(TConcrete);
// Must be a single constructor
var constructors = concreteType.GetConstructors().Single();
IList<object> values = new List<object>();
foreach (var parameter in constructors.GetParameters())
{
if (parameter.ParameterType.IsInterface == false)
{
throw new InvalidOperationException($"The type {concreteType.Name} has constructor paramters that are not interfaces.");
}
values.Add(resolver.GetService(parameter.ParameterType));
}
resolver.Register(() => Activator.CreateInstance(concreteType, values.ToArray()), typeof(TInterface));
}
The reason why I use that helper class is to avoid having to do the following in my AppBootStrapper:
dependencyResolver.Register(() => new SetupFlightViewModel(dependencyResolver.GetService<IScreen>(), dependencyResolver.GetService<IFlightsModel>(), dependencyResolver.GetService<IAirportsModel>()), typeof(ISetupFlightViewModel));
Just checking to make sure I am not doing anything non-obvious with the Splat/ReactiveUI framework and it's already provided. I realise there is a performance cost of doing the above and could probably use the Expression Tree compiled expressions to avoid the reflection cost every time or something.
Thanks for your help, Glenn