Reputation: 3879
In an application using Autofac as its IoC container, I have a generic interface with two type parameters:
public interface IMapper<TSource, TTarget>
{
TTarget GetTarget(TSource source);
}
and a wrapper interface to dynamically select the appropriate IMapper<TSource, TTarget>
depending on its input parameter types:
public interface IDynamicMapper
{
T GetTarget<T>(object source);
}
I want my implementation of IDynamicMapper to find at runtime the appropriate IMapper<TSource, TTarget>
component, using Autofac, which has a TSource
equal to source.GetType()
and TTarget
being a derived type of T
(or T
itself):
public class DynamicMapper : IDynamicMapper
{
private readonly ILifetimeScope _scope;
public DynamicMapper(ILifetimeScope scope)
{
this._scope = scope;
}
T IDynamicMapper.GetTarget<T>(object source)
{
Type sourceType = source.GetType();
Type targetBaseType = typeof(T);
//TODO: find an IMapper<TSource, TTarget> implementation where
// 1) Condition on TSource: typeof(TSource) == sourceType
// 2) Condition on TTarget: targetBaseType.IsAssignableFrom(typeof(TTarget))
// Many implementations can potentially match these criterias,
// choose the 1st one
// (this should not happen if the application is designed correctly)
if (mapper == null)
{
throw new ArgumentException(
"Could not find an IMapper<TSource, TTarget> implementation" +
" for the supplied parameters"
);
}
// call mapper.GetTarget(source) and return the result
// (mapper is probably dynamic, but its runtime type implements
// TTarget IMapper<TSource, TTarget>.GetTarget(TSource source))
}
}
All my components are registered to the Autofac container as their service interfaces in another part of the application (using assembly scanning for the record).
UPDATE 1
Based on Steven's pertinent answers I updated my interface as follow to use variance:
public interface IMapper<in TSource, out TTarget>
{
TTarget GetTarget(TSource source);
}
My dynamic mapper's GetTarget()
method looks like this:
T IDynamicMapper.GetTarget<T>(object source)
{
Type sourceType = source.GetType();
Type targetBaseType = typeof(TTarget);
Type mapperType = typeof(IMapper<,>).MakeGenericType(sourceType, targetBaseType);
// This fails with ComponentNotRegisteredException
dynamic mapper = this._scope.Resolve(mapperType);
// This also fails (mapper is null):
// IEnumerable<object> mappers = (IEnumerable<object>)this._scope.Resolve(typeof(IEnumerable<>).MakeGenericType(mapperType));
// dynamic mapper = mappers.FirstOrDefault();
// Invoke method
return mapper.GetTarget((dynamic)source);
}
However, when calling Resolve(mapperType)
or Resolve(typeof(IEnumerable<>).MakeGenericType(mapperType))
, the component is not resolved, although it is present in the container's registrations, mapped to the service IMapper<TSource, TTarget>
. The 1st call throws an exception and the 2nd one returns an empty enumerable.
Upvotes: 2
Views: 1912
Reputation: 3879
Autofac does not support covariant generic types (ISomething<out T>
). Another IoC container like Simple Injector could do the trick in this case, but to make it work with Autofac I ended up using another interface:
Services:
public interface IMapper<TSource, out TTarget> : IMapperLocator<TSource>
{
TTarget Extract(TSource source);
}
public interface IMapperLocator<TSource>
{
}
public interface IDynamicMapper
{
T Extract<T>(object source);
}
Implementation:
public class DynamicMapper : IDynamicMapper
{
private readonly ILifetimeScope _scope;
public DynamicMapper(ILifetimeScope scope)
{
this._scope = scope;
}
T IDynamicMapper.Extract<T>(object source)
{
// Get useful types
Type sourceType = source.GetType();
Type targetBaseType = typeof(TTarget);
Type mapperBaseType = typeof(IMapper<,>).MakeGenericType(sourceType, targetBaseType);
Type locatorType = typeof(IMapperLocator<>).MakeGenericType(sourceType);
Type enumType = typeof(IEnumerable<>).MakeGenericType(locatorType);
// Get all mapper implementations that take a TSource with the
// same type as the source object
var mappers = (IEnumerable<object>)this._scope.Resolve(enumType);
// Among all the mappers with the right TSource, select the one
// with TTarget assignable to T (throws if there is 0 or more than
// one mapper, as this would be an implementation error)
dynamic mapper = mappers.Single(x => mapperBaseType.IsAssignableFrom(x.GetType()));
// The method must implemented implicitly.
// A workaround would be to use a wrapper (IMapperWrapper<TSource, out TTarget>)
// that implements the method implicitly and invokes the mapper's method
// without using dynamic
return mapper.Extract((dynamic)source);
}
}
Upvotes: 3
Reputation: 172676
This should do the trick:
T IDynamicMapper.GetTarget<T>(object source) {
Type mapperType = typeof(IMapper<,>).MakeGenericType(source.GetType(), typeof(T));
// Will throw when no registration exists.
// Note the use of 'dynamic'.
dynamic mapper = scope.Resolve(mapperType);
return (T)mapper.GetTarget<T>((dynamic)source);
}
Upvotes: 4