Skip to content

Commit

Permalink
Non transient value type error (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonTalmi authored Dec 23, 2023
1 parent 97cc56a commit f8adaef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit f8adaef

Please sign in to comment.