Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non transient value type error #163

Merged
merged 8 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Jab.FunctionalTests.Common/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,21 @@ public void SupportsNamedServices()
internal partial class SupportsNamedServicesContainer
{
}

[Fact]
public void SupportsNoneTransientValueType()
{
SupportsNoneTransientValueTypeContainer c = new();
Assert.IsType<int>(c.GetService<int>());
Assert.IsType<float>(c.GetService<float>());
}

[ServiceProvider]
[Scoped(typeof(int))]
[Singleton(typeof(float))]
internal partial class SupportsNoneTransientValueTypeContainer
{
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/Jab/ContainerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ private void GenerateCallSiteWithCache(CodeWriter codeWriter, string rootReferen
if (serviceCallSite.Lifetime != ServiceLifetime.Transient)
{
var cacheLocation = GetCacheLocation(serviceCallSite.Identity);
codeWriter.Line($"if ({cacheLocation} == default)");
codeWriter.Line($"if ({cacheLocation} == null)");
codeWriter.Line($"lock (this)");
using (codeWriter.Scope($"if ({cacheLocation} == default)"))
using (codeWriter.Scope($"if ({cacheLocation} == null)"))
{
GenerateCallSite(
codeWriter,
Expand All @@ -38,7 +38,14 @@ private void GenerateCallSiteWithCache(CodeWriter codeWriter, string rootReferen
});
}

valueCallback(codeWriter, w => w.Append($"{cacheLocation}"));
if (serviceCallSite.ImplementationType.IsValueType)
{
valueCallback(codeWriter, w => w.Append($"{cacheLocation}.Value"));
}
else
{
valueCallback(codeWriter, w => w.Append($"{cacheLocation}"));
}
}
else if (serviceCallSite.IsDisposable != false)
{
Expand Down
Loading