Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Container / DefaultMonoRailContainer.cs
blobf088ede2f294f68831ea5ff71a147b7cf47950cf
1 // Copyright 2004-2008 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.Container
17 using System;
18 using System.Configuration;
19 using Castle.Components.Common.EmailSender;
20 using Castle.Components.Validator;
21 using Castle.Core.Configuration;
22 using Castle.MonoRail.Framework.Services;
23 using Castle.MonoRail.Framework.Configuration;
24 using Castle.MonoRail.Framework.Providers;
25 using Castle.MonoRail.Framework.Resources;
26 using Castle.MonoRail.Framework.Services.AjaxProxyGenerator;
28 /// <summary>
29 /// Pendent
30 /// </summary>
31 public class DefaultMonoRailContainer : AbstractServiceContainer, IMonoRailContainer
33 #region ServiceIdentification enum
35 /// <summary>
36 /// Enum for all known MonoRail services.
37 /// </summary>
38 public enum ServiceIdentification
40 /// <summary>
41 /// Default value
42 /// </summary>
43 Undefined,
44 /// <summary>
45 /// Custom ( not know service )
46 /// </summary>
47 Custom,
48 /// <summary>
49 /// The <see cref="IControllerFactory"/> service
50 /// </summary>
51 ControllerFactory,
52 /// <summary>
53 /// The <see cref="IControllerContextFactory"/> service
54 /// </summary>
55 ControllerContextFactory,
56 /// <summary>
57 /// The <see cref="IUrlBuilder"/> service
58 /// </summary>
59 UrlBuilder,
60 /// <summary>
61 /// The <see cref="IUrlTokenizer"/> service
62 /// </summary>
63 UrlTokenizer,
64 /// <summary>
65 /// The <see cref="IServerUtility"/> service
66 /// </summary>
67 ServerUtility,
68 /// <summary>
69 /// The <see cref="IControllerTree"/> service
70 /// </summary>
71 ControllerTree,
72 /// <summary>
73 /// The <see cref="ICacheProvider"/> service
74 /// </summary>
75 CacheProvider,
76 /// <summary>
77 /// The <see cref="IViewSourceLoader"/> service.
78 /// </summary>
79 ViewSourceLoader,
80 /// <summary>
81 /// The <see cref="IFilterFactory"/> service.
82 /// </summary>
83 FilterFactory,
84 /// <summary>
85 /// The <see cref="IViewComponentFactory"/> service
86 /// </summary>
87 ViewComponentFactory,
88 /// <summary>
89 /// The <see cref="IEmailSender"/> service.
90 /// </summary>
91 EmailSender,
92 /// <summary>
93 /// The <see cref="IControllerDescriptorProvider"/> service
94 /// </summary>
95 ControllerDescriptorProvider,
96 /// <summary>
97 /// The <see cref="IResourceDescriptorProvider"/> service
98 /// </summary>
99 ResourceDescriptorProvider,
100 /// <summary>
101 /// The <see cref="IViewComponentDescriptorProvider"/> service
102 /// </summary>
103 ViewComponentDescriptorProvider,
104 /// <summary>
105 /// The <see cref="IRescueDescriptorProvider"/> service
106 /// </summary>
107 RescueDescriptorProvider,
108 /// <summary>
109 /// The <see cref="ILayoutDescriptorProvider"/> service
110 /// </summary>
111 LayoutDescriptorProvider,
112 /// <summary>
113 /// The <see cref="IHelperDescriptorProvider"/> service
114 /// </summary>
115 HelperDescriptorProvider,
116 /// <summary>
117 /// The <see cref="IFilterDescriptorProvider"/> service
118 /// </summary>
119 FilterDescriptorProvider,
120 /// <summary>
121 /// The <see cref="IReturnBinderDescriptorProvider"/> service
122 /// </summary>
123 ReturnBinderDescriptorProvider,
124 /// <summary>
125 /// The <see cref="IResourceFactory"/> service
126 /// </summary>
127 ResourceFactory,
128 /// <summary>
129 /// The <see cref="IEmailTemplateService"/> service
130 /// </summary>
131 EmailTemplateService,
132 /// <summary>
133 /// The <see cref="IScaffoldingSupport"/> service
134 /// </summary>
135 ScaffoldingSupport,
136 /// <summary>
137 /// The <see cref="ITransformFilterDescriptorProvider"/> service
138 /// </summary>
139 TransformFilterDescriptorProvider,
140 // /// <summary>
141 // /// The <see cref="ITransformFilterFactory"/> service
142 // /// </summary>
143 // TransformationFilterFactory,
144 /// <summary>
145 /// The <see cref="IViewEngineManager"/> service
146 /// </summary>
147 ViewEngineManager,
148 /// <summary>
149 /// The <see cref="IValidatorRegistry"/> service
150 /// </summary>
151 ValidatorRegistry,
152 /// <summary>
153 /// The <see cref="IAjaxProxyGenerator"/> service
154 /// </summary>
155 AjaxProxyGenerator
158 #endregion
160 private IUrlTokenizer urlTokenizerCached;
161 private IUrlBuilder urlBuilderCached;
162 private ICacheProvider cacheProviderCached;
163 private IEngineContextFactory engContextFactoryCached;
164 private IControllerFactory controllerFactoryCached;
165 private IControllerContextFactory controllerContextFactoryCached;
166 private IControllerTree controllerTreeCached;
167 private IViewSourceLoader viewSourceLoaderCached;
168 private IFilterFactory filterFactoryCached;
169 private IControllerDescriptorProvider controllerDescriptorProviderCached;
170 private IViewEngineManager viewEngineManagerCached;
171 private IValidatorRegistry validatorRegistryCached;
172 private IActionSelector actionSelectorCached;
173 private IScaffoldingSupport scaffoldSupportCached;
174 private IJSONSerializer jsonSerializerCached;
175 private IStaticResourceRegistry staticResourceRegCached;
176 private IEmailTemplateService emailTemplateServiceCached;
177 private IEmailSender emailSenderCached;
178 private IResourceFactory resourceFactoryCached;
179 private IHelperFactory helperFactoryCached;
180 private IServiceInitializer serviceInitializerCached;
181 private ExtensionManager extensionManager;
183 /// <summary>
184 /// Initializes a new instance of the <see cref="DefaultMonoRailContainer"/> class.
185 /// </summary>
186 /// <param name="parentContainer">The parent container.</param>
187 public DefaultMonoRailContainer(IServiceProvider parentContainer) : base(parentContainer)
191 /// <summary>
192 /// Initializes a new instance of the <see cref="DefaultMonoRailContainer"/> class.
193 /// </summary>
194 public DefaultMonoRailContainer()
198 /// <summary>
199 /// Pendent
200 /// </summary>
201 public void UseServicesFromParent()
203 if (Parent == null)
205 return;
208 IServiceInitializer serviceInitializer = (IServiceInitializer) Parent.GetService(typeof(IServiceInitializer));
209 if (serviceInitializer != null)
211 AddService(typeof(IServiceInitializer), serviceInitializer);
214 IHelperFactory helperFactory = (IHelperFactory) Parent.GetService(typeof(IHelperFactory));
215 if (helperFactory != null)
217 AddService(typeof(IHelperFactory), helperFactory);
220 IUrlTokenizer urlTokenizer = (IUrlTokenizer) Parent.GetService(typeof(IUrlTokenizer));
221 if (urlTokenizer != null)
223 AddService(typeof(IUrlTokenizer), urlTokenizer);
226 IUrlBuilder urlBuilder = (IUrlBuilder) Parent.GetService(typeof(IUrlBuilder));
227 if (urlBuilder != null)
229 AddService(typeof(IUrlBuilder), urlBuilder);
232 ICacheProvider cacheProvider = (ICacheProvider) Parent.GetService(typeof(ICacheProvider));
233 if (cacheProvider != null)
235 AddService(typeof(ICacheProvider), cacheProvider);
238 IEngineContextFactory engContextFactory = (IEngineContextFactory) Parent.GetService(typeof(IEngineContextFactory));
239 if (engContextFactory != null)
241 AddService(typeof(IEngineContextFactory), engContextFactory);
244 IControllerFactory controllerFactory = (IControllerFactory) Parent.GetService(typeof(IControllerFactory));
245 if (controllerFactory != null)
247 AddService(typeof(IControllerFactory), controllerFactory);
250 IControllerContextFactory controllerCtxFactory =
251 (IControllerContextFactory) Parent.GetService(typeof(IControllerContextFactory));
252 if (controllerCtxFactory != null)
254 AddService(typeof(IControllerContextFactory), controllerCtxFactory);
257 IControllerTree controllerTree = (IControllerTree) Parent.GetService(typeof(IControllerTree));
258 if (controllerTree != null)
260 AddService(typeof(IControllerTree), controllerTree);
263 IViewSourceLoader viewSourceLoader = (IViewSourceLoader) Parent.GetService(typeof(IViewSourceLoader));
264 if (viewSourceLoader != null)
266 AddService(typeof(IViewSourceLoader), viewSourceLoader);
269 IFilterFactory filterFactory = (IFilterFactory) Parent.GetService(typeof(IFilterFactory));
270 if (filterFactory != null)
272 AddService(typeof(IFilterFactory), filterFactory);
275 IControllerDescriptorProvider controllerDescriptorProvider =
276 (IControllerDescriptorProvider) Parent.GetService(typeof(IControllerDescriptorProvider));
277 if (controllerDescriptorProvider != null)
279 AddService(typeof(IControllerDescriptorProvider), controllerDescriptorProvider);
282 ITransformFilterDescriptorProvider transformFilterDescriptorProvider =
283 (ITransformFilterDescriptorProvider) Parent.GetService(typeof(ITransformFilterDescriptorProvider));
284 if (transformFilterDescriptorProvider != null)
286 AddService(typeof(ITransformFilterDescriptorProvider), transformFilterDescriptorProvider);
289 IReturnBinderDescriptorProvider returnBinderDescriptorProvider =
290 (IReturnBinderDescriptorProvider) Parent.GetService(typeof(IReturnBinderDescriptorProvider));
291 if (returnBinderDescriptorProvider != null)
293 AddService(typeof(IReturnBinderDescriptorProvider), returnBinderDescriptorProvider);
296 IViewEngineManager viewEngManager = (IViewEngineManager) Parent.GetService(typeof(IViewEngineManager));
297 if (viewEngManager != null)
299 AddService(typeof(IViewEngineManager), viewEngManager);
302 IViewComponentFactory viewComponentFactory = (IViewComponentFactory) Parent.GetService(typeof(IViewComponentFactory));
303 if (viewComponentFactory != null)
305 AddService(typeof(IViewComponentFactory), viewComponentFactory);
308 IViewComponentRegistry viewComponentRegistry = (IViewComponentRegistry) Parent.GetService(typeof(IViewComponentRegistry));
309 if (viewComponentRegistry != null)
311 AddService(typeof(IViewComponentRegistry), viewComponentRegistry);
314 IValidatorRegistry validatorRegistry = (IValidatorRegistry) Parent.GetService(typeof(IValidatorRegistry));
315 if (validatorRegistry != null)
317 AddService(typeof(IValidatorRegistry), validatorRegistry);
320 IJSONSerializer jsonSerializer = (IJSONSerializer) Parent.GetService(typeof(IJSONSerializer));
321 if (jsonSerializer != null)
323 AddService(typeof(IJSONSerializer), jsonSerializer);
326 IStaticResourceRegistry staticResourceRegistry =
327 (IStaticResourceRegistry) Parent.GetService(typeof(IStaticResourceRegistry));
328 if (staticResourceRegistry != null)
330 AddService(typeof(IStaticResourceRegistry), staticResourceRegistry);
333 IEmailTemplateService emailTemplateService =
334 (IEmailTemplateService) Parent.GetService(typeof(IEmailTemplateService));
335 if (emailTemplateService != null)
337 AddService(typeof(IEmailTemplateService), emailTemplateService);
340 IEmailSender emailSender =
341 (IEmailSender) Parent.GetService(typeof(IEmailSender));
342 if (emailSender != null)
344 AddService(typeof(IEmailSender), emailSender);
347 IResourceFactory resourceFactory =
348 (IResourceFactory) Parent.GetService(typeof(IResourceFactory));
349 if (resourceFactory != null)
351 AddService(typeof(IResourceFactory), resourceFactory);
355 /// <summary>
356 /// Installs the primordial services.
357 /// </summary>
358 public void InstallPrimordialServices()
360 if (!HasService<IServiceInitializer>())
362 AddService<IServiceInitializer>(new DefaultServiceInitializer());
366 /// <summary>
367 /// Pendent
368 /// </summary>
369 public void InstallMissingServices()
371 if (!HasService<IHelperFactory>())
373 AddService<IHelperFactory>(new DefaultHelperFactory());
375 if (!HasService<IControllerTree>())
377 AddService<IControllerTree>(CreateService<DefaultControllerTree>());
379 if (!HasService<IUrlTokenizer>())
381 AddService<IUrlTokenizer>(CreateService<DefaultUrlTokenizer>());
383 if (!HasService<IUrlBuilder>())
385 AddService<IUrlBuilder>(CreateService<DefaultUrlBuilder>());
387 if (!HasService<ICacheProvider>())
389 AddService<ICacheProvider>(CreateService<DefaultCacheProvider>());
391 if (!HasService<IEngineContextFactory>())
393 AddService<IEngineContextFactory>(CreateService<DefaultEngineContextFactory>());
395 if (!HasService<IControllerContextFactory>())
397 AddService<IControllerContextFactory>(CreateService<DefaultControllerContextFactory>());
399 if (!HasService<IControllerFactory>())
401 AddService<IControllerFactory>(CreateService<DefaultControllerFactory>());
403 if (!HasService<IViewSourceLoader>())
405 AddService<IViewSourceLoader>(CreateService<FileAssemblyViewSourceLoader>());
407 if (!HasService<IFilterFactory>())
409 AddService<IFilterFactory>(CreateService<DefaultFilterFactory>());
411 if (!HasService<IResourceDescriptorProvider>())
413 AddService<IResourceDescriptorProvider>(CreateService<DefaultResourceDescriptorProvider>());
415 if (!HasService<IRescueDescriptorProvider>())
417 AddService<IRescueDescriptorProvider>(CreateService<DefaultRescueDescriptorProvider>());
419 if (!HasService<IResourceFactory>())
421 AddService<IResourceFactory>(CreateService<DefaultResourceFactory>());
423 if (!HasService<IFilterDescriptorProvider>())
425 AddService<IFilterDescriptorProvider>(CreateService<DefaultFilterDescriptorProvider>());
427 if (!HasService<IHelperDescriptorProvider>())
429 AddService<IHelperDescriptorProvider>(CreateService<DefaultHelperDescriptorProvider>());
431 if (!HasService<ILayoutDescriptorProvider>())
433 AddService<ILayoutDescriptorProvider>(CreateService<DefaultLayoutDescriptorProvider>());
435 if (!HasService<ITransformFilterDescriptorProvider>())
437 AddService<ITransformFilterDescriptorProvider>(CreateService<DefaultTransformFilterDescriptorProvider>());
439 if (!HasService<IReturnBinderDescriptorProvider>())
441 AddService<IReturnBinderDescriptorProvider>(CreateService<DefaultReturnBinderDescriptorProvider>());
443 if (!HasService<IControllerDescriptorProvider>())
445 AddService<IControllerDescriptorProvider>(CreateService<DefaultControllerDescriptorProvider>());
447 if (!HasService<IViewEngineManager>())
449 AddService<IViewEngineManager>(CreateService<DefaultViewEngineManager>());
451 if (!HasService<IValidatorRegistry>())
453 AddService<IValidatorRegistry>(CreateService<CachedValidationRegistry>());
455 if (!HasService<IActionSelector>())
457 AddService<IActionSelector>(CreateService<DefaultActionSelector>());
459 if (!HasService<IJSONSerializer>())
461 AddService<IJSONSerializer>(CreateService<NewtonsoftJSONSerializer>());
463 if (!HasService<IStaticResourceRegistry>())
465 AddService<IStaticResourceRegistry>(CreateService<DefaultStaticResourceRegistry>());
467 if (!HasService<IViewComponentRegistry>())
469 AddService<IViewComponentRegistry>(CreateService<DefaultViewComponentRegistry>());
471 if (!HasService<IViewComponentFactory>())
473 AddService<IViewComponentFactory>(CreateService<DefaultViewComponentFactory>());
475 if (!HasService<IViewComponentDescriptorProvider>())
477 AddService<IViewComponentDescriptorProvider>(CreateService<DefaultViewComponentDescriptorProvider>());
479 if (!HasService<IEmailTemplateService>())
481 AddService<IEmailTemplateService>(CreateService<EmailTemplateService>());
483 if (!HasService<IEmailSender>())
485 AddService<IEmailSender>(CreateService<MonoRailSmtpSender>());
487 if (!HasService<IAjaxProxyGenerator>())
489 AddService<IAjaxProxyGenerator>(CreateService<PrototypeAjaxProxyGenerator>());
493 /// <summary>
494 /// Initialize extensions and start the extension manager.
495 /// </summary>
496 public void StartExtensionManager()
498 extensionManager = new ExtensionManager(this);
500 AddService(typeof(ExtensionManager), extensionManager);
502 IMonoRailConfiguration config = GetService<IMonoRailConfiguration>();
504 foreach(ExtensionEntry entry in config.ExtensionEntries)
506 AssertImplementsService(typeof(IMonoRailExtension), entry.ExtensionType);
508 IMonoRailExtension extension = (IMonoRailExtension) CreateService(entry.ExtensionType);
510 extension.SetExtensionConfigNode(entry.ExtensionNode);
512 extensionManager.Extensions.Add(extension);
516 /// <summary>
517 /// Configures the specified config.
518 /// </summary>
519 /// <param name="config">The config.</param>
520 public void Configure(IMonoRailConfiguration config)
522 if (config == null) throw new ArgumentNullException("config");
524 AddService<IMonoRailConfiguration>(config);
526 if (config.ServicesConfig != null)
528 foreach(IConfiguration serviceConfig in config.ServicesConfig.Children)
530 RegisterServiceOverrideFromConfigurationNode(serviceConfig);
534 if (!HasService<IScaffoldingSupport>() && config.ScaffoldConfig.ScaffoldImplType != null)
536 AddService<IScaffoldingSupport>(Activator.CreateInstance(config.ScaffoldConfig.ScaffoldImplType));
540 /// <summary>
541 /// Creates the specified service running all life cycles for it.
542 /// </summary>
543 /// <param name="type">The service type.</param>
544 /// <returns></returns>
545 protected override object CreateService(Type type)
547 if (type == null)
549 throw new ArgumentNullException("type");
552 object instance;
556 instance = Activator.CreateInstance(type);
558 catch(Exception ex)
560 throw new MonoRailException("Error trying to instantiate service " + type.FullName, ex);
565 ServiceInitializer.Initialize(instance, this);
567 catch(Exception ex)
569 throw new MonoRailException("Error running lifecycle steps for MonoRail service " + type.FullName, ex);
572 return instance;
575 #region Services
577 /// <summary>
578 /// Gets or sets the URL tokenizer.
579 /// </summary>
580 /// <value>The URL tokenizer.</value>
581 public IUrlTokenizer UrlTokenizer
585 if (urlTokenizerCached == null)
587 urlTokenizerCached = GetService<IUrlTokenizer>();
589 return urlTokenizerCached;
591 set { urlTokenizerCached = value; }
594 /// <summary>
595 /// Gets or sets the URL builder.
596 /// </summary>
597 /// <value>The URL builder.</value>
598 public IUrlBuilder UrlBuilder
602 if (urlBuilderCached == null)
604 urlBuilderCached = GetService<IUrlBuilder>();
606 return urlBuilderCached;
608 set { urlBuilderCached = value; }
611 /// <summary>
612 /// Gets or sets the cache provider.
613 /// </summary>
614 /// <value>The cache provider.</value>
615 public ICacheProvider CacheProvider
619 if (cacheProviderCached == null)
621 cacheProviderCached = GetService<ICacheProvider>();
623 return cacheProviderCached;
625 set { cacheProviderCached = value; }
628 /// <summary>
629 /// Gets or sets the engine context factory.
630 /// </summary>
631 /// <value>The engine context factory.</value>
632 public IEngineContextFactory EngineContextFactory
636 if (engContextFactoryCached == null)
638 engContextFactoryCached = GetService<IEngineContextFactory>();
640 return engContextFactoryCached;
642 set { engContextFactoryCached = value; }
645 /// <summary>
646 /// Gets or sets the controller factory.
647 /// </summary>
648 /// <value>The controller factory.</value>
649 public IControllerFactory ControllerFactory
653 if (controllerFactoryCached == null)
655 controllerFactoryCached = GetService<IControllerFactory>();
657 return controllerFactoryCached;
659 set { controllerFactoryCached = value; }
662 /// <summary>
663 /// Gets or sets the controller context factory.
664 /// </summary>
665 /// <value>The controller context factory.</value>
666 public IControllerContextFactory ControllerContextFactory
670 if (controllerContextFactoryCached == null)
672 controllerContextFactoryCached = GetService<IControllerContextFactory>();
674 return controllerContextFactoryCached;
676 set { controllerContextFactoryCached = value; }
679 /// <summary>
680 /// Gets or sets the controller tree.
681 /// </summary>
682 /// <value>The controller tree.</value>
683 public IControllerTree ControllerTree
687 if (controllerTreeCached == null)
689 controllerTreeCached = GetService<IControllerTree>();
691 return controllerTreeCached;
693 set { controllerTreeCached = value; }
696 /// <summary>
697 /// Gets or sets the view source loader.
698 /// </summary>
699 /// <value>The view source loader.</value>
700 public IViewSourceLoader ViewSourceLoader
704 if (viewSourceLoaderCached == null)
706 viewSourceLoaderCached = GetService<IViewSourceLoader>();
708 return viewSourceLoaderCached;
710 set { viewSourceLoaderCached = value; }
713 /// <summary>
714 /// Gets or sets the filter factory.
715 /// </summary>
716 /// <value>The filter factory.</value>
717 public IFilterFactory FilterFactory
721 if (filterFactoryCached == null)
723 filterFactoryCached = GetService<IFilterFactory>();
725 return filterFactoryCached;
727 set { filterFactoryCached = value; }
730 /// <summary>
731 /// Gets or sets the controller descriptor provider.
732 /// </summary>
733 /// <value>The controller descriptor provider.</value>
734 public IControllerDescriptorProvider ControllerDescriptorProvider
738 if (controllerDescriptorProviderCached == null)
740 controllerDescriptorProviderCached = GetService<IControllerDescriptorProvider>();
742 return controllerDescriptorProviderCached;
744 set { controllerDescriptorProviderCached = value; }
747 /// <summary>
748 /// Gets or sets the view engine manager.
749 /// </summary>
750 /// <value>The view engine manager.</value>
751 public IViewEngineManager ViewEngineManager
755 if (viewEngineManagerCached == null)
757 viewEngineManagerCached = GetService<IViewEngineManager>();
759 return viewEngineManagerCached;
761 set { viewEngineManagerCached = value; }
764 /// <summary>
765 /// Gets or sets the validator registry.
766 /// </summary>
767 /// <value>The validator registry.</value>
768 public IValidatorRegistry ValidatorRegistry
772 if (validatorRegistryCached == null)
774 validatorRegistryCached = GetService<IValidatorRegistry>();
776 return validatorRegistryCached;
778 set { validatorRegistryCached = value; }
781 /// <summary>
782 /// Gets or sets the action selector.
783 /// </summary>
784 /// <value>The action selector.</value>
785 public IActionSelector ActionSelector
789 if (actionSelectorCached == null)
791 actionSelectorCached = GetService<IActionSelector>();
793 return actionSelectorCached;
795 set { actionSelectorCached = value; }
798 /// <summary>
799 /// Gets or sets the scaffold support.
800 /// </summary>
801 /// <value>The scaffold support.</value>
802 public IScaffoldingSupport ScaffoldSupport
806 if (scaffoldSupportCached == null)
808 scaffoldSupportCached = GetService<IScaffoldingSupport>();
810 return scaffoldSupportCached;
812 set { scaffoldSupportCached = value; }
815 /// <summary>
816 /// Gets or sets the JSON serializer.
817 /// </summary>
818 /// <value>The JSON serializer.</value>
819 public IJSONSerializer JSONSerializer
823 if (jsonSerializerCached == null)
825 jsonSerializerCached = GetService<IJSONSerializer>();
827 return jsonSerializerCached;
829 set { jsonSerializerCached = value; }
832 /// <summary>
833 /// Gets or sets the static resource registry service.
834 /// </summary>
835 /// <value>The static resource registry.</value>
836 public IStaticResourceRegistry StaticResourceRegistry
840 if (staticResourceRegCached == null)
842 staticResourceRegCached = GetService<IStaticResourceRegistry>();
844 return staticResourceRegCached;
846 set { staticResourceRegCached = value; }
849 /// <summary>
850 /// Gets or sets the email template service.
851 /// </summary>
852 /// <value>The email template service.</value>
853 public IEmailTemplateService EmailTemplateService
857 if (emailTemplateServiceCached == null)
859 emailTemplateServiceCached = GetService<IEmailTemplateService>();
861 return emailTemplateServiceCached;
863 set { emailTemplateServiceCached = value; }
866 /// <summary>
867 /// Gets or sets the email sender.
868 /// </summary>
869 /// <value>The email sender.</value>
870 public IEmailSender EmailSender
874 if (emailSenderCached == null)
876 emailSenderCached = GetService<IEmailSender>();
878 return emailSenderCached;
880 set { emailSenderCached = value; }
883 /// <summary>
884 /// Gets or sets the extension manager.
885 /// </summary>
886 /// <value>The extension manager.</value>
887 public ExtensionManager ExtensionManager
889 get { return extensionManager; }
890 set { extensionManager = value; }
893 /// <summary>
894 /// Gets or sets the resource factory.
895 /// </summary>
896 /// <value>The resource factory.</value>
897 public IResourceFactory ResourceFactory
901 if (resourceFactoryCached == null)
903 resourceFactoryCached = GetService<IResourceFactory>();
905 return resourceFactoryCached;
907 set { resourceFactoryCached = value; }
910 /// <summary>
911 /// Gets or sets the service initializer.
912 /// </summary>
913 /// <value>The service initializer.</value>
914 public IServiceInitializer ServiceInitializer
918 if (serviceInitializerCached == null)
920 serviceInitializerCached = GetService<IServiceInitializer>();
922 return serviceInitializerCached;
924 set { serviceInitializerCached = value; }
927 /// <summary>
928 /// Gets or sets the helper factory.
929 /// </summary>
930 /// <value>The helper factory.</value>
931 public IHelperFactory HelperFactory
935 if (helperFactoryCached == null)
937 helperFactoryCached = GetService<IHelperFactory>();
939 return helperFactoryCached;
941 set { helperFactoryCached = value; }
944 #endregion
946 private void RegisterServiceOverrideFromConfigurationNode(IConfiguration serviceConfig)
948 if (serviceConfig == null) throw new ArgumentNullException("serviceConfig");
950 string id = serviceConfig.Attributes["id"];
951 string _interface = serviceConfig.Attributes["interface"];
952 string type = serviceConfig.Attributes["type"];
954 ServiceIdentification servId = (ServiceIdentification) Enum.Parse(typeof(ServiceIdentification), id, true);
956 if (servId == ServiceIdentification.Undefined)
958 throw new MonoRailException("Invalid service id: '" + id + "'. " +
959 "Check your configuration file, services node under MonoRail configuration.");
961 if (string.IsNullOrEmpty(type))
963 throw new MonoRailException("No type specified for service: '" + id + "'. You must add a 'type' attribute. " +
964 "Check your configuration file, services node under MonoRail configuration.");
967 Type service;
969 if (!string.IsNullOrEmpty(_interface))
971 service = Type.GetType(_interface, false, false);
973 if (service == null)
975 throw new MonoRailException("Could not load service type: '" + _interface + "'.");
978 else
980 service = InferServiceFromId(servId);
983 Type impl = Type.GetType(type, false, false);
985 if (impl == null)
987 throw new MonoRailException("Could not load service implementation: '" + impl + "'.");
990 object instance;
994 instance = CreateService(impl);
996 catch(Exception ex)
998 throw new MonoRailException("Could not create implementation: '" + impl + "'.", ex);
1001 AddService(service, instance);
1004 private static Type InferServiceFromId(ServiceIdentification id)
1006 switch(id)
1008 case ServiceIdentification.ControllerFactory:
1009 return typeof(IControllerFactory);
1010 case ServiceIdentification.ControllerContextFactory:
1011 return typeof(IControllerContextFactory);
1012 case ServiceIdentification.ControllerTree:
1013 return typeof(IControllerTree);
1014 case ServiceIdentification.CacheProvider:
1015 return typeof(ICacheProvider);
1016 case ServiceIdentification.UrlBuilder:
1017 return typeof(IUrlBuilder);
1018 case ServiceIdentification.UrlTokenizer:
1019 return typeof(IUrlTokenizer);
1020 case ServiceIdentification.ServerUtility:
1021 return typeof(IServerUtility);
1022 case ServiceIdentification.FilterFactory:
1023 return typeof(IFilterFactory);
1024 case ServiceIdentification.ControllerDescriptorProvider:
1025 return typeof(IControllerDescriptorProvider);
1026 case ServiceIdentification.ResourceDescriptorProvider:
1027 return typeof(IResourceDescriptorProvider);
1028 case ServiceIdentification.RescueDescriptorProvider:
1029 return typeof(IRescueDescriptorProvider);
1030 case ServiceIdentification.LayoutDescriptorProvider:
1031 return typeof(ILayoutDescriptorProvider);
1032 case ServiceIdentification.HelperDescriptorProvider:
1033 return typeof(IHelperDescriptorProvider);
1034 case ServiceIdentification.FilterDescriptorProvider:
1035 return typeof(IFilterDescriptorProvider);
1036 case ServiceIdentification.ViewSourceLoader:
1037 return typeof(IViewSourceLoader);
1038 case ServiceIdentification.ResourceFactory:
1039 return typeof(IResourceFactory);
1040 case ServiceIdentification.ViewEngineManager:
1041 return typeof(IViewEngineManager);
1042 case ServiceIdentification.TransformFilterDescriptorProvider:
1043 return typeof(ITransformFilterDescriptorProvider);
1044 case ServiceIdentification.ValidatorRegistry:
1045 return typeof(IValidatorRegistry);
1046 case ServiceIdentification.EmailSender:
1047 return typeof(IEmailSender);
1048 case ServiceIdentification.ViewComponentFactory:
1049 return typeof(IViewComponentFactory);
1050 case ServiceIdentification.ScaffoldingSupport:
1051 return typeof(IScaffoldingSupport);
1052 case ServiceIdentification.EmailTemplateService:
1053 return typeof(IEmailTemplateService);
1054 case ServiceIdentification.ReturnBinderDescriptorProvider:
1055 return typeof(IReturnBinderDescriptorProvider);
1056 // case ServiceIdentification.TransformationFilterFactory:
1057 // return typeof(ITransformFilterFactory);
1058 case ServiceIdentification.AjaxProxyGenerator:
1059 return typeof(IAjaxProxyGenerator);
1060 case ServiceIdentification.ViewComponentDescriptorProvider:
1061 return typeof(IViewComponentDescriptorProvider);
1062 default:
1063 throw new NotSupportedException("Id not supported " + id);
1067 private static void AssertImplementsService(Type service, Type impl)
1069 if (!service.IsAssignableFrom(impl))
1071 String message = String.Format("Initialization Exception: " +
1072 "Service {0} does not implement or extend {1}", impl.FullName, service.FullName);
1073 throw new ConfigurationErrorsException(message);