StructureMap 中的命名单例实例(多个 nHibernate 会话工厂) [英] Named singleton instance in StructureMap (Multiple nHibernate session factories)

查看:47
本文介绍了StructureMap 中的命名单例实例(多个 nHibernate 会话工厂)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我有两个 Nhibernate SessionFactory,我需要向 StructureMap 注册一个使用.只有 Foo 需要 mySessionFactory 会话.

I have a scenario where I have two Nhibernate SessionFactorys I need to register an use with StructureMap. Only Foo needs mySessionFactory sessions.

像这样:

For<ISessionFactory>().Singleton().Use(NHibernateConfiguration.GetDefaultSessionFactory());
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession());
For<ISessionFactory>().Singleton().Use(AnotherNHibernateConfiguration.GetDefaultSessionFactory).Named("mySessionFactory");
For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>("mySessionFactory").OpenSession()).Named("mySession");

For<Foo>()
   .Use<Foo>()
   .Ctor<ISession>("session").Is(x => x.TheInstanceNamed("mySession"));

问题是当我只想在 Foo 中使用而其他任何地方都应该使用我的未命名实例时,现在到处都使用了 mySessionFactory.

The problem is that mySessionFactory is now used everywhere when I only wanted to to be used in Foo and everywhere else should use my unnamed instance.

我做错了什么?

推荐答案

在两个命名实例上,将 Use 更改为 Add.使用将该实例设置为默认值并将其添加为实例.您也可以颠倒配置的顺序(使用 Use 添加的类型的最后一个实例将成为默认值),但使用 Add 方法更加明确.

On both your named instances, change Use to Add. Use sets that instance as the default as well as adding it as an instance. You could also reverse the order of your config (the last instance of a type added with Use will become the default), but using the Add method is much more explicit.

这篇关于StructureMap 中的命名单例实例(多个 nHibernate 会话工厂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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