Improving parts management performance

This commit is contained in:
Sebastien Ros
2014-08-05 14:55:32 -07:00
parent 6550fbbf22
commit 80be0de019
2 changed files with 21 additions and 10 deletions

View File

@@ -284,11 +284,14 @@ namespace Orchard.ContentTypes.Controllers {
if (typeViewModel == null)
return HttpNotFound();
var typePartNames = new HashSet<string>(typeViewModel.Parts.Select(tvm => tvm.PartDefinition.Name));
var viewModel = new AddPartsViewModel {
Type = typeViewModel,
PartSelections = _contentDefinitionService.GetParts(false/*metadataPartsOnly*/)
.Where(cpd => !typeViewModel.Parts.Any(p => p.PartDefinition.Name == cpd.Name) && cpd.Settings.GetModel<ContentPartSettings>().Attachable)
.Select(cpd => new PartSelectionViewModel { PartName = cpd.Name, PartDisplayName = cpd.DisplayName, PartDescription = cpd.Description})
.Where(cpd => !typePartNames.Contains(cpd.Name) && cpd.Settings.GetModel<ContentPartSettings>().Attachable)
.Select(cpd => new PartSelectionViewModel { PartName = cpd.Name, PartDisplayName = cpd.DisplayName, PartDescription = cpd.Description })
.ToList()
};
return View(viewModel);

View File

@@ -180,22 +180,30 @@ namespace Orchard.ContentTypes.Services {
}
public IEnumerable<EditPartViewModel> GetParts(bool metadataPartsOnly) {
var typeNames = GetTypes().Select(ctd => ctd.Name);
var typeNames = new HashSet<string>(GetTypes().Select(ctd => ctd.Name));
// user-defined parts
// except for those parts with the same name as a type (implicit type's part or a mistake)
var userContentParts = _contentDefinitionManager
.ListPartDefinitions()
var userContentParts = _contentDefinitionManager.ListPartDefinitions()
.Where(cpd => !typeNames.Contains(cpd.Name))
.Select(cpd => new EditPartViewModel(cpd));
.Select(cpd => new EditPartViewModel(cpd))
.ToDictionary(
k => k.Name,
v => v);
// code-defined parts
var codeDefinedParts = metadataPartsOnly ?
Enumerable.Empty<EditPartViewModel>() :
_contentPartDrivers.SelectMany(d => d.GetPartInfo().Where(cpd => !userContentParts.Any(m => m.Name == cpd.PartName)).Select(cpi => new EditPartViewModel { Name = cpi.PartName }));
var codeDefinedParts = metadataPartsOnly
? Enumerable.Empty<EditPartViewModel>()
: _contentPartDrivers
.SelectMany(d => d.GetPartInfo()
.Where(cpd => !userContentParts.ContainsKey(cpd.PartName))
.Select(cpi => new EditPartViewModel { Name = cpi.PartName, DisplayName = cpi.PartName }))
.ToList();
// Order by display name
return userContentParts.Union(codeDefinedParts).OrderBy(m => m.DisplayName);
return codeDefinedParts
.Union(userContentParts.Values)
.OrderBy(m => m.DisplayName);
}
public EditPartViewModel GetPart(string name) {