Move the SetWeight logic to WeightedCollectionBuilderBase so other collections can use it too
This commit is contained in:
committed by
Nathan Woulfe
parent
17e43a6b09
commit
499d22aa20
@@ -16,6 +16,8 @@ namespace Umbraco.Core.Composing
|
||||
{
|
||||
protected abstract TBuilder This { get; }
|
||||
|
||||
private readonly Dictionary<Type, int> _customWeights = new Dictionary<Type, int>();
|
||||
|
||||
/// <summary>
|
||||
/// Clears all types in the collection.
|
||||
/// </summary>
|
||||
@@ -107,6 +109,18 @@ namespace Umbraco.Core.Composing
|
||||
return This;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the default weight of an item
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of item</typeparam>
|
||||
/// <param name="weight">The new weight</param>
|
||||
/// <returns></returns>
|
||||
public TBuilder SetWeight<T>(int weight) where T : TItem
|
||||
{
|
||||
_customWeights[typeof(T)] = weight;
|
||||
return This;
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> GetRegisteringTypes(IEnumerable<Type> types)
|
||||
{
|
||||
var list = types.ToList();
|
||||
@@ -118,6 +132,8 @@ namespace Umbraco.Core.Composing
|
||||
|
||||
protected virtual int GetWeight(Type type)
|
||||
{
|
||||
if (_customWeights.ContainsKey(type))
|
||||
return _customWeights[type];
|
||||
var attr = type.GetCustomAttributes(typeof(WeightAttribute), false).OfType<WeightAttribute>().SingleOrDefault();
|
||||
return attr?.Weight ?? DefaultWeight;
|
||||
}
|
||||
|
||||
@@ -442,6 +442,19 @@ namespace Umbraco.Tests.Composing
|
||||
AssertCollection(col, typeof(Resolved2), typeof(Resolved1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WeightedBuilderSetWeight()
|
||||
{
|
||||
var builder = _composition.WithCollectionBuilder<TestCollectionBuilderWeighted>()
|
||||
.Add<Resolved1>()
|
||||
.Add<Resolved2>();
|
||||
builder.SetWeight<Resolved1>(10);
|
||||
|
||||
var factory = _composition.CreateFactory();
|
||||
var col = builder.CreateCollection(factory);
|
||||
AssertCollection(col, typeof(Resolved1), typeof(Resolved2));
|
||||
}
|
||||
|
||||
#region Assertions
|
||||
|
||||
private static void AssertCollection(IEnumerable<Resolved> col, params Type[] expected)
|
||||
|
||||
@@ -10,22 +10,8 @@ namespace Umbraco.Web.Dashboards
|
||||
{
|
||||
public class DashboardCollectionBuilder : WeightedCollectionBuilderBase<DashboardCollectionBuilder, DashboardCollection, IDashboard>
|
||||
{
|
||||
private Dictionary<Type, int> _customWeights = new Dictionary<Type, int>();
|
||||
|
||||
protected override DashboardCollectionBuilder This => this;
|
||||
|
||||
/// <summary>
|
||||
/// Changes the default weight of a dashboard
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of dashboard</typeparam>
|
||||
/// <param name="weight">The new dashboard weight</param>
|
||||
/// <returns></returns>
|
||||
public DashboardCollectionBuilder SetWeight<T>(int weight) where T : IDashboard
|
||||
{
|
||||
_customWeights[typeof(T)] = weight;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override IEnumerable<IDashboard> CreateItems(IFactory factory)
|
||||
{
|
||||
// get the manifest parser just-in-time - injecting it in the ctor would mean that
|
||||
@@ -47,20 +33,13 @@ namespace Umbraco.Web.Dashboards
|
||||
|
||||
private int GetWeight(IDashboard dashboard)
|
||||
{
|
||||
var typeOfDashboard = dashboard.GetType();
|
||||
if(_customWeights.ContainsKey(typeOfDashboard))
|
||||
{
|
||||
return _customWeights[typeOfDashboard];
|
||||
}
|
||||
|
||||
switch (dashboard)
|
||||
{
|
||||
case ManifestDashboard manifestDashboardDefinition:
|
||||
return manifestDashboardDefinition.Weight;
|
||||
|
||||
default:
|
||||
var weightAttribute = dashboard.GetType().GetCustomAttribute<WeightAttribute>(false);
|
||||
return weightAttribute?.Weight ?? DefaultWeight;
|
||||
return GetWeight(dashboard.GetType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user