2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
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/>.
24 using Org
.Lwes
.Config
;
27 /// Utility class for accessing the default IEventTemplateDB implementation.
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>
37 public static class EventTemplateDB
42 /// Accesses the default instance of the IEventTemplateDB. Delegates to
43 /// an IoC container if present.
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
);
54 { // Not in IoC and not configured; fallback to programmatic default.
55 result
= CreateFallbackTemplateDB();
61 /// Creates an event template DB from the configuration.
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
);
82 /// Creates the named instance. If an IoC container is in use the IoC
83 /// container is consulted for the named instance first.
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
);
98 private static IEventTemplateDB
CreateFallbackTemplateDB()
100 FilePathEventTemplateDB db
= new FilePathEventTemplateDB();
101 db
.InitializeFromFilePath(AppDomain
.CurrentDomain
.SetupInformation
.ApplicationBase
, true);