1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 namespace Castle
.MicroKernel
.ComponentActivator
25 /// Attempts to dynamically load a UserControl by invoking Page.LoadControl.
26 /// There are two uses of this class.
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)
33 /// <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"
42 /// 2) Precompile a UserControl and add the pre-compiled class to the Kernel. (hard)
43 /// Has not been tested with proxies.
47 public class WebUserControlComponentActivator
: DefaultComponentActivator
50 /// Initializes a new instance of the <see cref="WebUserControlComponentActivator"/> class.
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
)
64 /// Creates the instance.
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;
81 createInstance
= Kernel
.ProxyFactory
.RequiresTargetInstance(Kernel
, Model
);
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
);
108 instance
= currentPage
.LoadControl(implType
, arguments
);
113 throw new ComponentActivatorException(
114 "WebUserControlComponentActivator: could not instantiate " + Model
.Implementation
.FullName
, ex
);
122 instance
= Kernel
.ProxyFactory
.Create(Kernel
, instance
, Model
, arguments
);
126 throw new ComponentActivatorException("ComponentActivator: could not proxy " + Model
.Implementation
.FullName
, ex
);