trivial, email change -- not affecting code
[lwes-dotnet/github-mirror.git] / Org.Lwes / DB / EventTemplateDB.cs
blobbb2b52c4083d3804b9855c01582181a39c6074a1
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.DB
22 using System;
24 using Org.Lwes.Config;
26 /// <summary>
27 /// Utility class for accessing the default IEventTemplateDB implementation.
28 /// </summary>
29 /// <remarks>
30 /// <para>If there is an ambient ServiceLocator present then this utility class will
31 /// delegate to the ServiceLocator. The ServiceLocator should declare an instance
32 /// of IEventTemplateDB with the name "eventTemplateDB".</para>
33 /// <para>If an IoC container is not present, or if a service instance is not defined
34 /// with the name "eventTemplateDB" then this utility will create a FilePathEventTemplateDB
35 /// initialized to use the application's base directory to load event specification files (*.esf) files.</para>
36 /// </remarks>
37 public static class EventTemplateDB
39 #region Methods
41 /// <summary>
42 /// Accesses the default instance of the IEventTemplateDB. Delegates to
43 /// an IoC container if present.
44 /// </summary>
45 public static IEventTemplateDB CreateDefault()
47 IEventTemplateDB result;
48 if (!IoCAdapter.TryCreateFromIoC<IEventTemplateDB>(Constants.DefaultEventTemplateDBContainerKey, out result))
49 { // Either there isn't a default event template defined in the IoC container
50 // or there isn't an IoC container in use... fall back to configuration section.
51 result = CreateFromConfig(Constants.DefaultEventTemplateDBConfigName);
53 if (result == null)
54 { // Not in IoC and not configured; fallback to programmatic default.
55 result = CreateFallbackTemplateDB();
57 return result;
60 /// <summary>
61 /// Creates an event template DB from the configuration.
62 /// </summary>
63 /// <param name="name">name of the instance to create</param>
64 /// <returns>the named instance if it exists within the configuration;
65 /// otherwise null</returns>
66 /// <remarks>Note that two subsequent calls to this method will return
67 /// two separate instances of the configured instance.</remarks>
68 public static IEventTemplateDB CreateFromConfig(string name)
70 LwesConfigurationSection config = LwesConfigurationSection.Current;
71 if (config.TemplateDBs == null) return null;
73 TemplateDBConfigurationSection namedTemplateDBConfig = config.TemplateDBs[name];
74 if (namedTemplateDBConfig == null) return null;
76 FilePathEventTemplateDB db = new FilePathEventTemplateDB();
77 db.InitializeFromFilePath(namedTemplateDBConfig.Path, namedTemplateDBConfig.IncludeSubdirectories);
78 return db;
81 /// <summary>
82 /// Creates the named instance. If an IoC container is in use the IoC
83 /// container is consulted for the named instance first.
84 /// </summary>
85 /// <param name="name">name of the instance to create</param>
86 /// <returns>the named instance if it exists either within the IoC container
87 /// or the configuration; otherwise null</returns>
88 public static IEventTemplateDB CreateNamedTemplateDB(string name)
90 IEventTemplateDB result;
91 if (!IoCAdapter.TryCreateFromIoC<IEventTemplateDB>(name, out result))
93 result = CreateFromConfig(name);
95 return result;
98 private static IEventTemplateDB CreateFallbackTemplateDB()
100 FilePathEventTemplateDB db = new FilePathEventTemplateDB();
101 db.InitializeFromFilePath(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, true);
102 return db;
105 #endregion Methods