Skip to content

Commit 2a9f96a

Browse files
committed
Added type extensions
1 parent 1376cc2 commit 2a9f96a

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Test")]
4+
5+
namespace NetCore.AutoRegisterDi
6+
{
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Reflection;
11+
using Attributes;
12+
using Microsoft.Extensions.DependencyInjection;
13+
14+
15+
/// <summary>
16+
/// Extensions for <see cref="Type"/>
17+
/// </summary>
18+
internal static class TypeExtensions
19+
{
20+
/// <summary>
21+
/// Check if type marked by <see cref="DoNotAutoRegisterAttribute"/>
22+
/// </summary>
23+
/// <param name="type">type</param>
24+
public static bool IsIgnoredType(this Type type)
25+
{
26+
var doNotAutoRegisterAttribute = type.GetCustomAttribute<DoNotAutoRegisterAttribute>();
27+
return doNotAutoRegisterAttribute != null;
28+
}
29+
30+
/// <summary>
31+
/// Check if class marked by multiple lifetime attributes
32+
/// </summary>
33+
/// <param name="type">type</param>
34+
/// <returns></returns>
35+
public static bool IsMultipleLifetime(this Type type)
36+
{
37+
var attributes = type.GetCustomAttributes(true)
38+
.Select(x => x.GetType())
39+
.ToList();
40+
if (attributes.Any())
41+
{
42+
return new List<bool>
43+
{
44+
attributes.Contains(typeof(RegisterAsTransientAttribute)),
45+
attributes.Contains(typeof(RegisterAsScopedAttribute)),
46+
attributes.Contains(typeof(RegisterAsSingletonAttribute))
47+
}.Count(x => x) > 1;
48+
}
49+
50+
return false;
51+
}
52+
53+
/// <summary>
54+
/// Returns service lifetime
55+
/// </summary>
56+
/// <param name="type">type</param>
57+
/// <returns></returns>
58+
public static ServiceLifetime GetTypeLiteTime(this Type type)
59+
{
60+
if (type.GetCustomAttribute<RegisterAsTransientAttribute>() != null)
61+
{
62+
return ServiceLifetime.Transient;
63+
}
64+
65+
if (type.GetCustomAttribute<RegisterAsScopedAttribute>() != null)
66+
{
67+
return ServiceLifetime.Scoped;
68+
}
69+
70+
if (type.GetCustomAttribute<RegisterAsSingletonAttribute>() != null)
71+
{
72+
return ServiceLifetime.Singleton;
73+
}
74+
75+
return ServiceLifetime.Transient;
76+
}
77+
}
78+
}

Test/TestTypeExtensions.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace Test
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using NetCore.AutoRegisterDi;
5+
using NetCore.AutoRegisterDi.Attributes;
6+
using Xunit;
7+
using Xunit.Extensions.AssertExtensions;
8+
9+
public class TestTypeExtensions
10+
{
11+
[DoNotAutoRegister]
12+
private class Ignore
13+
{
14+
}
15+
16+
[RegisterAsScoped]
17+
private class Scope
18+
{
19+
}
20+
21+
[RegisterAsTransient]
22+
private class Transient
23+
{
24+
}
25+
26+
[RegisterAsSingleton]
27+
private class Singleton
28+
{
29+
}
30+
31+
[RegisterAsSingleton]
32+
[RegisterAsTransient]
33+
private class Multiple
34+
{
35+
}
36+
37+
[Fact]
38+
public void IsIgnore()
39+
{
40+
typeof(Ignore).IsIgnoredType()
41+
.ShouldBeTrue();
42+
typeof(Scope).IsIgnoredType()
43+
.ShouldBeFalse();
44+
}
45+
46+
[Fact]
47+
public void IsMultipleLifetime()
48+
{
49+
typeof(Multiple).IsMultipleLifetime()
50+
.ShouldBeTrue();
51+
typeof(Singleton).IsMultipleLifetime()
52+
.ShouldBeFalse();
53+
}
54+
55+
[Fact]
56+
public void GetScopeLifetime()
57+
{
58+
typeof(Scope).GetTypeLiteTime().ShouldEqual(ServiceLifetime.Scoped);
59+
}
60+
61+
[Fact]
62+
public void GetTransientLifetime()
63+
{
64+
typeof(Transient).GetTypeLiteTime().ShouldEqual(ServiceLifetime.Transient);
65+
}
66+
67+
[Fact]
68+
public void GetSingletonLifetime()
69+
{
70+
typeof(Singleton).GetTypeLiteTime().ShouldEqual(ServiceLifetime.Singleton);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)