StructureMap 中具有多个 nHibernate Session 工厂的命名单例实例 (StructureMap 2.5.4)

发布于 2024-10-28 18:18:50 字数 1412 浏览 1 评论 0原文

我有完全相同的场景 StructureMap 中的命名单例实例(多个 nHibernate 会话工厂)

如果我使用 StructureMap 2.6.2 实现这一点 - 太棒了!

但是,由于遗留项目中涉及的各种因素无法使用最新的 .NET 框架版本以及其他一些原因,我目前必须使用 StructureMap 2.5.4。对于这个问题,假设我在可预见的将来无法升级。

2.5.4 显然在语法上与 2.6.2 有很大不同,我无法弄清楚如何使用它的 API 来实现相同的功能。

特别是,它是这个东西

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"MyName")).Named("MySessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("MyName");

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"My2ndName")).Named("My2ndSessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("My2ndName");

......使用名为 CreateSessionFactory(string) 的方法来创建相关配置。

“Add”和随后的“GetInstance”无效,需要重构才能与 2.5.4 一起使用 - 我太笨了,无法弄清楚如何实现,或者是否确实可以实现相同的目标。

干杯

I have the exact identical scenario as described in Named singleton instance in StructureMap (Multiple nHibernate session factories)

If I implement this using StructureMap 2.6.2 - great!

However, I have to currently use StructureMap 2.5.4 due to various factors involved in legacy projects not being able to use up-to-date .NET framework versions as well as a few other reasons. Assume for this question that I cannot upgrade for the forseeable future.

2.5.4 is obviously quite different in syntax to 2.6.2 and I can't work out how to implement the same thing using it's API.

In particular, it's this stuff

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"MyName")).Named("MySessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("MyName");

For<ISessionFactory>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Singleton))
            .Add(context => CreateSessionFactory(@"My2ndName")).Named("My2ndSessionFactory");
        For<ISession>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid))
            .Add(context => context.GetInstance<ISessionFactory>("MySessionFactory").OpenSession()).Named("My2ndName");

...with a method called CreateSessionFactory(string) that creates the relevant configuration.

The "Add" and subsequent "GetInstance" are invalid and need to be refactored to work with 2.5.4 - I'm just too dumb to work out how, or if indeed you can achieve the same thing.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我做我的改变 2024-11-04 18:18:50

我记得你的语法应该在 2.5.4 中工作。对于 2.5.3,以下语法应该有效:

c.ForRequestedType<ISessionFactory>().CacheBy(InstanceScope.Singleton).
   AddInstances(
     x =>
     {
       x.ConstructedBy(() => CreateSessionFactory("MyName"))
         .WithName("MyName");
       x.ConstructedBy(() => CreateSessionFactory("My2ndName"))
         .WithName("My2ndName");
     });

To my rememberance your syntax should work in 2.5.4. For 2.5.3 the following syntax should work:

c.ForRequestedType<ISessionFactory>().CacheBy(InstanceScope.Singleton).
   AddInstances(
     x =>
     {
       x.ConstructedBy(() => CreateSessionFactory("MyName"))
         .WithName("MyName");
       x.ConstructedBy(() => CreateSessionFactory("My2ndName"))
         .WithName("My2ndName");
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文