帮助structuremap配置 [英] Help with structuremap configuration

查看:92
本文介绍了帮助structuremap配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置Structuremap类似下面的,但我似乎可以得到它完全正确。

I am trying to configure Structuremap something like the following but I can seem to get it quite right

ObjectFactory.Initialize(x => {

    x.For<TunaRepository>()
        .Use(new TunaRepository(serviceEndpoint))
        .Named("Tuna");

    x.For<CodRepository>()
        .Use(new CodRepository(serviceEndpoint))
        .Named("Cod");

    x.For<HaddockRepository>()
        .Use(new HaddockRepository(serviceEndpoint))
        .Named("Haddock");

    x.For<IFishRepository>().AddInstances(y =>
                                        {
                                            y.OfConcreteType<TunaRepository>().
                                            // Somehow add all instances here???

                                        });

    x.For<TunaController>().Use<TunaController>()
        .Ctor<IFishRepository>("repo").Is<TunaRepository>(); // This Is should use the instance registered above

    x.For<CodController>().Use<CodController>()
        .Ctor<IFishRepository>("repo").???

    x.For<HaddockController>().Use<HaddockController>()
        .Ctor<IFishRepository>("repo").???

});

在我的code点,我希望能够做到:

At points in my code I want to be able to do:

var instances = ObjectFactory.GetAllInstances<IFishRepository>();

和也:

var instance = ObjectFactory.GetNamedInstance<IFishRepository>("Cod");

,也可以使用IFishRepository作为实际参数的到我的控制器:

and also use IFishRepository as an arguement to my controllers:

public TunaController(IFishRepository repo ...

所以我的问题是什么的最佳方式来配置我的ObjectFactory?

So my question is whats the best way to configure my ObjectFactory?

推荐答案

在注册命名实例,不应该是你使用Add方法的默认实例:

When registering named instances that should not be the Default instance you use the Add method:

 x.For<IFishRepository>()
   .Add(() => new TunaRepository(serviceEndpoint))
   .Named("Tuna");

也观察到,除非你想的TunaRepository是一个单身,你应该用一个lambda注册。当请求实例,并会在我的例子返回一个新的实例拉姆达将进行评估。当心与serviceEndpoint以及并确保,如果你想为所有存储库中的单实例。一般来说,我倾向于要连接使用&LT我所​​有的依赖关系;而在结构图>()在可能的情况,而不是使用新;>()使用&lt。这样做使得架构更加灵活,特别是因为你可以添加新的参数到构建函数不改变注册。

Also observe that unless you want the TunaRepository to be a Singleton you should register it using a lambda. The lambda will be evaluated when the instance is requested and will in my example return a new instance. Watch out with the serviceEndpoint as well and make sure if you want that single instance for all repositories. Generally I tend to wire up all my dependencies using For<>().Use<>() in structure map where possible instead of using "new". Doing so makes the architecture much more flexible, especially since you can add new parameters to the ctors without changing the registrations.

有关指定命名实例在一个构造函数中你可以使用这个语法:

For specifying a named instance to be used in a constructor you can use this syntax:

 x.For<TunaController>().Use<TunaController>().
   Ctor<IFishRepository>("repo").
   Is(i => i.TheInstanceNamed("Tuna")); 

由于所有的注册都遵循同样的模式,我会用一个的 IRegistrationConvention 以删除重复code,尽量减少增加新库的努力。

Since all your registrations are following the same patterns, I would use an IRegistrationConvention in order to remove duplicate code and minimize the effort of adding new repositories.

这篇关于帮助structuremap配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆