public IEnumerable<ModuleData> ListModules()
{
foreach (XElement m in Source.Descendants("Module"))
{
yield return new ModuleData(m.Element("ModuleID").Value);
}
}
Изначально вышеуказанный код велик, поскольку нет необходимости оценивать всю коллекцию, если она не нужна.
Однако, как только все Модули были перечислены один раз, становится все дороже повторно запрашивать XDocument, когда нет изменений.
Итак, как улучшение производительности:
public IEnumerable<ModuleData> ListModules()
{
if (Modules == null)
{
Modules = new List<ModuleData>();
foreach (XElement m in Source.Descendants("Module"))
{
Modules.Add(new ModuleData(m.Element("ModuleID").Value, 1, 1));
}
}
return Modules;
}
Это здорово, если я неоднократно использую весь список, но не так сильно.
Есть ли промежуточная площадка, где я могу вернуть результат до тех пор, пока весь список не будет повторен, затем кешируйте его и обслуживайте кеш для последующих запросов?