using System; using System.IO; using System.Runtime.CompilerServices; using System.Windows; using Autofac; using Chernobyl.Utility; namespace Chernobyl.App.Core { /// /// Extension and utility methods for Autofac. /// public static class AutofacExt { /// /// Binds a resource dictionary with the name "{name}.Templates.xaml" to the provided /// as a service. This name of the template file is automatically /// determined by the name of the module file where this method is called. For example, if /// the name of the file is "Factories.Module.cs" then 'name' will be "Factories". /// /// The instance to accept the binding. /// The path of the file where this method is called. Leave this /// parameter unfilled and it will be set automatically. public static void BindTemplateResourceAuto(this ContainerBuilder builder, [CallerFilePath]string callingFilePath = "") { builder.BindTemplateResource(GetTemplateNameFromFilePath(callingFilePath)); } /// /// Binds a resource dictionary with the name "{name}.Templates.xaml" to the provided /// as a service. /// /// The instance to accept the binding. /// The folder and where the resource dictionary file "{name}.Templates.xaml" /// to bind is located. public static void BindTemplateResource(this ContainerBuilder builder, string name) { builder.BindResourceDictionary($"{name}/{name}.Templates.xaml"); } /// /// Binds a resource dictionary with the name "{name}.Styles.xaml" to the provided /// as a service. This name of the template file is automatically /// determined by the name of the module file where this method is called. For example, if /// the name of the file is "Factories.Module.cs" then 'name' will be "Factories". /// /// The instance to accept the binding. /// The path of the file where this method is called. Leave this /// parameter unfilled and it will be set automatically. public static void BindStylesResourceAuto(this ContainerBuilder builder, [CallerFilePath]string callingFilePath = "") { builder.BindStylesResource(GetTemplateNameFromFilePath(callingFilePath)); } /// /// Binds a resource dictionary with the name "{name}.Styles.xaml" to the provided /// as a service. /// /// The instance to accept the binding. /// The folder and where the resource dictionary file "{name}.Styles.xaml" /// to bind is located. public static void BindStylesResource(this ContainerBuilder builder, string name) { builder.BindResourceDictionary($"{name}/{name}.Styles.xaml"); } /// /// Binds a resource dictionary to the provided as a service. /// /// The instance to accept the binding. /// The string URI to the resource dictionary to be bound. /// Whether the is relative, absolute, etc. public static void BindResourceDictionary(this ContainerBuilder builder, string dictionaryUri, UriKind uriKind = UriKind.Relative) { builder.Register(context => new ResourceDictionary { Source = new Uri(dictionaryUri, uriKind) }); } /// /// Gets the name of resource dictionary template from a module file path. /// static string GetTemplateNameFromFilePath(string moduleFilePath) { var fileName = Path.GetFileName(moduleFilePath); fileName.ThrowIfEmptyOrNull("Resource dictionary template file name"); var moduleStringIndex = fileName.IndexOf(".Module", StringComparison.Ordinal); return fileName.Remove(moduleStringIndex); } } }