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.
15 namespace Castle
.MonoRail
.Framework
.Container
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
;
31 public class DefaultMonoRailContainer
: AbstractServiceContainer
, IMonoRailContainer
33 #region ServiceIdentification enum
36 /// Enum for all known MonoRail services.
38 public enum ServiceIdentification
45 /// Custom ( not know service )
49 /// The <see cref="IControllerFactory"/> service
53 /// The <see cref="IControllerContextFactory"/> service
55 ControllerContextFactory
,
57 /// The <see cref="IUrlBuilder"/> service
61 /// The <see cref="IUrlTokenizer"/> service
65 /// The <see cref="IServerUtility"/> service
69 /// The <see cref="IControllerTree"/> service
73 /// The <see cref="ICacheProvider"/> service
77 /// The <see cref="IViewSourceLoader"/> service.
81 /// The <see cref="IFilterFactory"/> service.
85 /// The <see cref="IViewComponentFactory"/> service
89 /// The <see cref="IEmailSender"/> service.
93 /// The <see cref="IControllerDescriptorProvider"/> service
95 ControllerDescriptorProvider
,
97 /// The <see cref="IResourceDescriptorProvider"/> service
99 ResourceDescriptorProvider
,
101 /// The <see cref="IViewComponentDescriptorProvider"/> service
103 ViewComponentDescriptorProvider
,
105 /// The <see cref="IRescueDescriptorProvider"/> service
107 RescueDescriptorProvider
,
109 /// The <see cref="ILayoutDescriptorProvider"/> service
111 LayoutDescriptorProvider
,
113 /// The <see cref="IHelperDescriptorProvider"/> service
115 HelperDescriptorProvider
,
117 /// The <see cref="IFilterDescriptorProvider"/> service
119 FilterDescriptorProvider
,
121 /// The <see cref="IReturnBinderDescriptorProvider"/> service
123 ReturnBinderDescriptorProvider
,
125 /// The <see cref="IResourceFactory"/> service
129 /// The <see cref="IEmailTemplateService"/> service
131 EmailTemplateService
,
133 /// The <see cref="IScaffoldingSupport"/> service
137 /// The <see cref="ITransformFilterDescriptorProvider"/> service
139 TransformFilterDescriptorProvider
,
141 // /// The <see cref="ITransformFilterFactory"/> service
143 // TransformationFilterFactory,
145 /// The <see cref="IViewEngineManager"/> service
149 /// The <see cref="IValidatorRegistry"/> service
153 /// The <see cref="IAjaxProxyGenerator"/> service
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
;
184 /// Initializes a new instance of the <see cref="DefaultMonoRailContainer"/> class.
186 /// <param name="parentContainer">The parent container.</param>
187 public DefaultMonoRailContainer(IServiceProvider parentContainer
) : base(parentContainer
)
192 /// Initializes a new instance of the <see cref="DefaultMonoRailContainer"/> class.
194 public DefaultMonoRailContainer()
201 public void UseServicesFromParent()
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
);
356 /// Installs the primordial services.
358 public void InstallPrimordialServices()
360 if (!HasService
<IServiceInitializer
>())
362 AddService
<IServiceInitializer
>(new DefaultServiceInitializer());
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
>());
494 /// Initialize extensions and start the extension manager.
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
);
517 /// Configures the specified config.
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
));
541 /// Creates the specified service running all life cycles for it.
543 /// <param name="type">The service type.</param>
544 /// <returns></returns>
545 protected override object CreateService(Type type
)
549 throw new ArgumentNullException("type");
556 instance
= Activator
.CreateInstance(type
);
560 throw new MonoRailException("Error trying to instantiate service " + type
.FullName
, ex
);
565 ServiceInitializer
.Initialize(instance
, this);
569 throw new MonoRailException("Error running lifecycle steps for MonoRail service " + type
.FullName
, ex
);
578 /// Gets or sets the URL tokenizer.
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; }
595 /// Gets or sets the URL builder.
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; }
612 /// Gets or sets the cache provider.
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; }
629 /// Gets or sets the engine context factory.
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; }
646 /// Gets or sets the controller factory.
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; }
663 /// Gets or sets the controller context factory.
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; }
680 /// Gets or sets the controller tree.
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; }
697 /// Gets or sets the view source loader.
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; }
714 /// Gets or sets the filter factory.
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; }
731 /// Gets or sets the controller descriptor provider.
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; }
748 /// Gets or sets the view engine manager.
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; }
765 /// Gets or sets the validator registry.
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; }
782 /// Gets or sets the action selector.
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; }
799 /// Gets or sets the scaffold support.
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; }
816 /// Gets or sets the JSON serializer.
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; }
833 /// Gets or sets the static resource registry service.
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; }
850 /// Gets or sets the email template service.
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; }
867 /// Gets or sets the email sender.
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; }
884 /// Gets or sets the extension manager.
886 /// <value>The extension manager.</value>
887 public ExtensionManager ExtensionManager
889 get { return extensionManager; }
890 set { extensionManager = value; }
894 /// Gets or sets the resource factory.
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; }
911 /// Gets or sets the service initializer.
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; }
928 /// Gets or sets the helper factory.
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; }
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.");
969 if (!string.IsNullOrEmpty(_interface
))
971 service
= Type
.GetType(_interface
, false, false);
975 throw new MonoRailException("Could not load service type: '" + _interface
+ "'.");
980 service
= InferServiceFromId(servId
);
983 Type impl
= Type
.GetType(type
, false, false);
987 throw new MonoRailException("Could not load service implementation: '" + impl
+ "'.");
994 instance
= CreateService(impl
);
998 throw new MonoRailException("Could not create implementation: '" + impl
+ "'.", ex
);
1001 AddService(service
, instance
);
1004 private static Type
InferServiceFromId(ServiceIdentification 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
);
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
);