Skip to content

Commit 83aa473

Browse files
committed
Added ConfigurationModuleCatalog implementation of ModuleCatalog
1 parent dbe366a commit 83aa473

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.IO;
7+
using System.Linq;
8+
using Prism.Avalonia.Properties;
9+
using Prism.Properties;
10+
11+
namespace Prism.Modularity
12+
{
13+
14+
/// <summary>
15+
/// A catalog built from a configuration file.
16+
/// </summary>
17+
public class ConfigurationModuleCatalog : ModuleCatalog
18+
{
19+
/// <summary>
20+
/// Builds an instance of ConfigurationModuleCatalog with a <see cref="ConfigurationStore"/> as the default store.
21+
/// </summary>
22+
public ConfigurationModuleCatalog()
23+
{
24+
this.Store = new ConfigurationStore();
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets the store where the configuration is kept.
29+
/// </summary>
30+
public IConfigurationStore Store { get; set; }
31+
32+
/// <summary>
33+
/// Loads the catalog from the configuration.
34+
/// </summary>
35+
protected override void InnerLoad()
36+
{
37+
if (this.Store == null)
38+
{
39+
throw new InvalidOperationException(Resources.ConfigurationStoreCannotBeNull);
40+
}
41+
42+
this.EnsureModulesDiscovered();
43+
}
44+
45+
private static string GetFileAbsoluteUri(string filePath)
46+
{
47+
UriBuilder uriBuilder = new UriBuilder();
48+
uriBuilder.Host = String.Empty;
49+
uriBuilder.Scheme = Uri.UriSchemeFile;
50+
uriBuilder.Path = Path.GetFullPath(filePath);
51+
Uri fileUri = uriBuilder.Uri;
52+
53+
return fileUri.ToString();
54+
}
55+
56+
private void EnsureModulesDiscovered()
57+
{
58+
ModulesConfigurationSection section = this.Store.RetrieveModuleConfigurationSection();
59+
60+
if (section != null)
61+
{
62+
foreach (ModuleConfigurationElement element in section.Modules)
63+
{
64+
IList<string> dependencies = new List<string>();
65+
66+
if (element.Dependencies.Count > 0)
67+
{
68+
foreach (ModuleDependencyConfigurationElement dependency in element.Dependencies)
69+
{
70+
dependencies.Add(dependency.ModuleName);
71+
}
72+
}
73+
74+
ModuleInfo moduleInfo = new ModuleInfo(element.ModuleName, element.ModuleType)
75+
{
76+
Ref = GetFileAbsoluteUri(element.AssemblyFile),
77+
InitializationMode = element.StartupLoaded ? InitializationMode.WhenAvailable : InitializationMode.OnDemand
78+
};
79+
moduleInfo.DependsOn.AddRange(dependencies.ToArray());
80+
AddModule(moduleInfo);
81+
}
82+
}
83+
}
84+
}
85+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
3+
using System;
4+
5+
namespace Prism.Modularity
6+
{
7+
/// <summary>
8+
/// Specifies that the current module has a dependency on another module. This attribute should be used on classes that implement <see cref="IModule"/>.
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
11+
public sealed class ModuleDependencyAttribute : Attribute
12+
{
13+
private readonly string _moduleName;
14+
15+
/// <summary>
16+
/// Initializes a new instance of <see cref="ModuleDependencyAttribute"/>.
17+
/// </summary>
18+
/// <param name="moduleName">The name of the module that this module is dependant upon.</param>
19+
public ModuleDependencyAttribute(string moduleName)
20+
{
21+
_moduleName = moduleName;
22+
}
23+
24+
/// <summary>
25+
/// Gets the name of the module that this module is dependant upon.
26+
/// </summary>
27+
/// <value>The name of the module that this module is dependant upon.</value>
28+
public string ModuleName
29+
{
30+
get { return _moduleName; }
31+
}
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
using System;
4+
5+
namespace Prism.Modularity
6+
{
7+
[Serializable]
8+
public partial class ModuleInfo
9+
{
10+
}
11+
}

Prism.Avalonia/Prism.Avalonia.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageReference Include="Avalonia" Version="0.5.2-build4248-alpha" />
99
<PackageReference Include="CommonServiceLocator" Version="2.0.1" />
1010
<PackageReference Include="Prism.Core" Version="7.0.0.269-pre" />
11+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
1112
</ItemGroup>
1213

1314
<ItemGroup>

0 commit comments

Comments
 (0)