gpt4 book ai didi

nhibernate - NHibernate ISession 与 CaSTLe Windsor IoC 的循环依赖

转载 作者:行者123 更新时间:2023-12-04 02:48:27 24 4
gpt4 key购买 nike

我在 ASP.NET MVC 应用程序中使用 CaSTLe Windsor 作为我的 IoC 和 NHIbernate。它工作得很好,注册如下(有一个异常(exception)):

container.Register(Component.For<ISessionFactoryBuilder.().ImplementedBy<SessionFactoryBuilder>().LifestyleSingleton());

// Register the NHibernate session factory as a singleton using custom SessionFactoryBuilder.BuildSessionFactory method.
container.Register(Component.For<ISessionFactory>().UsingFactoryMethod(k => k.Resolve<ISessionFactoryBuilder>().BuildSessionFactory("ApplicationServices")).LifestyleSingleton());

container.Register(Component.For<IInterceptor>().ImplementedBy<ChangeAuditInfoInterceptor>().LifestylePerWebRequest());
container.Register(Component.For<ISession>().UsingFactoryMethod(k => k.Resolve<ISessionFactory>()
.OpenSession(container.Resolve<IInterceptor>())).LifestylePerWebRequest());

一切都很好,除了我的 ChangeAuditInterceptor 依次注入(inject)了 IAccountSession 服务,而 IAccountSession 服务又注入(inject)了 NHibernate ISession...这会导致以下循环依赖异常:

Dependency cycle has been detected when trying to resolve component 'Late bound NHibernate.ISession'. The resolution tree that resulted in the cycle is the following: Component 'Late bound NHibernate.ISession' resolved as dependency of component 'Blah.Core.Services.AccountSession' resolved as dependency of component 'Blah.Core.Infrastructure.Data.ChangeAuditInfoInterceptor' resolved as dependency of component 'Blah.Core.Infrastructure.Installers.SessionFactoryBuilder' resolved as dependency of component 'Late bound NHibernate.ISessionFactory' resolved as dependency of component 'Late bound NHibernate.ISession' which is the root component being resolved.

在过去的几年里,我通常使用 NHibernateSessionManager 运行,它负责在 IInterceptor 中进行插入,而不会导致这种循环依赖问题(与使用 CaSTLe Windsor 的 UsingFactoryMethod 功能的 SessionFactoryBuilder 的用法相反)。

对于如何解决这种循环依赖有什么建议吗?没有开始通过其他方式侵入 AccountSession 的 ISession(即绕过问题并因此产生异味的属性注入(inject))。我已将 ISession 注入(inject)切换为 AccountSession 服务的属性注入(inject),它工作正常,但我不喜欢隐式契约(Contract)与构造函数显式契约(Contract)。

public class AccountSession : IAccountSession
{
private readonly ISession _session;

public AccountSession(ISession session)
{
_session = session;
}

public Account GetCurrentAccount() // Called by a method in ChangeAuditInterceptor
{
...
}

...等等

最佳答案

尝试在拦截器类中添加对 Func< ISession > 的依赖

public class CustomInterceptor : EmptyInterceptor
{
private readonly Func<ISession> sessionFunc;
private ISession session;

protected ISession Session
{
get
{
return session ?? (session = sessionFunc());
}
}

public CustomInterceptor(Func<ISession> sessionFunc)
{
this.sessionFunc = sessionFunc;
}
}

和注册:

container.Register(Component.For<ISession>().
LifestylePerWebRequest()
.UsingFactoryMethod(container =>
{
var interceptor = container.Resolve<IInterceptor>();
return container.Resolve<ISessionFactory>.OpenSession(interceptor);
}));
container.Register(Component.For<Func<ISession>>()
.LifestylePerWebRequest()
.UsingFactoryMethod(container =>
{
Func<ISession> func = container.Resolve<ISession>;
return func;
}));

关于nhibernate - NHibernate ISession 与 CaSTLe Windsor IoC 的循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18317900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com