私のコードは以下の通りです。私の問題は、セッションが挿入されず、セッションが Null のままであることです。ここで何が問題なのですか?ninject 2.2を使用しています
public class SessionNinjectModule : NinjectModule
{
public override void Load()
{
this.Bind<ISession>().ToMethod(x => MvcApplication.SessionFactory.GetCurrentSession());
this.Bind(typeof(IRepository<>)).To(typeof(NHRepository<>)).InRequestScope();
}
}
public class MvcApplication : NinjectHttpApplication
{
public static ISessionFactory SessionFactory = CreateSessionFactory();
public MvcApplication()
{
this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
this.EndRequest += new EventHandler(MvcApplication_EndRequest);
}
void MvcApplication_EndRequest(object sender, EventArgs e)
{
CurrentSessionContext.Unbind(SessionFactory).Dispose();
}
void MvcApplication_BeginRequest(object sender, EventArgs e)
{
CurrentSessionContext.Bind(SessionFactory.OpenSession());
}
private static ISessionFactory CreateSessionFactory()
{
string connectionString = "Data Source=VARTAN-PC\\SQLEXPRESS;Initial Catalog=mdlivenh;Integrated Security=SSPI;";
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)).ExposeConfiguration(c => c.SetProperty("current_session_context_class", "thread_static"))
.Mappings(m =>
{
// Include both standard NHibernate mapping files and Fluent NHibernate mapping files
//m.HbmMappings.AddFromAssemblyOf<User>();
m.FluentMappings.AddFromAssemblyOf<NHMDLive.Models.Mappings.StudyMap>();
}).BuildSessionFactory();
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
/// <summary>
/// Creates the controllerfactory (I think)
/// </summary>
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public class NHRepository<T> : IQueryable<T>, IRepository<T> where T : class, IBaseEntity
{
private ISession _session;
[Inject]
public ISession session
{
get { return _session; }
set { _session = value; }
}
#region IRepository Members
virtual public T Get(object id)
{
return TryCatch(() => session.Get<T>(id));
}}
使用法: NHRepository リポジトリ = new NHRepository();