-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds AddTransient overload that accepts a value
- Loading branch information
1 parent
906b9df
commit 1c97333
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using Reflex.Core; | ||
|
||
namespace Reflex.Tests | ||
{ | ||
public class TransientValueResolverTests | ||
{ | ||
[Test] | ||
public void AddTransientFromValue_FirstResolve_ShouldReturnValue() | ||
{ | ||
var container = new ContainerDescriptor("") | ||
.AddTransient(42) | ||
.Build(); | ||
|
||
container.Resolve<int>().Should().Be(42); | ||
} | ||
|
||
[Test] | ||
public void AddValueType_AsTransientFromValue_SecondResolve_ShouldThrow() | ||
{ | ||
var container = new ContainerDescriptor("") | ||
.AddTransient(42) | ||
.Build(); | ||
|
||
Action resolve = () => container.Resolve<int>(); | ||
|
||
resolve.Should().NotThrow(); | ||
resolve.Should().Throw<Exception>(); | ||
} | ||
|
||
[Test] | ||
public void AddReferenceType_AsTransientFromValue_SecondResolve_ShouldThrow() | ||
{ | ||
var container = new ContainerDescriptor("") | ||
.AddTransient(string.Empty) | ||
.Build(); | ||
|
||
Action resolve = () => container.Resolve<string>(); | ||
|
||
resolve.Should().NotThrow(); | ||
resolve.Should().Throw<Exception>(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using Reflex.Core; | ||
using Reflex.Enums; | ||
|
||
namespace Reflex.Resolvers | ||
{ | ||
internal sealed class TransientValueResolver : Resolver | ||
{ | ||
private object _value; | ||
|
||
public TransientValueResolver(object value) | ||
{ | ||
RegisterCallSite(); | ||
_value = value; | ||
Disposables.TryAdd(value); | ||
Concrete = _value.GetType(); | ||
Lifetime = Lifetime.Transient; | ||
} | ||
|
||
public override object Resolve(Container container) | ||
{ | ||
IncrementResolutions(); | ||
|
||
if (_value == null) | ||
{ | ||
throw new Exception("Trying to resolve a second time from a TransientValueResolver"); | ||
} | ||
|
||
var value = _value; | ||
_value = null; | ||
return value; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters