Skip to content

Commit 336bf36

Browse files
committed
Version 2.0.0 - attributes to define ServiceLifetime of a class
1 parent 2e98718 commit 336bf36

24 files changed

+215
-138
lines changed

NetCore.AutoRegisterDi/Attributes/DoNotAutoRegisterAttribute.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

NetCore.AutoRegisterDi/Attributes/RegisterAsScopedAttribute.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

NetCore.AutoRegisterDi/Attributes/RegisterAsSingletonAttribute.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

NetCore.AutoRegisterDi/Attributes/RegisterAsTransientAttribute.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

NetCore.AutoRegisterDi/AutoRegisterHelpers.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith)
2-
// Licensed under MIT licence. See License.txt in the project root for license information.
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
34

45
using System;
56
using System.Linq;
@@ -49,8 +50,10 @@ public static AutoRegisterData Where(this AutoRegisterData autoRegData, Func<Typ
4950
/// This registers the classes against any public interfaces (other than IDisposable) implemented by the class
5051
/// </summary>
5152
/// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
53+
/// <param name="lifetime">Allows you to define the lifetime of the service - defaults to ServiceLifetime.Transient</param>
5254
/// <returns></returns>
53-
public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData)
55+
public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
56+
ServiceLifetime lifetime = ServiceLifetime.Transient)
5457
{
5558
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
5659
foreach (var classType in (autoRegData.TypeFilter == null
@@ -61,10 +64,9 @@ public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegister
6164
throw new ArgumentException($"Class {classType.FullName} has multiple life time attributes");
6265

6366
var interfaces = classType.GetTypeInfo().ImplementedInterfaces;
64-
var lifetime = classType.GetTypeLiteTime();
6567
foreach (var infc in interfaces.Where(i => i != typeof(IDisposable) && i.IsPublic && !i.IsNested))
6668
{
67-
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetime));
69+
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, classType.GetLifetimeForClass(lifetime)));
6870
}
6971
}
7072

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
4+
5+
using System;
6+
7+
namespace NetCore.AutoRegisterDi
8+
{
9+
/// <summary>
10+
/// Attribute for marking classes which no need to register in container
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Class)]
13+
public class DoNotAutoRegisterAttribute : Attribute
14+
{}
15+
}

NetCore.AutoRegisterDi/NetCore.AutoRegisterDi.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<PackageVersion>1.1.0</PackageVersion>
7-
<Version>1.1.0</Version>
8-
<AssemblyVersion>1.1.0.0</AssemblyVersion>
9-
<FileVersion>1.1.0.0</FileVersion>
6+
<PackageVersion>2.0.0</PackageVersion>
7+
<Version>2.0.0</Version>
8+
<AssemblyVersion>2.0.0.0</AssemblyVersion>
9+
<FileVersion>2.0.0.0</FileVersion>
1010
<PackageId>NetCore.AutoRegisterDi</PackageId>
1111
<PackageProjectUrl>https://github.com/JonPSmith/NetCore.AutoRegisterDi</PackageProjectUrl>
1212
<PackageLicenseUrl>https://raw.githubusercontent.com/JonPSmith/NetCore.AutoRegisterDi/master/LICENCE.txt</PackageLicenseUrl>
1313
<RepositoryUrl>https://github.com/JonPSmith/NetCore.AutoRegisterDi</RepositoryUrl>
1414
<RepositoryType>GitHub</RepositoryType>
1515
<PackageTags>NetCore DependencyInjection di</PackageTags>
1616
<Authors>Jon P Smith</Authors>
17-
<Copyright>Copyright (c) 2018 Inventory Innovations, Inc. and Selective Analytics Ltd.</Copyright>
17+
<Copyright>Copyright (c) 2018 Selective Analytics Ltd.</Copyright>
1818
<Company>Selective Analytics</Company>
1919
<Description>Extension method to find/register classes in an assembly into Microsoft.Extensions.DependencyInjection</Description>
20-
<PackageReleaseNotes>New Feature: if no assembly is provided it scans the assembly that called the method.</PackageReleaseNotes>
20+
<PackageReleaseNotes>New Feature: You can now set what ServiceLifetime your class has via Attributes - see README file.</PackageReleaseNotes>
2121
<PackageIconUrl>https://raw.githubusercontent.com/JonPSmith/NetCore.AutoRegisterDi/master/AutoRegisterDiIcon128.png</PackageIconUrl>
2222
</PropertyGroup>
2323

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace NetCore.AutoRegisterDi.PublicButHidden
8+
{
9+
/// <summary>
10+
/// Attribute for marking classes with the ServiceLifetime they should be registered as
11+
/// NOTE: We don't expect to use this attribute as there are better named attributes to use
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.Class)]
14+
public class RegisterWithLifetimeAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// This sets the ServiceLifetime that the AsPublicImplementedInterfaces method should use for this class
18+
/// </summary>
19+
/// <param name="requiredLifetime"></param>
20+
public RegisterWithLifetimeAttribute(ServiceLifetime requiredLifetime)
21+
{
22+
RequiredLifetime = requiredLifetime;
23+
}
24+
25+
/// <summary>
26+
/// This holds the ServiceLifetime that the class which has this attribute should be registered as
27+
/// </summary>
28+
public ServiceLifetime RequiredLifetime { get; }
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
4+
5+
using System;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using NetCore.AutoRegisterDi.PublicButHidden;
8+
9+
namespace NetCore.AutoRegisterDi
10+
{
11+
/// <summary>
12+
/// Attribute for marking classes which need to register with Scope lifetime
13+
/// </summary>
14+
public class RegisterAsScopedAttribute : RegisterWithLifetimeAttribute
15+
{
16+
17+
/// <summary>
18+
/// ctor to set the ServiceLifetime
19+
/// </summary>
20+
public RegisterAsScopedAttribute() : base(ServiceLifetime.Scoped)
21+
{ }
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
4+
5+
using System;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using NetCore.AutoRegisterDi.PublicButHidden;
8+
9+
namespace NetCore.AutoRegisterDi
10+
{
11+
/// <summary>
12+
/// Attribute for marking classes which need to register with Singleton lifetime
13+
/// </summary>
14+
public class RegisterAsSingletonAttribute : RegisterWithLifetimeAttribute
15+
{
16+
/// <summary>
17+
/// ctor to set the ServiceLifetime
18+
/// </summary>
19+
public RegisterAsSingletonAttribute() : base(ServiceLifetime.Singleton)
20+
{
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)