public static IPersistenceProvider LoadConfig(EussConfiguration section, string engineName) { int lastIndexOfSlash = section.ElementInformation.Source.LastIndexOf('\\'); if (engines.ContainsKey(engineName)) return engines[engineName]; return CreateEngine(section, section.PersistenceEngines[engineName], section.ElementInformation.Source.Substring(0, lastIndexOfSlash)); }
protected static IPersistenceProvider CreateEngine(EussConfiguration config, PersistenceEngineSection persistenceEngineSection, string basePath) { string factory = persistenceEngineSection.Factory; Assembly assembly = typeof(ConfigLoader).Assembly; // Evaluant.Uss string className; switch (factory) { case "Evaluant.Uss.SqlMapper.SqlMapperProvider": factory = "Evaluant.Uss.SqlMapper.SqlMapperProvider, Evaluant.Uss.SqlMapper"; className = "Evaluant.Uss.SqlMapper.SqlMapperProvider"; break; default: className = factory.Split(',')[0].Trim(); break; } if (factory.IndexOf(",") != -1) { string assemblyName = factory.Split(',')[1].Trim(); // Loads the specified assembly assembly = Assembly.Load(assemblyName); } Type type = null; IPersistenceProvider engine = null; type = assembly.GetType(className); // Search the corresponding partial name as it's not the FullName which is given if (type == null) foreach (Type t in assembly.GetTypes()) if (t.Name == className) { type = t; break; } if (type == null) throw new UniversalStorageException(String.Format("Factory type not found: {0}", className)); engine = Activator.CreateInstance(type) as IPersistenceProvider; if (engine == null) throw new UniversalStorageException("Unknown Persistence Factory : " + className); engines.Add(persistenceEngineSection.Name, engine); engine.Culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); // Initializes its properties if (!string.IsNullOrEmpty(persistenceEngineSection.Culture)) engine.Culture = new CultureInfo(persistenceEngineSection.Culture); PropertyInfo prop = engine.GetType().GetProperty("ConnectionString"); if (prop != null) prop.SetValue(engine, ResolveFilename(persistenceEngineSection.ConnectionString, basePath), null); foreach (PropertyElement pe in persistenceEngineSection) { prop = type.GetProperty(pe.Name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty); if (pe.Name == "ConnectionString") { continue; } if (prop != null && typeof(IPersistenceEngine).IsAssignableFrom(prop.PropertyType)) { if (!engines.ContainsKey(pe.Value)) CreateEngine(config, config.PersistenceEngines[pe.Value], basePath); prop.SetValue(engines[pe.Value], engine, null); } if (pe.Name.ToLower() == "delegator") { foreach (string engineName in pe.Value.Split(new char[] { ',' })) { if (!engines.ContainsKey(engineName)) CreateEngine(config, config.PersistenceEngines[engineName], basePath); AddDelegator(engines[engineName], engine); } continue; } if (pe.Name == "MappingFileNames") { continue; } if (pe.Name == "mapping") { AddMapping(pe, engine, type, basePath); continue; } if (prop == null) throw new UniversalStorageException(String.Format("The property named {0} could not be found", pe.Name)); if (Type.GetTypeCode(prop.PropertyType) != TypeCode.Object) prop.SetValue(engine, Convert.ChangeType(ResolveFilename(pe.Value, basePath), prop.PropertyType), null); else { Type typeToInstantiate = Type.GetType(pe.Value); if (typeToInstantiate == null) typeToInstantiate = assembly.GetType(pe.Value); if (typeToInstantiate == null) throw new UniversalStorageException(String.Format("The property value {0} could not be assigned to the persistence provider", pe.Value)); prop.SetValue(engine, Activator.CreateInstance(typeToInstantiate), null); } } foreach (MetadataElement me in persistenceEngineSection.Metadata) { AddMetadata(me, engine, basePath); } engine.InitializeConfiguration(); return engine; }
public static IPersistenceProvider LoadConfig(EussConfiguration eussConfiguration) { return LoadConfig(eussConfiguration, eussConfiguration.PersistenceEngines.DefaultEngine); }