我基本上使用了一个我在很多地方都见过的例子,但我不相信它工作正常。
所以,我有以下内容:
public class NHibernateHelper
{
private readonly string _connectionString;
private ISessionFactory _sessionFactory;
public ISessionFactory SessionFactory
{
get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); }
}
public NHibernateHelper(string connectionString)
{
_connectionString = connectionString;
}
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard
.Dialect("NHibernate.Spatial.Dialect.MySQLSpatialDialect,NHibernate.Spatial.MySQL")
.ConnectionString(_connectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<EventListing>())
.ExposeConfiguration(x =>
{
x.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, ConfigurationManager.AppSettings["current_session_context_class"]);
})
.BuildSessionFactory();
}
}
在我的 Ninject 启动中,我有
private static void RegisterServices(IKernel kernel)
{
NHibernateHelper helper = new NHibernateHelper(WebConfigurationManager.ConnectionStrings["eventlisting"].ConnectionString);
kernel.Bind<ISessionFactory>().ToConstant(helper.SessionFactory).InSingletonScope();
kernel.Bind<ISession>().ToProvider(new SessionProvider()).InRequestScope();
kernel.Bind(typeof(IReadOnlyRepository<>)).To(typeof(ReadOnlyRepository<>)).InRequestScope();
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
}
我的会话提供者类看起来像
public class SessionProvider : Provider<ISession>
{
protected override ISession CreateInstance(IContext context)
{
ISession session = DependencyResolver.Current.GetService<ISessionFactory>().OpenSession();
session.FlushMode = FlushMode.Auto;
return session;
}
}
令我困惑的是,尽管声明应该在请求范围内创建 ISession,但它似乎在每个请求中多次点击 CreateInstance 方法,并且看起来正在产生多个会话。
例如,如果我有一个带有这个构造函数的控制器:
public SomeController(
IRepository<SomeClass> someClassRepository,
IRepository<SomeOtherClass> someOtherClassRepository)
{
_someClassRepository = someClassRepository;
_someOtherRepository = someOtherClassRepository;
}
然后 CreateInstance 方法被调用两次(我应该添加存储库的构造函数需要一个 ISession 参数):
public Repository(ISession session)
{
this.Session = session;
}
我认为将它作为 InRequestScope 意味着它只被调用一次。是我误解还是我在某处遗漏了一些代码。