/// <summary> /// Returns a specific item. /// </summary> /// <param name="name">Name of the dynamic item to return.</param> /// <param name="contextSectionName">Name of the context section to consider.</param> /// <param name="persistenceLevel">The persistence level to consider.</param> /// <returns>The dynamic item with specified name and type.</returns> public object GetItem(string name, string contextSectionName = null, PersistenceLevels persistenceLevel = PersistenceLevels.Any) { if (persistenceLevel == PersistenceLevels.Any) { return((GetSingletonItem(name, contextSectionName) ?? GetScopedItem(name, contextSectionName)) ?? GetTransientItem(name, contextSectionName)); } else { switch (persistenceLevel) { case PersistenceLevels.Singleton: return(GetSingletonItem(name, contextSectionName)); case PersistenceLevels.Scoped: return(GetScopedItem(name, contextSectionName)); case PersistenceLevels.Transient: return(GetTransientItem(name, contextSectionName)); } } return(null); }
/// <summary> /// Removes the singleton items of a specific type. /// </summary> /// <param name="contextSectionName">Name of the context section to consider.</param> /// <param name="persistenceLevel">The persistence level to consider.</param> public void RemoveItems( string contextSectionName = null, PersistenceLevels persistenceLevel = PersistenceLevels.Singleton) { if ((persistenceLevel == PersistenceLevels.Any) || (persistenceLevel == PersistenceLevels.Singleton)) { var items = SingletonItems.Keys.Where(p => (string.IsNullOrEmpty(contextSectionName)) || (p?.ToString().ToLower().StartsWith(contextSectionName.ToLower() + "$") == true)); foreach (string key in items) { SingletonItems.Remove(key); } } if ((persistenceLevel == PersistenceLevels.Any) || (persistenceLevel == PersistenceLevels.Scoped)) { var items = ScopedItems.Keys.Where(p => (string.IsNullOrEmpty(contextSectionName)) || (p?.ToString().ToLower().StartsWith(contextSectionName.ToLower() + "$") == true)); foreach (string key in items) { ScopedItems.Remove(key); } } if ((persistenceLevel == PersistenceLevels.Any) || (persistenceLevel == PersistenceLevels.Transient)) { var items = TransientItems.Keys.Where(p => (string.IsNullOrEmpty(contextSectionName)) || (p?.ToString().ToLower().StartsWith(contextSectionName.ToLower() + "$") == true)); foreach (string key in items) { TransientItems.Remove(key); } } }
// Items ------------------------------------ /// <summary> /// Adds a new item to this instance. /// </summary> /// <param name="name">Name of the item to add.</param> /// <param name="item">Item to add.</param> /// <param name="contextSectionName">Name of the context section to consider.</param> /// <param name="persistenceLevel">Persistence level of the item to add.</param> public void AddItem( string name, object item, string contextSectionName = null, PersistenceLevels persistenceLevel = PersistenceLevels.Singleton) { switch (persistenceLevel) { case PersistenceLevels.Singleton: AddSingletonItem(name, item, contextSectionName); break; case PersistenceLevels.Scoped: AddScopedItem(name, item, contextSectionName); break; case PersistenceLevels.Transient: AddTransientItem(name, item, contextSectionName); break; } }
/// <summary> /// Clears the specified items of this instance. /// </summary> /// <param name="persistenceLevel">Persistence level of the item to add.</param> public void ClearItems(PersistenceLevels persistenceLevel = PersistenceLevels.Singleton) { switch (persistenceLevel) { case PersistenceLevels.Singleton: SingletonItems.Clear(); break; case PersistenceLevels.Scoped: ScopedItems.Clear(); break; case PersistenceLevels.Transient: TransientItems.Clear(); break; case PersistenceLevels.Any: SingletonItems.Clear(); ScopedItems.Clear(); TransientItems.Clear(); break; } }