Skip to content

Commit

Permalink
#449 Making the lazy behavior more consistent
Browse files Browse the repository at this point in the history
Removing probably the outdated conition because all the tests are passing.
  • Loading branch information
dadhi committed Feb 26, 2022
1 parent 2cac52f commit 1799e1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5322,7 +5322,7 @@ public static Expression GetLazyExpressionOrDefault(Request request, bool nullWr
// by resolving the factory we are checking that the service itself is registered...
var serviceFactory = container.ResolveFactory(serviceRequest);
if (serviceFactory == null)
return request.IfUnresolved == IfUnresolved.Throw ? null : Constant(null, lazyType);
return null;
serviceRequest = serviceRequest.WithResolvedFactory(serviceFactory, skipRecursiveDependencyCheck: true);

// But what about its dependencies. In order to check on them we need to get the expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,33 @@ public void Import_AllowDefault_ImportsServiceWithDependencies()
Assert.IsNotNull(x.HardDrive.LogicBoard);
}

// [Test] // doesn't work as expected
public void Import_LazyAllowDefault_DoesntImportServiceWithoutDependencies()
[Test]
public void Import_Lazy_with_AllowDefault_Should_not_check_into_dependencies()
{
var container = new Container().WithMef();
// .With(r => r.WithoutFuncAndLazyWithoutRegistration());
var container = new Container().WithMef()
.With(r => r.WithoutFuncAndLazyWithoutRegistration());

// container.Register<IHardDrive, HitachiHardDrive>();
container.Register<IHardDrive, SamsungHardDrive>();

var x = new Computer3();
container.InjectPropertiesAndFields(x);

//var h = x.HardDrive;
// should be null: couldn't assemble a computer with a hard drive
// because logic board for the hard drive is not registered
Assert.IsNull(x.HardDrive);
Assert.That(x.HardDrive, Is.Not.Null); // it is not null because the registration of HardDrive is present but its dependencies are not checked yet (cause laziness)
Assert.That(x.HardDrive.Value, Is.Null); // it is null because of allow default and we failing to get the dependency
}

[Test]
public void Import_Lazy_with_AllowDefault_Should_not_check_into_Lazy_dependencies_as_well()
{
var container = new Container().WithMef();

container.Register<IHardDrive, HitachiHardDrive>();

var x = new Computer3();
container.InjectPropertiesAndFields(x);

// instead, we currently have this situation:
//Assert.That(x.HardDrive, Is.Not.Null);
//Assert.That(x.HardDrive.Value, Is.Not.Null);
//Assert.That(() => x.HardDrive.Value.LogicBoard, Throws.Exception.TypeOf<NullReferenceException>());
Assert.That(x.HardDrive, Is.Not.Null); // it is not null because the registration of HardDrive is present but its dependencies are not checked yet (cause laziness)
Assert.That(x.HardDrive.Value, Is.Null); // it is null because of allow default and we failing to get the dependency - even if dependency is lazy, we still check the registration
}

public class Computer
Expand Down

0 comments on commit 1799e1e

Please sign in to comment.