Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / ComponentActivator / WebUserControlComponentActivator.cs
blob1961aa12e7b43f07ffb7b0fbd24044674942f2fb
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 #if !MONO
17 namespace Castle.MicroKernel.ComponentActivator
19 using System;
20 using System.Web;
21 using System.Web.UI;
22 using Castle.Core;
24 /// <summary>
25 /// Attempts to dynamically load a UserControl by invoking Page.LoadControl.
26 /// There are two uses of this class.
27 /// <para>
28 /// 1) Add a component to the Kernel and add a VirtualPath attribute specifying
29 /// the relative path of the .ascx file for the associated UserControl. (easy)
30 /// </para>
31 /// <example>
32 /// <code>
33 /// &lt;component id="BasketView"
34 /// service="Castle.ShoppingCart.IBasketView, Castle.ShoppingCart"
35 /// type="Castle.ShoppingCart.BasketView, Castle.ShoppingCart"
36 /// lifestyle="transient"
37 /// virtualPath="~/Views/BasketView.ascx"
38 /// /&gt;
39 /// </code>
40 /// </example>
41 /// <para>
42 /// 2) Precompile a UserControl and add the pre-compiled class to the Kernel. (hard)
43 /// Has not been tested with proxies.
44 /// </para>
45 /// </summary>
46 [Serializable]
47 public class WebUserControlComponentActivator : DefaultComponentActivator
49 /// <summary>
50 /// Initializes a new instance of the <see cref="WebUserControlComponentActivator"/> class.
51 /// </summary>
52 /// <param name="model">The model.</param>
53 /// <param name="kernel">The kernel.</param>
54 /// <param name="onCreation">The on creation.</param>
55 /// <param name="onDestruction">The on destruction.</param>
56 public WebUserControlComponentActivator(ComponentModel model, IKernel kernel,
57 ComponentInstanceDelegate onCreation,
58 ComponentInstanceDelegate onDestruction)
59 : base(model, kernel, onCreation, onDestruction)
63 /// <summary>
64 /// Creates the instance.
65 /// </summary>
66 /// <param name="context">The context.</param>
67 /// <param name="arguments">The arguments.</param>
68 /// <param name="signature">The signature.</param>
69 /// <returns></returns>
70 protected override object CreateInstance(CreationContext context, object[] arguments, Type[] signature)
72 object instance = null;
74 Type implType = Model.Implementation;
76 bool createProxy = Model.Interceptors.HasInterceptors;
77 bool createInstance = true;
79 if (createProxy)
81 createInstance = Kernel.ProxyFactory.RequiresTargetInstance(Kernel, Model);
84 if (createInstance)
86 try
88 HttpContext currentContext = HttpContext.Current;
89 if (currentContext == null)
91 throw new InvalidOperationException(
92 "System.Web.HttpContext.Current is null. WebUserControlComponentActivator can only be used in an ASP.Net environment.");
95 Page currentPage = currentContext.Handler as Page;
96 if (currentPage == null)
98 throw new InvalidOperationException("System.Web.HttpContext.Current.Handler is not of type System.Web.UI.Page");
101 string virtualPath = Model.Configuration.Attributes["VirtualPath"];
102 if (!string.IsNullOrEmpty(virtualPath))
104 instance = currentPage.LoadControl(virtualPath);
106 else
108 instance = currentPage.LoadControl(implType, arguments);
111 catch(Exception ex)
113 throw new ComponentActivatorException(
114 "WebUserControlComponentActivator: could not instantiate " + Model.Implementation.FullName, ex);
118 if (createProxy)
122 instance = Kernel.ProxyFactory.Create(Kernel, instance, Model, arguments);
124 catch(Exception ex)
126 throw new ComponentActivatorException("ComponentActivator: could not proxy " + Model.Implementation.FullName, ex);
130 return instance;
135 #endif