Welcome Guest, you are in: Login

Castle Project


Navigation (Windsor)

Search the wiki


This documentation is a work in progress.

Let us know if some information is not clear, inaccurate or missing. Also feel free to update the wiki yourself.

[MOVED] Windsor Tutorial - Part Seven - Lifestyles

Page discussion for [MOVED] Windsor Tutorial - Part Seven - Lifestyles
Newbie help2011/01/13 22:12 by Jonathan Channon
Sorry to contact you this way but didn't know where else to ask a question. I have been following your 7 part Windsor tutorial and am very new to IOC.

Can you answer me, is a ISession created on every web request or just when the application starts? I only have MVC 2 at the moment and have created a project where a controller receives a repository interface and the class that implements the irepository has a ISession passed to the constructor.

I am seeing that for each controller and repository the ISession seems to be the same. The only way I am concluding that is by looking at the ISessionFactory statisitic start time as they are all the same.

Just wondering what the expected behaviour is.

Again sorry if this is the wrong place to ask the question.

Re: Newbie help2011/01/13 22:25 by Krzysztof Koźmic
Scope of the instance's lifetime is determined by its Lifestyle. The PerWebRequest lifestyle will manage one instance for every web request that requires that. For questions about Windsor or castle project in general, there's a castle users group on Google Groups.

Re: Newbie help2011/01/13 22:34 by Jonathan Channon
The lifestyle is set to PerWebRequest as in your tutorial and I thought it would go through the PersistenceFacility Init method on every HTTP request but doesnt seem to.
Re: Newbie help2011/01/13 23:10 by Krzysztof Koźmic
I updated the tutorial to make it more explicit what's happening. in short:
  • Init is for facility initialisation, and is called once after you AddFacility to the container.
  • PerWebRequest is a lifestyle of the component, and it means that at most one instance will be created for any given web request in your app.

HTH, Krzysztof


Feel free to share any other feedback, and point to places in the tutorial that you find either confusing or not explaining something well, or clearly enough so that we can improve the tutorial. If there's something that you think should be covered in the tutorial but was not, feel free to suggest topics to cover in forthcoming parts of the tutorial as well.
Re: Newbie help2011/01/13 23:19 by Jonathan Channon

Thanks for the modification. However I am still unsure about the ISession creation. Does it mean that for every HTTP request an ISession is intialised? I did some SQL profiling and there only seems to be one connection. i.e. no more connections are being made no matter how many times I call an action on a controller even on multiple controllers using ISession. Could you explain whats going on and how to test it to see it in action?? Please :-)

The only other feedback I would suggest is maybe using Repositories rather than passing ISession to the Controller. I have created some repository interfaces, classes that implement the interface and in their constructors pass in a ISession and in the constructors of the controllers pass the repository interface. This seems to be a best of practice recommendation.
Re: Newbie help2011/01/13 23:38 by Jonathan Channon
Something may have just clicked.

When the application starts it calls OpenSession in the PersistanceFacility.

Because this is registered as PerWebRequest does that mean for each web request this session is then shared using ISession?
Re: Newbie help2011/01/13 23:41 by Krzysztof Koźmic
Two things:

  • The facility does not call OpenSession
  • As Jose explained on the group, there's no 1 - 1 relation between ISession instances and physical database connections.
Re: Newbie help2011/01/13 23:45 by Jonathan Channon
In the PersistanceFacility class there is an OpenSession method call.

Anyway does my other post make sense? Once its registered the ISession is then created or passed to each web request?
Re: Newbie help2011/01/14 05:24 by Krzysztof Koźmic
there's no call to OpenSession in the facility.

There's a delegate passed as a factory method that calls it. That's a difference as this delegate is not invoked within the Init method.

ScrewTurn Wiki version Some of the icons created by FamFamFam.