Skip to content

Commit

Permalink
Changing Map Strategy to resolve in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Nov 17, 2017
1 parent e686e02 commit 7dbdb11
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/ObjectBuilder/Policies/ResolveMappingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Unity.Builder;
using Unity.Policy;

namespace Unity.ObjectBuilder.Policies
{
public class ResolveMappingPolicy : BuildKeyMappingPolicy, IDependencyResolverPolicy
{
public ResolveMappingPolicy(NamedTypeBuildKey newBuildKey)
: base(newBuildKey)
{

}

public object Resolve(IBuilderContext context)
{
return context.NewBuildUp(Map(context.BuildKey, context));

}
}
}
16 changes: 5 additions & 11 deletions src/ObjectBuilder/Strategies/BuildKeyMappingStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System;
using Unity.Builder;
using Unity.Builder.Strategy;
using Unity.Lifetime;
using Unity.Policy;

namespace Unity.ObjectBuilder.Strategies
Expand All @@ -21,22 +19,18 @@ public class BuildKeyMappingStrategy : BuilderStrategy
public override void PreBuildUp(IBuilderContext context)
{
var policy = context.Policies.Get<IBuildKeyMappingPolicy>(context.BuildKey);
if (null == policy) return;

if (policy != null)
{
context.BuildKey = policy.Map(context.BuildKey, context);
}

if (context.BuildKey == context.OriginalBuildKey ||
0 < ((PolicyList)context.Policies).Count) return;

ILifetimePolicy lifetimePolicy = context.Policies.Get<ILifetimePolicy>(context.BuildKey, out _);
var existing = lifetimePolicy?.GetValue();
var existing = (policy as IDependencyResolverPolicy)?.Resolve(context);
if (existing != null)
{
context.Existing = existing;
context.BuildComplete = true;
return;
}

context.BuildKey = policy.Map(context.BuildKey, context);
}
}
}
8 changes: 5 additions & 3 deletions src/UnityContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Unity.Policy;
using Unity.Registration;
using Unity.Resolution;
using Unity.ResolverPolicy;

namespace Unity
{
Expand Down Expand Up @@ -86,8 +85,11 @@ public IUnityContainer RegisterType(Type typeFrom, Type typeTo, string name, Lif
}
else
{
_policies.Set<IBuildKeyMappingPolicy>(new BuildKeyMappingPolicy(new NamedTypeBuildKey(to, name)),
new NamedTypeBuildKey(typeFrom, name));
var policy = (null != injectionMembers && injectionMembers.Length > 0) || lifetimeManager is IRequireBuildUpPolicy
? new BuildKeyMappingPolicy(new NamedTypeBuildKey(to, name))
: new ResolveMappingPolicy(new NamedTypeBuildKey(to, name));

_policies.Set<IBuildKeyMappingPolicy>(policy, new NamedTypeBuildKey(typeFrom, name));
}
}
if (lifetimeManager != null)
Expand Down
8 changes: 3 additions & 5 deletions tests/Unity.Tests/CodeplexIssuesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ public void CanUseNonDefaultLifetimeManagerWithOpenGenericRegistration()
public void CanOverrideGenericLifetimeManagerWithSpecificOne()
{
IUnityContainer container = new UnityContainer()
.RegisterType(typeof(ISomeInterface<>),
typeof(MyTypeImplementingSomeInterface<>),
new ContainerControlledLifetimeManager())
.RegisterType(typeof(ISomeInterface<>), typeof(MyTypeImplementingSomeInterface<>), new ContainerControlledLifetimeManager())
.RegisterType(typeof(MyTypeImplementingSomeInterface<double>), new TransientLifetimeManager());

ISomeInterface<string> string1 = container.Resolve<ISomeInterface<string>>();
ISomeInterface<string> string2 = container.Resolve<ISomeInterface<string>>();

ISomeInterface<double> double1 = container.Resolve<ISomeInterface<double>>();
ISomeInterface<double> double2 = container.Resolve<ISomeInterface<double>>();
var double1 = container.Resolve<MyTypeImplementingSomeInterface<double>>();
var double2 = container.Resolve<MyTypeImplementingSomeInterface<double>>();

Assert.AreSame(string1, string2);
Assert.AreNotSame(double1, double2);
Expand Down
45 changes: 31 additions & 14 deletions tests/Unity.Tests/GitHubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@
using Unity;
using Unity.Attributes;
using Unity.Exceptions;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Tests.TestObjects;
using UnityContainer = Unity.UnityContainer;

namespace GitHub
{
[TestClass]
public class Issues
{

[TestMethod]
public void unity_154_5()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<OtherEmailService>(new ContainerControlledLifetimeManager());
container.RegisterType<IService, OtherEmailService>();
container.RegisterType<IOtherService, OtherEmailService>(new InjectionConstructor(container));

Assert.AreNotSame(container.Resolve<IService>(),
container.Resolve<IOtherService>());
}


[TestMethod]
public void unity_154()
{
Expand All @@ -20,7 +36,8 @@ public void unity_154()
container.RegisterType<IService, OtherEmailService>();
container.RegisterType<IOtherService, OtherEmailService>();

Assert.AreSame(container.Resolve<IService>(), container.Resolve<IOtherService>());
Assert.AreSame(container.Resolve<IService>(),
container.Resolve<IOtherService>());
}


Expand All @@ -39,23 +56,23 @@ public void unity_153()
}
}



[TestMethod]
public void Issue_35()
{
IUnityContainer container = new UnityContainer();

container.RegisterType<ILogger, MockLogger>(new ContainerControlledLifetimeManager());
ILogger logger = container.Resolve<ILogger>();
[TestMethod]
public void Issue_35()
{
IUnityContainer container = new UnityContainer();

Assert.IsNotNull(logger);
Assert.AreSame(container.Resolve<ILogger>(), logger);
container.RegisterType<ILogger, MockLogger>(new ContainerControlledLifetimeManager());
ILogger logger = container.Resolve<ILogger>();

container.RegisterType<MockLogger>(new TransientLifetimeManager());
Assert.IsNotNull(logger);
Assert.AreSame(container.Resolve<ILogger>(), logger);

container.RegisterType<MockLogger>(new TransientLifetimeManager());

Assert.AreSame(container.Resolve<ILogger>(), logger);
}

Assert.AreSame(container.Resolve<ILogger>(), logger);
}
[TestMethod]
public void GitHub_Issue_88() // https://github.com/unitycontainer/unity/issues/88
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Unity.Tests/TestObjects/EmailService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System;
using Unity.Attributes;

namespace Unity.Tests.TestObjects
{
Expand All @@ -19,6 +20,18 @@ public void Dispose()
// A dummy class to support testing type mapping
public class OtherEmailService : IService, IOtherService, IDisposable
{
[InjectionConstructor]
public OtherEmailService()
{

}

public OtherEmailService(IUnityContainer container)
{

}


public bool Disposed = false;
public void Dispose()
{
Expand Down

0 comments on commit 7dbdb11

Please sign in to comment.