我该如何在 Unity 中执行此操作?

发布于 2024-07-30 01:10:27 字数 1402 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-08-06 01:10:27

这就是我最终所做的:

       IEnumerable<IObjectMapper> allMappers = new List<IObjectMapper>() { 
            new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
            new StringMapper(),
            new FlagsEnumMapper(),
            new EnumMapper(),
            new ArrayMapper(),
            new DictionaryMapper(),
            new EnumerableMapper(),
            new AssignableMapper(),
            //new TypeConverterMapper(),
            new NullableMapper(),
        };

        container.RegisterType<Configuration>(new SingletonLifetime())
                        .Configure<InjectedMembers>()
                            .ConfigureInjectionFor<Configuration>(
                                new InjectionConstructor(allMappers));

    container.RegisterType<IConfigurationProvider, Configuration>();
    container.RegisterType<IConfiguration, Configuration>();
    container.RegisterType<IMappingEngine, MappingEngine>();

这可行,但如果其他人有更好的实现,我会洗耳恭听,这仍然有赏金。

This is what I ended up doing:

       IEnumerable<IObjectMapper> allMappers = new List<IObjectMapper>() { 
            new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
            new StringMapper(),
            new FlagsEnumMapper(),
            new EnumMapper(),
            new ArrayMapper(),
            new DictionaryMapper(),
            new EnumerableMapper(),
            new AssignableMapper(),
            //new TypeConverterMapper(),
            new NullableMapper(),
        };

        container.RegisterType<Configuration>(new SingletonLifetime())
                        .Configure<InjectedMembers>()
                            .ConfigureInjectionFor<Configuration>(
                                new InjectionConstructor(allMappers));

    container.RegisterType<IConfigurationProvider, Configuration>();
    container.RegisterType<IConfiguration, Configuration>();
    container.RegisterType<IMappingEngine, MappingEngine>();

This works but if someone else has a better implementation I'm all ears and this still has a bounty on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文