Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerzy Jurgiel committed Oct 25, 2019
0 parents commit 6ae1392
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 0 deletions.
433 changes: 433 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions ServiceRegistration.sln
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
26 changes: 26 additions & 0 deletions ServiceRegistration/Program.cs
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();
}
}
}
12 changes: 12 additions & 0 deletions ServiceRegistration/ServiceRegistration.csproj
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>
11 changes: 11 additions & 0 deletions ServiceRegistration/Services/IMailService.cs
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();
}
}
11 changes: 11 additions & 0 deletions ServiceRegistration/Services/ITestService.cs
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();
}
}
16 changes: 16 additions & 0 deletions ServiceRegistration/Services/MailService.cs
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");
}
}
}
24 changes: 24 additions & 0 deletions ServiceRegistration/Services/TestService.cs
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 ServiceRegistrationLibrary/Attributes/ScopedServiceAttribute.cs
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 ServiceRegistrationLibrary/Attributes/SingletonServiceAttribute.cs
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 ServiceRegistrationLibrary/Attributes/TransientServiceAttribute.cs
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
{
}
}
65 changes: 65 additions & 0 deletions ServiceRegistrationLibrary/ServiceExtension.cs
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 ServiceRegistrationLibrary/ServiceRegistrationLibrary.csproj
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>

0 comments on commit 6ae1392

Please sign in to comment.