Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions LinqToWiki.Core/Internals/NamespaceInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,24 +12,34 @@ namespace LinqToWiki.Internals
/// </summary>
public class NamespaceInfo : IEnumerable<Namespace>
{
private readonly Dictionary<int, Namespace> m_namespaces;
private readonly Lazy<Dictionary<int, Namespace>> m_namespaces;

/// <summary>
/// Creates a <see cref="NamespaceInfo"/> based on a function to get a collection of namespaces.
/// </summary>
/// <param name="namespaces"></param>
private NamespaceInfo(Func<IEnumerable<Namespace>> namespaces)
{
m_namespaces = new Lazy<Dictionary<int, Namespace>>(
() => namespaces().ToDictionary(ns => ns.Id));
}
/// <summary>
/// Creates a <see cref="NamespaceInfo"/> based on a collection of namespaces.
/// </summary>
/// <param name="namespaces"></param>
internal NamespaceInfo(IEnumerable<Namespace> namespaces)
: this(() => namespaces)
{
m_namespaces = namespaces.ToDictionary(ns => ns.Id);
}

/// <summary>
/// Creates a <see cref="NamespaceInfo"/> by downloading the namespace information
/// for a wiki.
/// </summary>
public NamespaceInfo(WikiInfo wiki)
: this(GetNamespaces(wiki))
{}
: this(() => GetNamespaces(wiki))
{
}

/// <summary>
/// Downloads the namespaces for a wiki.
Expand All @@ -51,12 +62,12 @@ private static IEnumerable<Namespace> GetNamespaces(WikiInfo wiki)
/// </summary>
public Namespace this[int id]
{
get { return m_namespaces[id]; }
get { return m_namespaces.Value[id]; }
}

public IEnumerator<Namespace> GetEnumerator()
{
return m_namespaces.Values.GetEnumerator();
return m_namespaces.Value.Values.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
Expand Down