Skip to content

Commit

Permalink
Merge pull request #7 from futurum-dev/feature/source-generator
Browse files Browse the repository at this point in the history
Source Generators and Analyzers and dotnet 7
  • Loading branch information
futurum-dev authored Apr 16, 2023
2 parents e568731 + c098288 commit c5e2cc0
Show file tree
Hide file tree
Showing 94 changed files with 3,715 additions and 80 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ jobs:
with:
fetch-depth: 100

- name: Setup .NET 6
- name: Setup .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.x
dotnet-version: 7.x

- name: Install dependencies
run: dotnet restore

- name: Build Generator
run: dotnet build ./src/Futurum.Microsoft.Extensions.DependencyInjection.Generator --configuration Release --no-restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test on .NET 6
- name: Test on .NET 7
run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov

- name: Publish coverage report to coveralls.io
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ jobs:
with:
fetch-depth: 100

- name: Setup .NET 6
- name: Setup .NET 7
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.x
dotnet-version: 7.x

- name: Install dependencies
run: dotnet restore

- name: Build Generator
run: dotnet build ./src/Futurum.Microsoft.Extensions.DependencyInjection.Generator --configuration Release --no-restore

- name: Build
run: dotnet build --configuration Release --no-restore

Expand Down
20 changes: 20 additions & 0 deletions Futurum.Microsoft.Extensions.DependencyInjection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{D89EB7
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Futurum.Microsoft.Extensions.DependencyInjection.Sample", "sample\Futurum.Microsoft.Extensions.DependencyInjection.Sample\Futurum.Microsoft.Extensions.DependencyInjection.Sample.csproj", "{062C6431-11B5-4927-90F4-FB928C59CE24}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Futurum.Microsoft.Extensions.DependencyInjection.Generator", "src\Futurum.Microsoft.Extensions.DependencyInjection.Generator\Futurum.Microsoft.Extensions.DependencyInjection.Generator.csproj", "{8D3BB258-2AE3-42D8-8FCA-53CB08EFBC66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample", "sample\Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample\Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample.csproj", "{0A9F5652-EF5A-45AD-86A9-29702F426944}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Futurum.Microsoft.Extensions.DependencyInjection.Generator.Tests", "test\Futurum.Microsoft.Extensions.DependencyInjection.Generator.Tests\Futurum.Microsoft.Extensions.DependencyInjection.Generator.Tests.csproj", "{45B99B38-E5F5-47FF-88AE-BC48E7EA2616}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,9 +47,23 @@ Global
{062C6431-11B5-4927-90F4-FB928C59CE24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{062C6431-11B5-4927-90F4-FB928C59CE24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{062C6431-11B5-4927-90F4-FB928C59CE24}.Release|Any CPU.Build.0 = Release|Any CPU
{8D3BB258-2AE3-42D8-8FCA-53CB08EFBC66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D3BB258-2AE3-42D8-8FCA-53CB08EFBC66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D3BB258-2AE3-42D8-8FCA-53CB08EFBC66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D3BB258-2AE3-42D8-8FCA-53CB08EFBC66}.Release|Any CPU.Build.0 = Release|Any CPU
{0A9F5652-EF5A-45AD-86A9-29702F426944}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A9F5652-EF5A-45AD-86A9-29702F426944}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A9F5652-EF5A-45AD-86A9-29702F426944}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A9F5652-EF5A-45AD-86A9-29702F426944}.Release|Any CPU.Build.0 = Release|Any CPU
{45B99B38-E5F5-47FF-88AE-BC48E7EA2616}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45B99B38-E5F5-47FF-88AE-BC48E7EA2616}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45B99B38-E5F5-47FF-88AE-BC48E7EA2616}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45B99B38-E5F5-47FF-88AE-BC48E7EA2616}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CAF8E145-BD58-4000-96E4-51390C7407FF} = {3BC45DF0-972E-4400-80FC-442F6B3084CD}
{062C6431-11B5-4927-90F4-FB928C59CE24} = {D89EB769-9F2C-443F-9821-012D173DA16A}
{0A9F5652-EF5A-45AD-86A9-29702F426944} = {D89EB769-9F2C-443F-9821-012D173DA16A}
{45B99B38-E5F5-47FF-88AE-BC48E7EA2616} = {3BC45DF0-972E-4400-80FC-442F6B3084CD}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_EXPR_METHOD_ON_SINGLE_LINE/@EntryValue">NEVER</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_EXPR_PROPERTY_ON_SINGLE_LINE/@EntryValue">NEVER</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">200</s:Int64>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Futurum_002EMicrosoft_002EExtensions_002EDependencyInjection_002EGenerator_002ESample_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Futurum_002EMicrosoft_002EExtensions_002EDependencyInjection_002EGenerator_002ETests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Futurum_002EMicrosoft_002EExtensions_002EDependencyInjection_002ETests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

A dotnet library, that allows Microsoft.Extensions.DependencyInjection to work with Futurum.Core. It also adds support for modules and startables.

- [x] Autodiscovery of DependencyInjection registrations, based on [attributes](#attribute-based-registration) and Source Generators
- [x] Autodiscovery of DependencyInjection modules, based on [attributes](#attribute-based-module) and Source Generators
- [x] Autodiscovery of DependencyInjection startables, based on [attributes](#attribute-based-startable) and Source Generators
- [x] [Roslyn Analysers](#roslyn-analysers) to help build your WebApiEndpoint(s), using best practices

## TryGetService
Try to get the service object of the specified type.

Expand All @@ -30,15 +35,15 @@ public class TestModule : IModule
}
```

### RegisterModule extension method
### AddModule extension method
Allows you to register a module.

```csharp
services.RegisterModule<TestModule>();
services.AddModule<TestModule>();
```

```csharp
services.RegisterModule(new TestModule());
services.AddModule(new TestModule());
```

## Startables
Expand Down Expand Up @@ -73,4 +78,41 @@ Creates a ServiceProvider containing services from the provided IServiceCollecti

```csharp
var serviceProvider = services.BuildServiceProviderWithStartables();
```
```

## Attribute based registration
You can also register services using attributes.

- RegisterAsSingleton attribute
- RegisterAsScoped attribute
- RegisterAsTransient attribute

### DuplicateRegistrationStrategy
- Try - Adds the new registration, if the service hasn't already been registered
- Replace - Removes any existing registration and then adds the new registration
- Add - Adds the new registration, irrespective of if its previously been registered

**NOTE** - This defaults to *Try*

### InterfaceRegistrationStrategy
- Self - Registers the service as itself
- ImplementedInterfaces - Registers the service as each its implemented interfaces
- SelfWithInterfaces - Registers the service as itself and each its implemented interfaces

**NOTE** - This defaults to *SelfWithInterfaces*

## Attribute based module
You can also register modules using attributes.

- RegisterAsDependencyInjectionModule attribute

## Attribute based startable
You can also register modules using attributes.

- RegisterAsDependencyInjectionStartable attribute

## Roslyn Analysers
- FMEDI0001 - Invalid Module Parameter
- FMEDI0002 - Missing Module Parameter
- FMEDI0003 - Non empty constructor found on Module
- FMEDI0004 - Non empty constructor found on Startable
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Futurum.Microsoft.Extensions.DependencyInjection.Generator\Futurum.Microsoft.Extensions.DependencyInjection.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\Futurum.Microsoft.Extensions.DependencyInjection\Futurum.Microsoft.Extensions.DependencyInjection.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample;

public class InstanceModule
{
[RegisterAsDependencyInjectionModule]
public void Register(IServiceCollection services)
{
services.TryAddTransient<IModuleService, ModuleService>();
}
}

public static class StaticModule
{
[RegisterAsDependencyInjectionModule]
public static void Register(IServiceCollection services)
{
services.TryAddTransient<IModuleService, ModuleService>();
}
}

public interface IModuleService
{
}

public class ModuleService : IModuleService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample;

public interface IService1
{
}

public interface IService2
{
}

public interface IService3
{
}

[RegisterAsTransient(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.Self)]
public class TransientService_Self : IService1
{
}

[RegisterAsTransient(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.ImplementedInterfaces)]
public class TransientService_ImplementedInterfaces : IService1
{
}

[RegisterAsTransient(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.SelfWithInterfaces)]
public class TransientService_SelfWithInterfaces : IService1
{
}

[RegisterAsScoped(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.Self)]
public class ScopedService_Self : IService2
{
}

[RegisterAsScoped(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.ImplementedInterfaces)]
public class ScopedService_ImplementedInterfaces : IService2
{
}

[RegisterAsScoped(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.SelfWithInterfaces)]
public class ScopedService_SelfWithInterfaces : IService2
{
}

[RegisterAsSingleton(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.Self)]
public class SingletonService_Self : IService3
{
}

[RegisterAsSingleton(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.ImplementedInterfaces)]
public class SingletonService_ImplementedInterfaces : IService3
{
}

[RegisterAsSingleton(InterfaceRegistrationStrategy = InterfaceRegistrationStrategy.SelfWithInterfaces)]
public class SingletonService_SelfWithInterfaces : IService3
{
}

[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Try)]
public class SingletonService_Try : IService1
{
}

[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Replace)]
public class SingletonService_Replace : IService1
{
}

[RegisterAsScoped(DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class SingletonService_Add : IService1
{
}

[RegisterAsScoped(ServiceType = typeof(IService2), DuplicateRegistrationStrategy = DuplicateRegistrationStrategy.Add)]
public class ServiceMultipleInterfaces : IService1, IService2
{
}

public interface IService_OpenGeneric<T>
{
}

[RegisterAsSingleton(ImplementationType = typeof(TransientServiceOpenGeneric<>), ServiceType = typeof(IService_OpenGeneric<>))]
public class TransientServiceOpenGeneric<T> : IService_OpenGeneric<T>
{
}

[RegisterAsSingleton(ImplementationType = typeof(ScopedServiceOpenGeneric<>), ServiceType = typeof(IService_OpenGeneric<>))]
public class ScopedServiceOpenGeneric<T> : IService_OpenGeneric<T>
{
}

[RegisterAsSingleton(ImplementationType = typeof(SingletonServiceOpenGeneric<>), ServiceType = typeof(IService_OpenGeneric<>))]
public class SingletonServiceOpenGeneric<T> : IService_OpenGeneric<T>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Generator.Sample;

public class InstanceStartable
{
[RegisterAsDependencyInjectionStartable]
public void Register()
{
}
}

public static class StaticStartable
{
[RegisterAsDependencyInjectionStartable]
public static void Register()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

public class AutomaticInstanceModule
{
[RegisterAsDependencyInjectionModule]
public void Load(IServiceCollection services)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

public class AutomaticInstanceStartable
{
[RegisterAsDependencyInjectionStartable]
public void Start()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

[RegisterAsScoped]
public class AutomaticScopedService : IService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

[RegisterAsSingleton]
public class AutomaticSingletonService : IService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

public static class AutomaticStaticModule
{
[RegisterAsDependencyInjectionModule]
public static void Load(IServiceCollection services)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Futurum.Microsoft.Extensions.DependencyInjection.Sample;

public static class AutomaticStaticStartable
{
[RegisterAsDependencyInjectionStartable]
public static void Start()
{
var x = 10;
}
}
Loading

0 comments on commit c5e2cc0

Please sign in to comment.