Skip to content

Commit

Permalink
Fix singleton binding when ResolutionMode is RETURN_NULL [Fix #54]
Browse files Browse the repository at this point in the history
  • Loading branch information
intentor committed Aug 12, 2016
1 parent aff6ece commit 8cd90cb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/Assets/Adic/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Copyright (c) 2014-2016 André "Intentor" Martins
http://intentor.com.br/
------------------------------------------------

Version 2.20.2 (2016-08-11)

Framework
- Fix singleton binding when ResolutionMode is RETURN_NULL. [Issue #54]

Version 2.20.1 (2016-08-03)

Commander extension
Expand Down
33 changes: 17 additions & 16 deletions src/Assets/Adic/Scripts/Framework/Injection/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Injector(IReflectionCache cache, IBinder binder, ResolutionMode resolutio
/// <typeparam name="T">Type to be resolved.</typeparam>
/// <returns>The instance or NULL.</returns>
public T Resolve<T>() {
return (T)this.Resolve(typeof(T), InjectionMember.None, null, null);
return (T)this.Resolve(typeof(T), InjectionMember.None, null, null, false);
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ public T Resolve<T>() {
/// <param name="identifier">Identifier to look for.</param>
/// <returns>The instance or NULL.</returns>
public T Resolve<T>(object identifier) {
return (T)this.Resolve(typeof(T), InjectionMember.None, null, identifier);
return (T)this.Resolve(typeof(T), InjectionMember.None, null, identifier, false);
}

/// <summary>
Expand All @@ -76,7 +76,7 @@ public T Resolve<T>(object identifier) {
/// <param name="type">Type to be resolved.</param>
/// <returns>The instance or NULL.</returns>
public object Resolve(Type type) {
return this.Resolve(type, InjectionMember.None, null, null);
return this.Resolve(type, InjectionMember.None, null, null, false);
}

/// <summary>
Expand All @@ -89,7 +89,7 @@ public object Resolve(Type type) {
/// <returns>The instance or NULL.</returns>
public object Resolve(object identifier) {
//Given no type will be passed, it'll always resolve an array.
var instances = (object[])this.Resolve(null, InjectionMember.None, null, identifier);
var instances = (object[])this.Resolve(null, InjectionMember.None, null, identifier, false);

if (instances != null && instances.Length > 0) {
return instances[0];
Expand All @@ -108,7 +108,7 @@ public object Resolve(object identifier) {
/// <param name="identifier">Identifier to look for.</param>
/// <returns>The instance or NULL.</returns>
public object Resolve(Type type, object identifier) {
return this.Resolve(type, InjectionMember.None, null, identifier);
return this.Resolve(type, InjectionMember.None, null, identifier, false);
}

/// <summary>
Expand Down Expand Up @@ -185,7 +185,8 @@ public object[] ResolveAll(Type type, object identifier) {
/// <param name="member">Member for which the binding is being resolved.</param>
/// <param name="parentInstance">Parent object in which the resolve is occuring.</param>
/// <param name="identifier">The binding identifier to be looked for.</param>
protected object Resolve(Type type, InjectionMember member, object parentInstance, object identifier) {
/// <param name="alwaysResolve">Always resolve the type, even when resolution mode is null.</param>
protected object Resolve(Type type, InjectionMember member, object parentInstance, object identifier, bool alwaysResolve) {
object resolution = null;

if (this.beforeResolve != null) {
Expand Down Expand Up @@ -227,7 +228,7 @@ protected object Resolve(Type type, InjectionMember member, object parentInstanc
IList<object> instances = new List<object>();

if (bindings == null) {
if (this.resolutionMode == ResolutionMode.ALWAYS_RESOLVE) {
if (alwaysResolve || this.resolutionMode == ResolutionMode.ALWAYS_RESOLVE) {
instances.Add(this.Instantiate(type as Type));
} else {
return null;
Expand Down Expand Up @@ -331,7 +332,7 @@ protected object Inject(object instance, ReflectedClass reflectedClass) {
protected void InjectFields(object instance, SetterInfo[] fields) {
for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++) {
var field = fields[fieldIndex];
var valueToSet = this.Resolve(field.type, InjectionMember.Field, instance, field.identifier);
var valueToSet = this.Resolve(field.type, InjectionMember.Field, instance, field.identifier, false);
field.setter(instance, valueToSet);
}
}
Expand All @@ -344,7 +345,8 @@ protected void InjectFields(object instance, SetterInfo[] fields) {
protected void InjectProperties(object instance, SetterInfo[] properties) {
for (int propertyIndex = 0; propertyIndex < properties.Length; propertyIndex++) {
var property = properties[propertyIndex];
var valueToSet = this.Resolve(property.type, InjectionMember.Property, instance, property.identifier);
var valueToSet = this.Resolve(property.type, InjectionMember.Property, instance, property.identifier,
false);
property.setter(instance, valueToSet);
}
}
Expand Down Expand Up @@ -485,12 +487,8 @@ protected object[] GetParametersFromInfo(object instance, ParameterInfo[] parame
for (int paramIndex = 0; paramIndex < parameters.Length; paramIndex++) {
var parameterInfo = parametersInfo[paramIndex];

parameters[paramIndex] = this.Resolve(
parameterInfo.type,
InjectionMember.Constructor,
instance,
parameterInfo.identifier
);
parameters[paramIndex] = this.Resolve(parameterInfo.type, InjectionMember.Constructor, instance,
parameterInfo.identifier, false);
}

return parameters;
Expand All @@ -517,6 +515,7 @@ protected void OnBeforeAddBinding(IBinder source, ref BindingInfo binding) {
var valueTypeIsTheSame =
isSingleton &&
bindingIsType &&
bindingFromBinder.value != null &&
bindingFromBinder.value.GetType().Equals(binding.value);
var valueInstanceIsTheSame =
isSingleton &&
Expand All @@ -534,8 +533,10 @@ protected void OnBeforeAddBinding(IBinder source, ref BindingInfo binding) {
if (existingBinding != null) {
binding.value = existingBinding.value;
} else {

if (bindingIsType) {
var value = this.Resolve(binding.value as Type);
//Force resolution to prevent returning null on ResolutionMode.RETURN_NULL.
var value = this.Resolve(binding.value as Type, InjectionMember.None, null, null, true);
binding.value = value;
} else {
this.Inject(binding.value);
Expand Down
13 changes: 13 additions & 0 deletions src/Assets/Tests/Editor/InjectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,18 @@ public void TestResolutionModeReturnNullNotBound() {

Assert.IsNull(instance);
}

[Test]
public void TestResolutionModeReturnNullSingleton() {
var container = new InjectionContainer(ResolutionMode.RETURN_NULL);
container.Bind<IMockInterface>().ToSingleton<MockIClassWithAttributes>();

var instance = container.Resolve<IMockInterface>();

Assert.IsNotNull(instance);
Assert.AreEqual(typeof(MockIClassWithAttributes), instance.GetType());
Assert.Null(((MockIClassWithAttributes)instance).field4);
Assert.Null(((MockIClassWithAttributes)instance).property4);
}
}
}

0 comments on commit 8cd90cb

Please sign in to comment.