Added new factory type
This commit is contained in:
Родитель
d7bd3e2e18
Коммит
32d44f5e68
|
@ -1,25 +0,0 @@
|
||||||
using System;
|
|
||||||
using Unity.Builder;
|
|
||||||
|
|
||||||
namespace Unity.Policy
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a builder policy for mapping build keys.
|
|
||||||
/// </summary>
|
|
||||||
public interface IBuildKeyMappingPolicy
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Maps the build key.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context">Current build context. Used for contextual information
|
|
||||||
/// if writing a more sophisticated mapping. This parameter can be null
|
|
||||||
/// (called when getting container registrations).</param>
|
|
||||||
/// <returns>The new build key.</returns>
|
|
||||||
Type Map(ref BuilderContext context);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Instructs engine to resolve type rather than build it
|
|
||||||
/// </summary>
|
|
||||||
bool RequireBuild { get; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -71,9 +71,4 @@
|
||||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Policy\BuildPlanCreator\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -87,6 +87,14 @@ namespace Unity
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
IUnityContainer IUnityContainer.RegisterType(IEnumerable<Type> interfaces, Type implementationType, string name,
|
||||||
|
LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net461</TargetFramework>
|
<TargetFramework>net461</TargetFramework>
|
||||||
|
<SignAssembly>true</SignAssembly>
|
||||||
|
<AssemblyOriginatorKeyFile>..\..\src\package.snk</AssemblyOriginatorKeyFile>
|
||||||
|
<DelaySign>false</DelaySign>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using Runner.Setup;
|
using Runner.Setup;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Builder;
|
||||||
|
|
||||||
namespace Runner.Tests
|
namespace Runner.Tests
|
||||||
{
|
{
|
||||||
|
@ -15,16 +16,17 @@ namespace Runner.Tests
|
||||||
[IterationSetup]
|
[IterationSetup]
|
||||||
public virtual void SetupContainer()
|
public virtual void SetupContainer()
|
||||||
{
|
{
|
||||||
_container = new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Compiled);
|
||||||
|
|
||||||
_container.RegisterType<Poco>();
|
_container.RegisterType<Poco>();
|
||||||
_container.RegisterType<IFoo, Foo>();
|
_container.RegisterType<IFoo, Foo>();
|
||||||
_container.RegisterType<IFoo, Foo>("1");
|
_container.RegisterType<IFoo, Foo>("1");
|
||||||
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
||||||
|
_container.RegisterType<IFoo>("3", Invoke.Factory((ref BuilderContext c) => new Foo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
||||||
public object IUnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
public object UnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
||||||
|
|
||||||
[Benchmark(Description = "Compiled<object> (unregistered)")]
|
[Benchmark(Description = "Compiled<object> (unregistered)")]
|
||||||
public object Unregistered() => _container.Resolve(typeof(object), null);
|
public object Unregistered() => _container.Resolve(typeof(object), null);
|
||||||
|
@ -35,8 +37,11 @@ namespace Runner.Tests
|
||||||
[Benchmark(Description = "Compiled<IService> (registered)")]
|
[Benchmark(Description = "Compiled<IService> (registered)")]
|
||||||
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
||||||
|
|
||||||
|
[Benchmark(Description = "Compiled<IService> (legacy)")]
|
||||||
|
public object LegacyFactory() => _container.Resolve(typeof(IFoo), "2");
|
||||||
|
|
||||||
[Benchmark(Description = "Compiled<IService> (factory)")]
|
[Benchmark(Description = "Compiled<IService> (factory)")]
|
||||||
public object Factory() => _container.Resolve(typeof(IFoo), "2");
|
public object Factory() => _container.Resolve(typeof(IFoo), "3");
|
||||||
|
|
||||||
[Benchmark(Description = "Compiled<IService[]> (registered)")]
|
[Benchmark(Description = "Compiled<IService[]> (registered)")]
|
||||||
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using Runner.Setup;
|
using Runner.Setup;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Builder;
|
||||||
|
|
||||||
namespace Runner.Tests
|
namespace Runner.Tests
|
||||||
{
|
{
|
||||||
|
@ -15,12 +16,13 @@ namespace Runner.Tests
|
||||||
[IterationSetup]
|
[IterationSetup]
|
||||||
public virtual void SetupContainer()
|
public virtual void SetupContainer()
|
||||||
{
|
{
|
||||||
_container = new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Compiled);
|
||||||
|
|
||||||
_container.RegisterType<Poco>();
|
_container.RegisterType<Poco>();
|
||||||
_container.RegisterType<IFoo, Foo>();
|
_container.RegisterType<IFoo, Foo>();
|
||||||
_container.RegisterType<IFoo, Foo>("1");
|
_container.RegisterType<IFoo, Foo>("1");
|
||||||
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
||||||
|
_container.RegisterType<IFoo>("3", Invoke.Factory((ref BuilderContext c) => new Foo()));
|
||||||
|
|
||||||
for (var i = 0; i < 3; i++)
|
for (var i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
|
@ -29,11 +31,12 @@ namespace Runner.Tests
|
||||||
_container.Resolve<IFoo>();
|
_container.Resolve<IFoo>();
|
||||||
_container.Resolve<IFoo>("1");
|
_container.Resolve<IFoo>("1");
|
||||||
_container.Resolve<IFoo>("2");
|
_container.Resolve<IFoo>("2");
|
||||||
|
_container.Resolve<IFoo>("3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
||||||
public object IUnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
public object UnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
||||||
|
|
||||||
[Benchmark(Description = "PreCompiled<object> (pre-built)")]
|
[Benchmark(Description = "PreCompiled<object> (pre-built)")]
|
||||||
public object Unregistered() => _container.Resolve(typeof(object), null);
|
public object Unregistered() => _container.Resolve(typeof(object), null);
|
||||||
|
@ -44,8 +47,11 @@ namespace Runner.Tests
|
||||||
[Benchmark(Description = "PreCompiled<IService> (pre-built)")]
|
[Benchmark(Description = "PreCompiled<IService> (pre-built)")]
|
||||||
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
||||||
|
|
||||||
[Benchmark(Description = "PreCompiled<IService> (factory)")]
|
[Benchmark(Description = "Compiled<IService> (legacy)")]
|
||||||
public object Factory() => _container.Resolve(typeof(IFoo), "2");
|
public object LegacyFactory() => _container.Resolve(typeof(IFoo), "2");
|
||||||
|
|
||||||
|
[Benchmark(Description = "Compiled<IService> (factory)")]
|
||||||
|
public object Factory() => _container.Resolve(typeof(IFoo), "3");
|
||||||
|
|
||||||
[Benchmark(Description = "PreCompiled<IService[]> (pre-built)")]
|
[Benchmark(Description = "PreCompiled<IService[]> (pre-built)")]
|
||||||
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using Runner.Setup;
|
using Runner.Setup;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Builder;
|
||||||
|
|
||||||
namespace Runner.Tests
|
namespace Runner.Tests
|
||||||
{
|
{
|
||||||
|
@ -15,12 +16,13 @@ namespace Runner.Tests
|
||||||
[IterationSetup]
|
[IterationSetup]
|
||||||
public virtual void SetupContainer()
|
public virtual void SetupContainer()
|
||||||
{
|
{
|
||||||
_container = new UnityContainer(UnityContainer.BuildStrategy.Resolved);
|
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Resolved);
|
||||||
|
|
||||||
_container.RegisterType<Poco>();
|
_container.RegisterType<Poco>();
|
||||||
_container.RegisterType<IFoo, Foo>();
|
_container.RegisterType<IFoo, Foo>();
|
||||||
_container.RegisterType<IFoo, Foo>("1");
|
_container.RegisterType<IFoo, Foo>("1");
|
||||||
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
||||||
|
_container.RegisterType<IFoo>("3", Invoke.Factory((ref BuilderContext c) => new Foo()));
|
||||||
|
|
||||||
for (var i = 0; i < 3; i++)
|
for (var i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
|
@ -29,11 +31,12 @@ namespace Runner.Tests
|
||||||
_container.Resolve<IFoo>();
|
_container.Resolve<IFoo>();
|
||||||
_container.Resolve<IFoo>("1");
|
_container.Resolve<IFoo>("1");
|
||||||
_container.Resolve<IFoo>("2");
|
_container.Resolve<IFoo>("2");
|
||||||
|
_container.Resolve<IFoo>("3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
||||||
public object IUnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
public object UnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
||||||
|
|
||||||
[Benchmark(Description = "PreResolved<object> (optimized)")]
|
[Benchmark(Description = "PreResolved<object> (optimized)")]
|
||||||
public object Unregistered() => _container.Resolve(typeof(object), null);
|
public object Unregistered() => _container.Resolve(typeof(object), null);
|
||||||
|
@ -44,8 +47,11 @@ namespace Runner.Tests
|
||||||
[Benchmark(Description = "PreResolved<IService> (optimized)")]
|
[Benchmark(Description = "PreResolved<IService> (optimized)")]
|
||||||
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
||||||
|
|
||||||
[Benchmark(Description = "PreResolved<IService> (factory)")]
|
[Benchmark(Description = "Compiled<IService> (legacy)")]
|
||||||
public object Factory() => _container.Resolve(typeof(IFoo), "2");
|
public object LegacyFactory() => _container.Resolve(typeof(IFoo), "2");
|
||||||
|
|
||||||
|
[Benchmark(Description = "Compiled<IService> (factory)")]
|
||||||
|
public object Factory() => _container.Resolve(typeof(IFoo), "3");
|
||||||
|
|
||||||
[Benchmark(Description = "PreResolved<IService[]> (optimized)")]
|
[Benchmark(Description = "PreResolved<IService[]> (optimized)")]
|
||||||
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using BenchmarkDotNet.Attributes;
|
using System;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
using Runner.Setup;
|
using Runner.Setup;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
@ -19,7 +20,7 @@ namespace Runner.Tests
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Description = "Register (No Mapping)")]
|
[Benchmark(Description = "Register (No Mapping)")]
|
||||||
public object Register() => _container.RegisterType(null, typeof(object), null, null);
|
public object Register() => _container.RegisterType((Type)null, typeof(object), null, null);
|
||||||
|
|
||||||
[Benchmark(Description = "Register Mapping")]
|
[Benchmark(Description = "Register Mapping")]
|
||||||
public object RegisterMapping() => _container.RegisterType(typeof(IFoo), typeof(Foo), null, null);
|
public object RegisterMapping() => _container.RegisterType(typeof(IFoo), typeof(Foo), null, null);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using Runner.Setup;
|
using Runner.Setup;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Unity;
|
using Unity;
|
||||||
|
using Unity.Builder;
|
||||||
|
|
||||||
namespace Runner.Tests
|
namespace Runner.Tests
|
||||||
{
|
{
|
||||||
|
@ -15,16 +16,17 @@ namespace Runner.Tests
|
||||||
[IterationSetup]
|
[IterationSetup]
|
||||||
public virtual void SetupContainer()
|
public virtual void SetupContainer()
|
||||||
{
|
{
|
||||||
_container = new UnityContainer(UnityContainer.BuildStrategy.Resolved);
|
_container = new UnityContainer(Unity.UnityContainer.BuildStrategy.Resolved);
|
||||||
|
|
||||||
_container.RegisterType<Poco>();
|
_container.RegisterType<Poco>();
|
||||||
_container.RegisterType<IFoo, Foo>();
|
_container.RegisterType<IFoo, Foo>();
|
||||||
_container.RegisterType<IFoo, Foo>("1");
|
_container.RegisterType<IFoo, Foo>("1");
|
||||||
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
_container.RegisterType<IFoo>("2", Invoke.Factory(c => new Foo()));
|
||||||
|
_container.RegisterType<IFoo>("3", Invoke.Factory((ref BuilderContext c) => new Foo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
[Benchmark(Description = "Resolve<IUnityContainer> ")]
|
||||||
public object IUnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
public object UnityContainer() => _container.Resolve(typeof(IUnityContainer), null);
|
||||||
|
|
||||||
[Benchmark(Description = "Resolved<object> (unregistered)")]
|
[Benchmark(Description = "Resolved<object> (unregistered)")]
|
||||||
public object Unregistered() => _container.Resolve(typeof(object), null);
|
public object Unregistered() => _container.Resolve(typeof(object), null);
|
||||||
|
@ -35,8 +37,11 @@ namespace Runner.Tests
|
||||||
[Benchmark(Description = "Resolved<IService> (registered)")]
|
[Benchmark(Description = "Resolved<IService> (registered)")]
|
||||||
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
public object Mapping() => _container.Resolve(typeof(IFoo), null);
|
||||||
|
|
||||||
[Benchmark(Description = "Resolved<IService> (factory)")]
|
[Benchmark(Description = "Compiled<IService> (legacy)")]
|
||||||
public object Factory() => _container.Resolve(typeof(IFoo), "2");
|
public object LegacyFactory() => _container.Resolve(typeof(IFoo), "2");
|
||||||
|
|
||||||
|
[Benchmark(Description = "Compiled<IService> (factory)")]
|
||||||
|
public object Factory() => _container.Resolve(typeof(IFoo), "3");
|
||||||
|
|
||||||
[Benchmark(Description = "Resolved<IService[]> (registered)")]
|
[Benchmark(Description = "Resolved<IService[]> (registered)")]
|
||||||
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
public object Array() => _container.Resolve(typeof(IFoo[]), null);
|
||||||
|
|
|
@ -326,8 +326,8 @@ namespace Unity.Tests.v5.Lifetime
|
||||||
|
|
||||||
var aInstance = new EmailService();
|
var aInstance = new EmailService();
|
||||||
|
|
||||||
uc.RegisterType(null, typeof(EmailService), null, new ContainerControlledLifetimeManager(), null);
|
uc.RegisterType((Type)null, typeof(EmailService), null, new ContainerControlledLifetimeManager(), null);
|
||||||
uc.RegisterType(null, typeof(EmailService), "SetA", new ContainerControlledLifetimeManager(), null);
|
uc.RegisterType((Type)null, typeof(EmailService), "SetA", new ContainerControlledLifetimeManager(), null);
|
||||||
uc.RegisterInstance(aInstance);
|
uc.RegisterInstance(aInstance);
|
||||||
uc.RegisterInstance("hello", aInstance);
|
uc.RegisterInstance("hello", aInstance);
|
||||||
uc.RegisterInstance("hello", aInstance, new ExternallyControlledLifetimeManager());
|
uc.RegisterInstance("hello", aInstance, new ExternallyControlledLifetimeManager());
|
||||||
|
|
Загрузка…
Ссылка в новой задаче