Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / AbstractViewComponentFactory.cs
blob7cc026a1ce44cd0cbf010fac71d0fd6b9d72840e
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 Castle.MonoRail.Framework.Services
17 using System;
18 using Castle.Core;
19 using Castle.Core.Logging;
20 using Castle.MonoRail.Framework.ViewComponents;
22 /// <summary>
23 /// Base implementation for <see cref="IViewComponentFactory"/>
24 /// </summary>
25 public abstract class AbstractViewComponentFactory : IInitializable, IServiceEnabledComponent, IViewComponentFactory
27 /// <summary>
28 /// The logger instance
29 /// </summary>
30 private ILogger logger = NullLogger.Instance;
32 /// <summary>
33 /// Initializes a new instance of the <see cref="AbstractViewComponentFactory"/> class.
34 /// </summary>
35 protected AbstractViewComponentFactory()
39 #region IInitializable implementation
41 /// <summary>
42 /// Invoked by the framework in order to initialize the state
43 /// </summary>
44 public virtual void Initialize()
46 AddBuiltInComponents();
49 #endregion
51 #region IServiceEnabledComponent implementation
53 /// <summary>
54 /// Invoked by the framework in order to give a chance to
55 /// obtain other services
56 /// </summary>
57 /// <param name="provider">The service proviver</param>
58 public virtual void Service(IServiceProvider provider)
60 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
62 if (loggerFactory != null)
64 logger = loggerFactory.Create(typeof(AbstractViewComponentFactory));
68 #endregion
70 /// <summary>
71 /// Creates an instance of the requested <see cref="ViewComponent"/>
72 /// </summary>
73 /// <param name="name">The view component's name</param>
74 /// <returns>The view component instance</returns>
75 public virtual ViewComponent Create(String name)
77 Type viewCompType = ResolveType(name);
79 try
81 return (ViewComponent) Activator.CreateInstance(viewCompType);
83 catch (Exception ex)
85 logger.Error("Could not create ViewComponent instance", ex);
87 throw;
91 /// <summary>
92 /// Gets the registry.
93 /// </summary>
94 /// <value>The registry.</value>
95 public IViewComponentRegistry Registry
97 get { return GetViewComponentRegistry(); }
100 /// <summary>
101 /// Resolves the type.
102 /// </summary>
103 /// <param name="name">The name.</param>
104 /// <returns></returns>
105 protected Type ResolveType(string name)
107 if (logger.IsDebugEnabled)
109 logger.DebugFormat("Creating view component '{0}'", name);
112 Type viewCompType = GetViewComponentRegistry().GetViewComponent(name);
114 return viewCompType;
117 /// <summary>
118 /// Releases a ViewComponent instance
119 /// </summary>
120 /// <remarks>
121 /// Not currently used
122 /// </remarks>
123 /// <param name="instance"></param>
124 public virtual void Release(ViewComponent instance)
126 if (logger.IsDebugEnabled)
128 logger.Debug("Releasing view component instance " + instance);
132 /// <summary>
133 /// Implementors should return a reference to
134 /// the current view engine.
135 /// </summary>
136 public abstract IViewEngine ViewEngine { get; set; }
138 /// <summary>
139 /// Registers viewcomponents provided by default.
140 /// <seealso cref="CaptureFor"/>
141 /// <seealso cref="SecurityComponent"/>
142 /// </summary>
143 protected virtual void AddBuiltInComponents()
145 RegisterComponent("CaptureFor", typeof(CaptureFor));
146 RegisterComponent("SecurityComponent", typeof(SecurityComponent));
147 RegisterComponent("ChildContentComponent", typeof(ChildContentComponent));
148 // RegisterComponent("UpdatePage", typeof(UpdatePage));
149 // RegisterComponent("UpdatePageTag", typeof(UpdatePageTag));
150 RegisterComponent("AuthenticatedContent", typeof(AuthenticatedContent));
151 RegisterComponent("DiggStylePagination", typeof(DiggStylePagination));
152 RegisterComponent("SelectStylePagination", typeof(SelectStylePagination));
153 RegisterComponent("ColumnRenderer", typeof(ColumnRenderer));
154 RegisterComponent("SiteMapComponent", typeof(SiteMapComponent));
155 RegisterComponent("TreeMakerComponent", typeof(TreeMakerComponent));
158 /// <summary>
159 /// Registers a view component type.
160 /// </summary>
161 /// <param name="name">The view components's name</param>
162 /// <param name="type">The view component's which must extend <see cref="ViewComponent"/></param>
163 protected void RegisterComponent(String name, Type type)
165 if (logger.IsDebugEnabled)
167 logger.DebugFormat("Registering ViewComponent {0} Type {1} ", name, type);
169 GetViewComponentRegistry().AddViewComponent(name, type);
172 /// <summary>
173 /// Gets the view component registry.
174 /// </summary>
175 /// <returns></returns>
176 protected virtual IViewComponentRegistry GetViewComponentRegistry()
178 throw new NotImplementedException();