using System.Collections.Generic; using System.Diagnostics; using JD.Collections; using JD.TeaBoxAddinSystem.Addins.Description; namespace JD.TeaBoxAddinSystem.Addins.Internal { /// /// Dependency manager. /// internal class DependencyManager { #region Variables private readonly MultiDictionary m_dependencies = new MultiDictionary(); #endregion #region Methods internal void RegisterModule(Module module) { foreach (AddinReference reference in module.Dependencies) { AddDependency(module, reference.Name); } } /// /// Adds a dependency of towards the given . /// /// /// private void AddDependency(Module module, string addinId) { Debug.Assert(module != null); Debug.Assert(!string.IsNullOrEmpty(addinId)); Debug.Assert(!m_dependencies.Contains(addinId, module)); m_dependencies.Add(addinId, module); } internal void UnregisterModule(Module module) { foreach (AddinReference reference in module.Dependencies) { RemoveDependency(module, reference.Name); } } /// /// Removes a dependency of towards the given . /// /// /// private void RemoveDependency(Module module, string addinId) { Debug.Assert(module != null); Debug.Assert(!string.IsNullOrEmpty(addinId)); Debug.Assert(m_dependencies.Contains(addinId, module)); m_dependencies.Remove(addinId, module); } /// /// Clears all dependency info. /// internal void Clear() { m_dependencies.Clear(); } /// /// Gets all the dependencies (weak and strong) on the given . /// /// /// public IEnumerable GetDependencies(string addinId) { Debug.Assert(!string.IsNullOrEmpty(addinId)); return m_dependencies[addinId]; } /// /// Gets the modules that depend weakly on the given . /// /// /// public IEnumerable GetWeakDependencies(string addinId) { foreach (Module module in GetDependencies(addinId)) { if (!module.IsMainModule) { yield return module; } } } /// /// Gets the modules that depend strongly on the given . /// /// /// public IEnumerable GetStrongDependencies(string addinId) { foreach (Module module in GetDependencies(addinId)) { if (module.IsMainModule) { yield return module; } } } /// /// Gets whether there is an addin that depends on the given . /// /// /// public bool HasStrongDependencies(string addinId) { foreach (Module module in GetStrongDependencies(addinId)) { return true; } return false; } #endregion } }