我该如何在 Unity 中执行此操作?
Jimmy Bogart 有一篇关于使用 Automapper 的文章使用 IoC 容器。 他有一个使用 StructureMap 的示例,但我使用的是 Unity,我不确定如何正确使用 InjectionConstructor。
下面是文章中的代码,下面是我糟糕的尝试。 谁能告诉我如何正确执行此操作?
public class ConfigurationRegistry : Registry
{
public ConfigurationRegistry()
{
ForRequestedType<Configuration>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.OfConcreteType<Configuration>()
.CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers));
ForRequestedType<IConfigurationProvider>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
ForRequestedType<IConfiguration>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
}
}
我的尝试:
container.RegisterType<IConfiguration, Configuration>(new SingletonLifetime())
.Configure<InjectedMembers>()
.ConfigureInjectionFor<Configuration>(
new InjectionConstructor(typeof(IEnumerable<IObjectMapper>)), MapperRegistry.AllMappers);
Jimmy Bogart has an article on using Automapper with an IoC container. He has an example using StructureMap but I am using Unity and I'm not sure how to use an InjectionConstructor properly.
Below is the code from the Article and below that is my poor attempt. Can anyone tell me how to do this properly?
public class ConfigurationRegistry : Registry
{
public ConfigurationRegistry()
{
ForRequestedType<Configuration>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.OfConcreteType<Configuration>()
.CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers));
ForRequestedType<IConfigurationProvider>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
ForRequestedType<IConfiguration>()
.TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<Configuration>());
}
}
My attempt:
container.RegisterType<IConfiguration, Configuration>(new SingletonLifetime())
.Configure<InjectedMembers>()
.ConfigureInjectionFor<Configuration>(
new InjectionConstructor(typeof(IEnumerable<IObjectMapper>)), MapperRegistry.AllMappers);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我最终所做的:
这可行,但如果其他人有更好的实现,我会洗耳恭听,这仍然有赏金。
This is what I ended up doing:
This works but if someone else has a better implementation I'm all ears and this still has a bounty on it.