/src/Framework/N2/Engine/ContentEngine.cs

https://github.com/lundbeck/n2cms · C# · 238 lines · 156 code · 40 blank · 42 comment · 5 complexity · 8b039975ace4a2d70ae8a81fd48cbefc MD5 · raw file

  1. #region License
  2. /* Copyright (C) 2006-2007 Cristian Libardo
  3. *
  4. * This is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2.1 of
  7. * the License, or (at your option) any later version.
  8. */
  9. #endregion
  10. using System;
  11. using N2.Configuration;
  12. using N2.Definitions;
  13. using N2.Edit;
  14. using N2.Integrity;
  15. using N2.Persistence;
  16. using N2.Plugin;
  17. using N2.Security;
  18. using N2.Web;
  19. using System.Diagnostics;
  20. namespace N2.Engine
  21. {
  22. /// <summary>
  23. /// This principal gateway to N2 services. The class is responsible for
  24. /// initializing and providing access to the services that compose N2.
  25. /// </summary>
  26. public class ContentEngine : IEngine
  27. {
  28. private readonly IServiceContainer container;
  29. /// <summary>
  30. /// Creates an instance of the content engine using default settings and configuration.
  31. /// </summary>
  32. public ContentEngine()
  33. : this(new TinyIoC.TinyIoCServiceContainer(), EventBroker.Instance, new ContainerConfigurer())
  34. {
  35. }
  36. /// <summary>
  37. /// Creates an instance of the content engine using default settings and configuration.
  38. /// </summary>
  39. public ContentEngine(IServiceContainer container)
  40. : this(container, EventBroker.Instance, new ContainerConfigurer())
  41. {
  42. }
  43. /// <summary>Sets the current container to the given container.</summary>
  44. /// <param name="container">An previously prepared service container.</param>
  45. /// <param name="broker"></param>
  46. public ContentEngine(IServiceContainer container, EventBroker broker, ContainerConfigurer configurer)
  47. {
  48. this.container = container;
  49. configurer.Configure(this, broker, new ConfigurationManagerWrapper());
  50. }
  51. /// <summary>Tries to determine runtime parameters from the given configuration.</summary>
  52. /// <param name="config">The configuration to use.</param>
  53. /// <param name="sectionGroup">The configuration section to retrieve configuration from</param>
  54. /// <param name="broker">Web ap event provider</param>
  55. public ContentEngine(System.Configuration.Configuration config, string sectionGroup, IServiceContainer container, EventBroker broker, ContainerConfigurer configurer)
  56. {
  57. if (config == null) throw new ArgumentNullException("config");
  58. if (string.IsNullOrEmpty(sectionGroup)) throw new ArgumentException("Must be non-empty and match a section group in the configuration file.", "sectionGroup");
  59. this.container = container;
  60. configurer.Configure(this, broker, new ConfigurationReadingWrapper(config, sectionGroup));
  61. }
  62. private class ConfigurationReadingWrapper : ConfigurationManagerWrapper
  63. {
  64. System.Configuration.Configuration config;
  65. public ConfigurationReadingWrapper(System.Configuration.Configuration config, string sectionGroup)
  66. : base(sectionGroup)
  67. {
  68. this.config = config;
  69. }
  70. public override T GetSection<T>(string sectionName, bool required = true)
  71. {
  72. return config.GetSection(sectionName) as T;
  73. }
  74. }
  75. #region Properties
  76. public IServiceContainer Container
  77. {
  78. get { return container; }
  79. }
  80. /// <summary>Gets N2 persistence manager used for database persistence of content.</summary>
  81. public IPersister Persister
  82. {
  83. get { return Resolve<IPersister>(); }
  84. }
  85. /// <summary>Gets N2 definition manager</summary>
  86. public IDefinitionManager Definitions
  87. {
  88. get { return Resolve<IDefinitionManager>(); }
  89. }
  90. /// <summary>Gets N2 integrity manager </summary>
  91. public IIntegrityManager IntegrityManager
  92. {
  93. get { return Resolve<IIntegrityManager>(); }
  94. }
  95. /// <summary>Gets N2 security manager responsible of item access checks.</summary>
  96. public ISecurityManager SecurityManager
  97. {
  98. get { return Resolve<ISecurityManager>(); }
  99. }
  100. /// <summary>Gets the url parser responsible of mapping managementUrls to items and back again.</summary>
  101. public IUrlParser UrlParser
  102. {
  103. get { return Resolve<IUrlParser>(); }
  104. }
  105. /// <summary>Gets the edit manager responsible for plugins in edit mode.</summary>
  106. public IEditManager EditManager
  107. {
  108. get { return Resolve<IEditManager>(); }
  109. }
  110. public IEditUrlManager ManagementPaths
  111. {
  112. get { return Resolve<IEditUrlManager>(); }
  113. }
  114. public IWebContext RequestContext
  115. {
  116. get { return Resolve<IWebContext>(); }
  117. }
  118. public IHost Host
  119. {
  120. get { return Resolve<IHost>(); }
  121. }
  122. #endregion
  123. #region Methods
  124. public void Initialize()
  125. {
  126. var bootstrapper = container.Resolve<IPluginBootstrapper>();
  127. var plugins = bootstrapper.GetPluginDefinitions();
  128. bootstrapper.InitializePlugins(this, plugins);
  129. container.StartComponents();
  130. }
  131. #endregion
  132. #region Container Methods
  133. /// <summary>Resolves a service configured in the factory.</summary>
  134. [DebuggerStepThrough]
  135. public T Resolve<T>() where T : class
  136. {
  137. return (T) Container.Resolve(typeof (T));
  138. }
  139. public object Resolve(Type serviceType)
  140. {
  141. return Container.Resolve(serviceType);
  142. }
  143. /// <summary>Registers a component in the IoC container.</summary>
  144. /// <param name="key">A unique key.</param>
  145. /// <param name="serviceType">The type of component to register.</param>
  146. [Obsolete("Use Container.AddComponent")]
  147. public virtual void AddComponent(string key, Type serviceType)
  148. {
  149. AddComponent(key, serviceType, serviceType);
  150. }
  151. /// <summary>Registers a component in the IoC container.</summary>
  152. /// <param name="key">A unique key.</param>
  153. /// <param name="serviceType">The type of service to provide.</param>
  154. /// <param name="classType">The type of component to register.</param>
  155. [Obsolete("Use Container.AddComponent")]
  156. public virtual void AddComponent(string key, Type serviceType, Type classType)
  157. {
  158. Container.AddComponent(key, serviceType, classType);
  159. }
  160. /// <summary>Adds a compnent instance to the container.</summary>
  161. /// <param name="key">A unique key.</param>
  162. /// <param name="serviceType">The type of service to provide.</param>
  163. /// <param name="instance">The service instance to add.</param>
  164. [Obsolete("Use Container.AddComponentInstance")]
  165. public void AddComponentInstance(string key, Type serviceType, object instance)
  166. {
  167. Container.AddComponentInstance(key, serviceType, instance);
  168. }
  169. [Obsolete("Use Container.AddComponentLifeStyle")]
  170. public void AddComponentLifeStyle(string key, Type classType, ComponentLifeStyle lifeStyle)
  171. {
  172. Container.AddComponentLifeStyle(key, classType, lifeStyle);
  173. }
  174. [Obsolete("Use Container.Release")]
  175. public void Release(object instance)
  176. {
  177. Container.Release(instance);
  178. }
  179. [Obsolete("Use Container.AddComponentInstance")]
  180. public T AddComponentInstance<T>(T instance) where T : class
  181. {
  182. if (instance != null)
  183. {
  184. AddComponentInstance(typeof (T).Name, typeof (T), instance);
  185. }
  186. return instance;
  187. }
  188. public ContentHelperBase Content
  189. {
  190. get { return new ContentHelperBase(() => this, () => RequestContext.CurrentPath); }
  191. }
  192. #endregion
  193. public ConfigurationManagerWrapper Config
  194. {
  195. get { return Resolve<ConfigurationManagerWrapper>(); }
  196. }
  197. }
  198. }