Relaxed the need for Types that represent database boundaries to extend from ActiveRe...
[castle.git] / Samples / Castle / PetStore.Web / GlobalApplication.cs
blob204bdd8f3eb7444256649c9e76a173c3bdeb2907
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace PetStore.Web
17 using System;
18 using System.Web;
20 using Castle.ActiveRecord;
22 using Castle.Core.Resource;
24 using Castle.Windsor;
25 using Castle.Windsor.Configuration.Interpreters;
28 public class GlobalApplication : HttpApplication, IContainerAccessor
30 private static WindsorContainer container;
32 public GlobalApplication()
34 BeginRequest += new EventHandler(OnBeginRequest);
35 EndRequest += new EventHandler(OnEndRequest);
36 AuthenticateRequest += new EventHandler(OnAuthenticateRequest);
39 public void Application_OnStart()
41 container = new WindsorContainer( new XmlInterpreter(new ConfigResource()) );
44 public void Application_OnEnd()
46 container.Dispose();
49 public void OnAuthenticateRequest(object sender, EventArgs e)
51 HttpContext context = HttpContext.Current;
53 HttpCookie cookie = context.Request.Cookies.Get("usertoken");
55 if (cookie != null)
57 context.User = PetStore.Model.User.Find(Convert.ToInt32(cookie.Value));
61 public IWindsorContainer Container
63 get { return container; }
66 public void OnBeginRequest(object sender, EventArgs e)
68 HttpContext.Current.Items.Add( "nh.sessionscope", new SessionScope() );
71 public void OnEndRequest(object sender, EventArgs e)
73 SessionScope scope = (SessionScope) HttpContext.Current.Items["nh.sessionscope"];
75 try
77 if (scope != null) scope.Dispose();
79 catch(Exception ex)
81 HttpContext.Current.Trace.Warn( "Error", "Problems with the session:" + ex.Message, ex );