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

Added LifeSpanAttribute #514

Merged
merged 1 commit into from
Oct 16, 2019
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
34 changes: 34 additions & 0 deletions src/LightInject.Tests/LifeSpanTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if NETCOREAPP2_0

using System;
using System.Linq;
using Xunit;


namespace LightInject.Tests
{
public class LifeSpanTests
{
[Fact]
public void ShouldGetLifeSpans()
{
Assert.Equal(10, GetLifeSpan(typeof(PerRequestLifeTime)));
}


private int GetLifeSpan(Type lifetimeType)
{
var attribute = (LifeSpanAttribute)lifetimeType.GetCustomAttributes(typeof(LifeSpanAttribute), true).FirstOrDefault();

if (attribute == null)
{
throw new InvalidOperationException($"The lifetime {lifetimeType} is not decorated with the LifeSpanAttribute");
}

return attribute.Value;
}

}
}

#endif
2 changes: 1 addition & 1 deletion src/LightInject.Tests/LightInject.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.6.0">
<PackageReference Include="coverlet.msbuild" Version="2.7.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
24 changes: 24 additions & 0 deletions src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5973,6 +5973,7 @@ public override string ToString()
/// <summary>
/// Ensures that only one instance of a given service can exist within the current <see cref="IServiceContainer"/>.
/// </summary>
[LifeSpan(30)]
public class PerContainerLifetime : ILifetime, IDisposable, ICloneableLifeTime
{
private readonly object syncRoot = new object();
Expand Down Expand Up @@ -6018,6 +6019,7 @@ public ILifetime Clone()
/// <summary>
/// Ensures that a new instance is created for each request in addition to tracking disposable instances.
/// </summary>
[LifeSpan(10)]
public class PerRequestLifeTime : ILifetime, ICloneableLifeTime
{
/// <inheritdoc/>
Expand All @@ -6043,6 +6045,7 @@ public ILifetime Clone()
/// If the service instance implements <see cref="IDisposable"/>,
/// it will be disposed when the <see cref="Scope"/> ends.
/// </remarks>
[LifeSpan(20)]
public class PerScopeLifetime : ILifetime, ICloneableLifeTime
{
private readonly ThreadSafeDictionary<Scope, object> instances = new ThreadSafeDictionary<Scope, object>();
Expand Down Expand Up @@ -6327,6 +6330,27 @@ public int GetHashCode(T obj)
}
}

/// <summary>
/// Use to indicate the lifespan of a given <see cref="ILifetime"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class LifeSpanAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="LifeSpanAttribute"/> class.
/// </summary>
/// <param name="value">A value that indicates the lifespan of a given <see cref="ILifetime"/>.</param>
public LifeSpanAttribute(int value)
{
Value = value;
}

/// <summary>
/// Gets a value that indicates the lifespan of a given <see cref="ILifetime"/>.
/// </summary>
public int Value { get; }
}

/// <summary>
/// Used at the assembly level to describe the composition root(s) for the target assembly.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/LightInject/LightInject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<!-- <TargetFrameworks>net46;netstandard1.1;netstandard1.3;netstandard1.6;netstandard2.0;netcoreapp2.0;net452</TargetFrameworks> -->
<TargetFrameworks>netcoreapp2.0;netstandard2.0;netstandard1.6;netstandard1.3;netstandard1.1;net46;net452</TargetFrameworks>
<Version>6.1.0</Version>
<Version>6.2.0</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://www.lightinject.net</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand Down