-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jerzy Jurgiel
committed
Oct 25, 2019
0 parents
commit 6ae1392
Showing
13 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.29411.108 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceRegistration", "ServiceRegistration\ServiceRegistration.csproj", "{647DC861-3B1F-4322-9A58-23BC11779D8A}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceRegistrationLibrary", "ServiceRegistrationLibrary\ServiceRegistrationLibrary.csproj", "{E6E64FE6-90A5-4728-9CB2-83A13666CD85}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{647DC861-3B1F-4322-9A58-23BC11779D8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{647DC861-3B1F-4322-9A58-23BC11779D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{647DC861-3B1F-4322-9A58-23BC11779D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{647DC861-3B1F-4322-9A58-23BC11779D8A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{E6E64FE6-90A5-4728-9CB2-83A13666CD85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E6E64FE6-90A5-4728-9CB2-83A13666CD85}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E6E64FE6-90A5-4728-9CB2-83A13666CD85}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E6E64FE6-90A5-4728-9CB2-83A13666CD85}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {6490A37E-875F-4191-BFA1-5F96AAC81301} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ServiceRegistration.Services; | ||
using ServiceRegistrationLibrary; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ServiceRegistration | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.RegisterCustomServices(); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
|
||
var service = serviceProvider.GetService<ITestService>(); | ||
service.DoSth(); | ||
|
||
|
||
Console.ReadLine(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ServiceRegistrationLibrary\ServiceRegistrationLibrary.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistration.Services | ||
{ | ||
public interface IMailService | ||
{ | ||
void SendMail(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistration.Services | ||
{ | ||
public interface ITestService | ||
{ | ||
void DoSth(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using ServiceRegistrationLibrary.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistration.Services | ||
{ | ||
[TransientService] | ||
public class MailService : IMailService | ||
{ | ||
public void SendMail() | ||
{ | ||
Console.WriteLine("Mail Sent"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using ServiceRegistrationLibrary.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistration.Services | ||
{ | ||
[SingletonService] | ||
|
||
public class TestService : ITestService | ||
{ | ||
private readonly IMailService _mailService; | ||
|
||
public TestService(IMailService mailService) | ||
{ | ||
_mailService = mailService; | ||
} | ||
public void DoSth() | ||
{ | ||
Console.WriteLine("Working"); | ||
_mailService.SendMail(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ServiceRegistrationLibrary/Attributes/ScopedServiceAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistrationLibrary.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false)] | ||
public class ScopedServiceAttribute : Attribute | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
ServiceRegistrationLibrary/Attributes/SingletonServiceAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistrationLibrary.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false)] | ||
|
||
public class SingletonServiceAttribute : Attribute | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ServiceRegistrationLibrary/Attributes/TransientServiceAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ServiceRegistrationLibrary.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false)] | ||
public class TransientServiceAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ServiceRegistrationLibrary.Attributes; | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace ServiceRegistrationLibrary | ||
{ | ||
public static class ServiceExtension | ||
{ | ||
|
||
public static ServiceCollection RegisterCustomServices(this ServiceCollection services) | ||
{ | ||
var assembly = Assembly.GetCallingAssembly(); | ||
return RegisterCustomServicesFunctionality(services, assembly, "Service"); | ||
} | ||
|
||
public static ServiceCollection RegisterCustomServices(this ServiceCollection services, Assembly assembly) | ||
{ | ||
return RegisterCustomServicesFunctionality(services, assembly, "Service"); | ||
} | ||
|
||
|
||
public static ServiceCollection RegisterCustomServices(this ServiceCollection services, string name) | ||
{ | ||
var assembly = Assembly.GetCallingAssembly(); | ||
return RegisterCustomServicesFunctionality(services, assembly, name); | ||
} | ||
public static ServiceCollection RegisterCustomServices(this ServiceCollection services, Assembly assembly, string name) | ||
{ | ||
return RegisterCustomServicesFunctionality(services, assembly, name); | ||
} | ||
|
||
|
||
|
||
private static ServiceCollection RegisterCustomServicesFunctionality(ServiceCollection services, Assembly assembly, string name) | ||
{ | ||
var types = assembly.GetTypes().Where(x => x.Name.Contains(name) && x.IsClass); | ||
foreach (var type in types) | ||
{ | ||
var intrfce = type.GetInterfaces().FirstOrDefault(x => x.Name.Contains(name)); | ||
if (intrfce == null) continue; | ||
var attribute = type.CustomAttributes.FirstOrDefault(x => x.AttributeType.Name.Contains("ServiceAttribute")); | ||
|
||
if (attribute == null) | ||
{ | ||
services.AddTransient(intrfce, type); | ||
} | ||
else if (attribute.AttributeType.Equals(typeof(TransientServiceAttribute))) | ||
{ | ||
services.AddTransient(intrfce, type); | ||
} | ||
else if (attribute.AttributeType.Equals(typeof(SingletonServiceAttribute))) | ||
{ | ||
services.AddSingleton(intrfce, type); | ||
} | ||
else if (attribute.AttributeType.Equals(typeof(ScopedServiceAttribute))) | ||
{ | ||
services.AddScoped(intrfce, type); | ||
} | ||
} | ||
return services; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ServiceRegistrationLibrary/ServiceRegistrationLibrary.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |