I want the Game object to be re-usable within a user "session", which means that if he leaves the site this instance won't be used again.
1 game/user.session
But in the current situation,it seems that each users will get the same game instance.
Users may be anonymous. So I guess I could use some aftificial session id, but I'm not sure if it's the right way.
(You may also suggest a better solution than using a try/catch there, because I'm sure there must be one, but as this works it is not a priority for now.)
For the future I suggest that you ask questions regarding Ninject on the Ninject mailinglist or Stackoverflow. The comunity is much more active there.
Now to your question. I strongly suggest that you think about a solution where you don't need session scoped objects.
The session scoped objects will be kept in memory for quite a long time (if I remember correctly the session timeout is 20 min). You can run into out of memory problems quite fast if it is not used correctly. Additionally the game will become a 2nd generation
gc object which means you will have a lot of 2nd generation garbage collections which dramatically increases the GC running time and the latency of your website at this time. Furthermore, it makes scaling to multiple server instances much more difficult. You
have to route users to the same server for the whole session. Also the user won't be able to start a new game during his session. You will have to reuse the previous instance.
If you still want to have session scoped object then you can do so by adding an object as session scope to HttpContext.Current.Session["SessionScope"] at sesson start
public void Session_Start()
{
HttpContext.Current.Session["SessionScope"] = new object();
}
I suggest to have a look at Ninject.MVC3 extension. It adds the integration to MVC so that you dont have to add this NinjectKernel singleton so that you can keep your application free from Ninject references.
thomas_msn01
Member
17 Points
43 Posts
Ninject and lifecycle ?
Apr 07, 2012 08:15 PM|LINK
I'm rather new with MVC3 and Ninject.
Right now here what I'm using:
This returns me Ninject Kernel:
using Ninject; namespace MasterMindDnet { public class NinjectKernel { private static IKernel _Instance = new StandardKernel(); public static IKernel getInstance() { return _Instance; } } }this returns me an instance of my object.
public static Game getNinjectInstance() { IGame iGame; try { iGame = NK.Get<IGame>(DefaultNinjectNewGameName); } catch { GameParameters ng = GameParameters.getNinjecInstance(); NK.Bind<IGame>().To<Game>().InSingletonScope().Named(DefaultNinjectNewGameName) .WithConstructorArgument("CodeLength", ng.CodeLength) .WithConstructorArgument("TurnsToBePlayed", ng.Turns) .WithConstructorArgument("DoubleColorAllowed", ng.DoubleColorAllowed) .WithConstructorArgument("GameColors", ng.GameColors) ; iGame = NK.Get<IGame>(DefaultNinjectNewGameName); } return (Game)iGame; }I want the Game object to be re-usable within a user "session", which means that if he leaves the site this instance won't be used again.
1 game/user.session
But in the current situation,it seems that each users will get the same game instance.
Users may be anonymous. So I guess I could use some aftificial session id, but I'm not sure if it's the right way.
(You may also suggest a better solution than using a try/catch there, because I'm sure there must be one, but as this works it is not a priority for now.)
Thank your for your help.
RemoGloor
Member
12 Points
1 Post
Re: Ninject and lifecycle ?
Apr 09, 2012 09:09 PM|LINK
Hi Thomas
For the future I suggest that you ask questions regarding Ninject on the Ninject mailinglist or Stackoverflow. The comunity is much more active there.
Now to your question. I strongly suggest that you think about a solution where you don't need session scoped objects.
The session scoped objects will be kept in memory for quite a long time (if I remember correctly the session timeout is 20 min). You can run into out of memory problems quite fast if it is not used correctly. Additionally the game will become a 2nd generation gc object which means you will have a lot of 2nd generation garbage collections which dramatically increases the GC running time and the latency of your website at this time. Furthermore, it makes scaling to multiple server instances much more difficult. You have to route users to the same server for the whole session. Also the user won't be able to start a new game during his session. You will have to reuse the previous instance.
If you still want to have session scoped object then you can do so by adding an object as session scope to HttpContext.Current.Session["SessionScope"] at sesson start
public void Session_Start() { HttpContext.Current.Session["SessionScope"] = new object(); }and later you can use it as session scope:
I suggest to have a look at Ninject.MVC3 extension. It adds the integration to MVC so that you dont have to add this NinjectKernel singleton so that you can keep your application free from Ninject references.
Remo