我正在使用DryIoc和ExpressMapper。
我将ExpressMapper包装在另一个类中,在某些时候它应该用于映射到带有参数化构造函数的类型。实际上,当我将视图模型映射到业务模型时。该参数是存储库类的实例。
使用TDD,我的Mapper类就像这样(请注意构造函数接受Func)。
public class Mapper<T1, T2> : IMapper<T1, T2>
{
private readonly MappingServiceProvider _mapper;
public Mapper()
{
_mapper = new MappingServiceProvider();
_mapper.Register<T1, T2>();
}
public Mapper(Func<T2> func)
{
_mapper = new MappingServiceProvider();
_mapper.Register<T1, T2>().Instantiate((t1) => func());
}
public T2 Map(T1 t)
{
return _mapper.Map<T1, T2>(t);
}
}
因此我试着像这样使用Ioc:
Func<IActivitiesModel>factoryActivitiesModel = () => container.Resolve<IActivitiesModel>();
container.Register(Made.Of(() => factoryActivitiesModel));
// () => new ActivitiesModel(container.Resolve<IActivityRepository>(IfUnresolved.Throw)
container.Register<IMapper<ActivitiesViewModel, IActivitiesModel>, Mapper<ActivitiesViewModel, IActivitiesModel>>(Reuse.Singleton, Made.Of(
() => new Mapper<ActivitiesViewModel, IActivitiesModel>(Arg.Of<Func<IActivitiesModel>>())
));
但它没有。
Unable to use null factory object with factory method ActivitiesMVC.Ioc.<>c__DisplayClass4_0::System.Func`1[ActivitiesLogic.Models.IActivitiesModel] factoryActivitiesModel when resolving: Func<ActivitiesLogic.Models.IActivitiesModel>.
无论如何,我之后尝试了不同的方法,但似乎都没有。
我想避免将容器存储为静态单例并让映射器依赖它(我希望映射器保持Ioc不可知)。
如何实现?
EDIt:当前解决方案(使用不良静态单例)
public class Ioc
{
public class Factory<T> : IFactory<T>
{
public T Create()
{
return Container.Resolve<T>();
}
}
private static Lazy<Container> _container;
public static Container Container => _container.Value;
static Ioc()
{
_container = new Lazy<Container>(GetContainer);
}
static private Container GetContainer()
{
var container = new Container(rules => rules
//.WithoutThrowOnRegisteringDisposableTransient()
.WithTrackingDisposableTransients());
container.Register(Made.Of(() => new ActivityController(Arg.Of<IMapper<ActivitiesViewModel, IActivitiesModel>>())));
container.Register<ILabContext, LabContext>(new SingletonReuse());
container.Register<IActivityRepository, ActivityRepository>(new SingletonReuse());
container.Register<IActivitiesModel>(made: Made.Of(() => new ActivitiesModel(Arg.Of<IActivityRepository>())));
container.Register(typeof(IFactory<>), typeof(Factory<>), new SingletonReuse());
container.Register<IMapper<ActivitiesViewModel, IActivitiesModel>, Mapper<ActivitiesViewModel, IActivitiesModel>>(Reuse.Singleton, Made.Of(
() => new Mapper<ActivitiesViewModel, IActivitiesModel>(Arg.Of<IFactory<IActivitiesModel>>())
));
container.Register<IMapper<ActivitiesModel, ActivitiesEntity>, Mapper<ActivitiesModel, ActivitiesEntity>>(new SingletonReuse(), Made.Of(
() => new Mapper<ActivitiesModel, ActivitiesEntity>()
));
return container;
;
}
}
答案 0 :(得分:1)
首先,我不熟悉ExpressMapper,因此将尝试仅基于您的示例代码提供帮助。
据我所知,您希望注册活动模型,存储库,虚拟机等,以及依赖于Func依赖关系的IMapper / Mapper实现。另一个细节是mapper有两个构造函数,这就是为什么你可能试图使用Made.Of。
但是Made.Of使用Expression&gt;而不是Func委托。这就是我相信例外的原因。
尝试此设置:
container.Register(typeof(IMapper<,>), typeof(Mapper<,>), Reuse.Singleton,
// will select second constructor with Func parameter
made: FactoryMethod.ConstructorWithResolvableArguments);
// normal model registrations, no need to use Made.Of
// if implementations have single constructor,
// othetwise try use the same made as for Mapper.
container.Register<IActivitiesModel, ActivitiesModel>();
// ... the same way register repository, vm, etc.
如果你的代码中有多个构造函数的情况很常见(可能是ExpressMapper要求有一个默认的ctor?)那么你可以为每个容器配置全局自动构造函数选择:
container = new Container(rules => rules
.With(FactoryMethod.ConstructorWithResolvableArguments));