Skip to content

Commit

Permalink
Merge branch 'v8/contrib' into v8/dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Umbraco.Web.UI.Client/package-lock.json
  • Loading branch information
nul800sebastiaan committed May 26, 2020
2 parents 76e9d7b + 6da69f1 commit 78a7785
Show file tree
Hide file tree
Showing 27 changed files with 543 additions and 162 deletions.
20 changes: 17 additions & 3 deletions src/Umbraco.Core/FactoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Umbraco.Core.Composing;
Expand Down Expand Up @@ -77,15 +78,28 @@ public static object CreateInstance(this IFactory factory, Type type, params obj

var ctorParameters = ctor.GetParameters();
var ctorArgs = new object[ctorParameters.Length];
var availableArgs = new List<object>(args);
var i = 0;
foreach (var parameter in ctorParameters)
{
// no! IsInstanceOfType is not ok here
// ReSharper disable once UseMethodIsInstanceOfType
var arg = args?.FirstOrDefault(a => parameter.ParameterType.IsAssignableFrom(a.GetType()));
ctorArgs[i++] = arg ?? factory.GetInstance(parameter.ParameterType);
var idx = availableArgs.FindIndex(a => parameter.ParameterType.IsAssignableFrom(a.GetType()));
if(idx >= 0)
{
// Found a suitable supplied argument
ctorArgs[i++] = availableArgs[idx];

// A supplied argument can be used at most once
availableArgs.RemoveAt(idx);
}
else
{
// None of the provided arguments is suitable: get an instance from the factory
ctorArgs[i++] = factory.GetInstance(parameter.ParameterType);
}
}
return ctor.Invoke(ctorArgs);
}
}
}
}
12 changes: 12 additions & 0 deletions src/Umbraco.Core/Models/RelationTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Umbraco.Core.Models
{
internal static class RelationTypeExtensions
{
internal static bool IsSystemRelationType(this IRelationType relationType) =>
relationType.Alias == Constants.Conventions.RelationTypes.RelatedDocumentAlias
|| relationType.Alias == Constants.Conventions.RelationTypes.RelatedMediaAlias
|| relationType.Alias == Constants.Conventions.RelationTypes.RelateDocumentOnCopyAlias
|| relationType.Alias == Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias
|| relationType.Alias == Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
}
}
14 changes: 13 additions & 1 deletion src/Umbraco.Core/Runtime/SqlMainDomLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Dtos;
Expand All @@ -16,7 +18,7 @@ namespace Umbraco.Core.Runtime
internal class SqlMainDomLock : IMainDomLock
{
private string _lockId;
private const string MainDomKey = "Umbraco.Core.Runtime.SqlMainDom";
private const string MainDomKeyPrefix = "Umbraco.Core.Runtime.SqlMainDom";
private const string UpdatedSuffix = "_updated";
private readonly ILogger _logger;
private IUmbracoDatabase _db;
Expand Down Expand Up @@ -126,6 +128,16 @@ public Task ListenAsync()

}

/// <summary>
/// Returns the keyvalue table key for the current server/app
/// </summary>
/// <remarks>
/// The key is the the normal MainDomId which takes into account the AppDomainAppId and the physical file path of the app and this is
/// combined with the current machine name. The machine name is required because the default semaphore lock is machine wide so it implicitly
/// takes into account machine name whereas this needs to be explicitly per machine.
/// </remarks>
private string MainDomKey { get; } = MainDomKeyPrefix + "-" + (NetworkHelper.MachineName + MainDom.GetMainDomId()).GenerateHash<SHA1>();

private void ListeningLoop()
{
while (true)
Expand Down
1 change: 1 addition & 0 deletions src/Umbraco.Core/Umbraco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Models\ContentDataIntegrityReportEntry.cs" />
<Compile Include="Models\ContentDataIntegrityReportOptions.cs" />
<Compile Include="Models\InstallLog.cs" />
<Compile Include="Models\RelationTypeExtensions.cs" />
<Compile Include="Persistence\Repositories\IInstallationRepository.cs" />
<Compile Include="Persistence\Repositories\Implement\InstallationRepository.cs" />
<Compile Include="Services\Implement\InstallationService.cs" />
Expand Down
30 changes: 30 additions & 0 deletions src/Umbraco.Tests/Composing/ContainerConformingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ public void CanRegisterSingletonWithCreate()
Assert.AreSame(s1, s2);
}

[Test]
public void CanRegisterMultipleSameTypeParametersWithCreateInstance()
{
var register = GetRegister();

register.Register<Thing4>(c =>
{
const string param1 = "param1";
const string param2 = "param2";

return c.CreateInstance<Thing4>(param1, param2);
});

var factory = register.CreateFactory();
var instance = factory.GetInstance<Thing4>();
Assert.AreNotEqual(instance.Thing, instance.AnotherThing);
}

public interface IThing { }

public abstract class ThingBase : IThing { }
Expand All @@ -352,5 +370,17 @@ public NeedThings(IEnumerable<ThingBase> things)

public IEnumerable<ThingBase> Things { get; }
}

public class Thing4 : ThingBase
{
public readonly string Thing;
public readonly string AnotherThing;

public Thing4(string thing, string anotherThing)
{
Thing = thing;
AnotherThing = anotherThing;
}
}
}
}
Loading

0 comments on commit 78a7785

Please sign in to comment.