/// <summary> /// Create the .NET class for a particular projection type from its name /// </summary> /// <param name="projectionName"> /// The "business" name of the projection to map to a .NET class /// </param> public IProjection CreateProjectionClass(string projectionName) { if (string.IsNullOrWhiteSpace(projectionName)) { // No idea what projection is being sought return(null); } if (AllMaps.ContainsKey(projectionName)) { return(AllMaps[projectionName].CreateProjectionClass()); } else { // maybe the projection name already is the .NET class (bad practice) var type = Type.GetType(projectionName, false); if (null == type) { if (!EventMaps.TryFindType(projectionName, out type)) { // Unable to create an event class for the name return(null); } } if (null != type) { return((IProjection)Activator.CreateInstance(type)); } } // Unable to create an event class for the name return(null); }
/// <summary> /// Load the projection maps as stored in any configuration files /// </summary> public void LoadFromConfig(string basePath = null) { if (string.IsNullOrWhiteSpace(basePath)) { basePath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") // local_root ?? (Environment.GetEnvironmentVariable("HOME") == null ? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) : $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot"); // azure_root } ConfigurationBuilder builder = new ConfigurationBuilder(); if (!string.IsNullOrWhiteSpace(basePath)) { builder.SetBasePath(basePath); } builder.AddJsonFile("appsettings.json", true) .AddJsonFile("config.local.json", true) .AddJsonFile("config.json", true) .AddJsonFile("connectionstrings.json", true) .AddEnvironmentVariables(); IConfigurationRoot config = builder.Build(); // Get the [ProjectionMaps] section IConfigurationSection projectionMapSection = config.GetSection("ProjectionMaps"); if (null != projectionMapSection) { if (projectionMapSection.Exists()) { var configProjectionMaps = projectionMapSection.Get <List <ProjectionMap> >(c => c.BindNonPublicProperties = true); if (null != configProjectionMaps) { foreach (ProjectionMap map in configProjectionMaps) { if (null != map) { if (!AllMaps.ContainsKey(map.ProjectionName)) { Type eventType = null; if (EventMaps.TryFindType(map.ProjectionImplementationClassName, out eventType)) { AllMaps.Add(map.ProjectionName, new ProjectionMap(map.ProjectionName, eventType.FullName)); } } } } } } } }
public IProjection CreateProjectionClass() { if (!string.IsNullOrWhiteSpace(ProjectionImplementationClassName)) { Type typeRet; if (EventMaps.TryFindType(ProjectionImplementationClassName, out typeRet)) { return((IProjection)(Activator.CreateInstance(typeRet))); } } // Could not create any instance return(null); }