动态更改连接字符串时发出 Rebind NInject

发布于 01-04 03:10 字数 1120 浏览 2 评论 0原文

在连接字符串中,我应该通过使用连接字符串中的属性 application name=userid 动态传递logginguserid值,并使用查询select app_name()进入SQL Server。

使用的技术: 1).net 4.0 2)NHibernate 3)Ninject

在登录之前,我使用 Ninject IoC 容器和 NHibernate 加载没有应用程序名称属性的连接字符串,在登录之后,我将登录的用户 ID 作为构造函数值传递,并重新绑定 NhibernateConfiguration 类,如下

在登录之前 在注入 NHibernateConfiguration

public override void Load()
        {
            Bind<ISession>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>()
                                               .GetSessionFactory()
                                               .OpenSession());

            Bind<ISessionFactory>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>().GetSessionFactory());

        }

登录后,将logginguserid与构造函数参数一起传递,如下所示。

using (var kernal = ServiceLocator.GetKernel())
{
 kernal.Rebind<NHibernateConfiguration>().To<NHibernateConfiguration>()
.WithConstructorArgument("loggedUserId", user.Id);
}

但我无法重新绑定或注入 NHibernateConfiguration 类。

请帮助我如何使用 Ninject 重新绑定 NHibernateConfiguration 类

In connection string i should dynamically passing the loggeduserid value by using the attribute application name=userid in connection string and getting into SQL Server by using the query select app_name().

Technologies used:
1).net 4.0
2)NHibernate
3)Ninject

Before logged-in i am using Ninject IoC container and NHibernate to load the connection string without the application name attribute and after logged in I am passing the logged user id as a constructor value and rebinding the NhibernateConfiguration class is as follows

Before Logged in inject the NHibernateConfiguration

public override void Load()
        {
            Bind<ISession>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>()
                                               .GetSessionFactory()
                                               .OpenSession());

            Bind<ISessionFactory>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>().GetSessionFactory());

        }

After logged in passing the loggeduserid with the constructor argument is as follows.

using (var kernal = ServiceLocator.GetKernel())
{
 kernal.Rebind<NHibernateConfiguration>().To<NHibernateConfiguration>()
.WithConstructorArgument("loggedUserId", user.Id);
}

but I am not able rebind or inject the NHibernateConfiguration class .

Please help me how to rebind the NHibernateConfiguration class by using Ninject

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

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

发布评论

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

评论(1

许一世地老天荒2025-01-11 03:10:29

不要在应用程序执行期间使用重新绑定。这可能会导致很多问题。使用条件出价代替:

kernel.Bind<NHibernateConfiguration>().To<NHibernateConfiguration>();
kernel.Bind<NHibernateConfiguration>().To<NHibernateConfiguration>()
      .When(ctx => IsLoggedIn())
      .WithConstructorArgument("loggedUserId", request => user.Id);

private bool IsLoggedIn()
{
     // add code to decide if the user is logged in
}

此外,您的会话绑定没有多大意义。它应该改为获取会话工厂。

Bind<ISession>().ToMethod(x => x.Kernel.Get<ISessionFactory>()
                .OpenSession());

Don't use rebind during the execution of your application. This can cause many problems. Use conditional bidnings instead:

kernel.Bind<NHibernateConfiguration>().To<NHibernateConfiguration>();
kernel.Bind<NHibernateConfiguration>().To<NHibernateConfiguration>()
      .When(ctx => IsLoggedIn())
      .WithConstructorArgument("loggedUserId", request => user.Id);

private bool IsLoggedIn()
{
     // add code to decide if the user is logged in
}

Also your session binding doesn't make much sense. It should get the session factory instead.

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