Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / MVC / Castle.MVC / StatePersister / SessionPersister.cs
blob06a85699d19141116e3f159c614dfe5079fce107
1 #region Apache Notice
2 /*****************************************************************************
3 *
4 * Castle.MVC
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ********************************************************************************/
19 #endregion
21 #region Autors
23 /************************************************
24 * Gilles Bayon
25 *************************************************/
26 #endregion
28 #region Using
30 using System;
31 using System.Web;
32 using System.Collections;
34 using Castle.MVC.States;
35 #endregion
37 namespace Castle.MVC.StatePersister
39 /// <summary>
40 /// This class provides simple session-based state persister for Web applications.
41 /// </summary>
42 public class SessionPersister : IStatePersister
45 #region Fields
47 private IStateFactory _stateFactory = null;
49 #endregion
51 #region Constructors
52 /// <summary>
53 /// Constructor
54 /// </summary>
55 public SessionPersister()
57 #endregion
59 #region IStatePersister Members
61 /// <summary>
62 /// State factory
63 /// </summary>
64 public IStateFactory StateFactory
66 get{ return _stateFactory;}
67 set{ _stateFactory = value; }
70 /// <summary>
71 /// Saves state to the Session object.
72 /// </summary>
73 /// <param name="state">The state to save.</param>
74 public void Save(IState state)
76 // put State object directly into Session
77 HttpContext.Current.Session[ BaseState.SESSION_KEY ] = state;
80 /// <summary>
81 /// Loads the saved state.
82 /// </summary>
83 /// <returns>The saved state</returns>
84 public IState Load()
86 // pull State object directly out of Session
87 IState state = HttpContext.Current.Session[ BaseState.SESSION_KEY ] as IState;
88 if (state==null)
90 state = _stateFactory.Create();
91 HttpContext.Current.Session[ BaseState.SESSION_KEY ] = state;
93 state.Reset();
94 return state;
97 /// <summary>
98 /// Release a state
99 /// </summary>
100 /// <param name="state">The state to release.</param>
101 public void Release(IState state)
103 HttpContext.Current.Session.Remove( BaseState.SESSION_KEY );
104 _stateFactory.Release(state);
106 #endregion