Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ignatandrei/functionsdi
DI for functions
https://github.com/ignatandrei/functionsdi
Last synced: about 1 month ago
JSON representation
DI for functions
- Host: GitHub
- URL: https://github.com/ignatandrei/functionsdi
- Owner: ignatandrei
- License: mit
- Created: 2022-06-17T04:03:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-06T22:29:09.000Z (11 months ago)
- Last Synced: 2024-10-04T16:38:54.870Z (2 months ago)
- Language: C#
- Size: 72.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/ignatandrei/functionsdi
README
# FunctionsDI
[![Nuget](https://img.shields.io/nuget/v/RSCG_FunctionsWithDI)](https://www.nuget.org/packages/RSCG_FunctionsWithDI)
Generate (constructor) and functions calls similar with ASP.NET Core WebAPI ( [FromServices] will be provided by DI )
Also, verifies for null .# Usage 1 - generate constructors from methods
Reference into the csproj
```xml
true
$(BaseIntermediateOutputPath)GeneratedX
```
Then for every class you can write [FromServices]
```csharp
using RSCG_FunctionsWithDI_Base;
//namespace if necessary
public partial class TestDIFunction
{
public bool TestMyFunc1([FromServices] TestDI1 t1, [FromServices] TestDI2 t2, int x, int y)
{
return true;
}
//more functions
}
```generates the constructor with needed details
```csharp
public partial class TestDIFunction
{
private TestDI1 _TestDI1;
private TestDI2 _TestDI2;
public TestDIFunction (TestDI1 _TestDI1,TestDI2 _TestDI2) //constructor generated with needed DI
{
this._TestDI1=_TestDI1;
this._TestDI2=_TestDI2;} //end constructor
//making call to TestMyFunc1
public bool TestMyFunc1(int x,int y){
var t1 = this._TestDI1 ;
if(t1 == null) throw new ArgumentException(" service TestDI1 is null in TestDIFunction ");
var t2 = this._TestDI2 ;
if(t2 == null) throw new ArgumentException(" service TestDI2 is null in TestDIFunction ");
return TestMyFunc1(t1,t2,x,y);
}```
so you can call
```csharpvar test=serviceProvider.GetService();
Console.WriteLine(test.TestMyFunc1(10,3)); // calling without the [FromServices] arguments```
# Usage 2 - generate constructors from fields / constructors
```xml
true
$(BaseIntermediateOutputPath)GeneratedX
```
Assuming this classes, that you want to keep a minimum of parameters constructors
```csharp
public partial class TestDIFunctionAdvWithConstructor
{
[RSCG_FunctionsWithDI_Base.FromServices]
private TestDI1 NewTestDI1;[RSCG_FunctionsWithDI_Base.FromServices]
public TestDI2 NewTestDI2 { get; set; }public readonly TestDI3 myTestDI3;
private TestDIFunctionAdvWithConstructor(TestDI3 test)
{
myTestDI3= test;
}
}
public partial class TestDIFunctionAdvNoConstructor
{
[RSCG_FunctionsWithDI_Base.FromServices]
public TestDI1 NewTestDI1;[RSCG_FunctionsWithDI_Base.FromServices]
private TestDI2 NewTestDI2 { get; set; }}
```
the generator will generate```csharp
namespace TestFunctionsWithDI{
public partial class TestDIFunctionAdvNoConstructor
{
public TestDIFunctionAdvNoConstructor( TestDI1 _NewTestDI1,TestDI2 _NewTestDI2 )
{
this.NewTestDI1 = _NewTestDI1;
this.NewTestDI2 = _NewTestDI2;
}//end constructor}//class
}//namespacenamespace TestFunctionsWithDI
{
public partial class TestDIFunctionAdvWithConstructor
{
public TestDIFunctionAdvWithConstructor(TestDI3 test, TestDI1 _NewTestDI1, TestDI2 _NewTestDI2) : this (test)
{
this.NewTestDI1 = _NewTestDI1;
this.NewTestDI2 = _NewTestDI2;
}//end constructor}//class
}//namespace
```Enjoy!
# More Roslyn Source Code Generators
You can find more RSCG with examples at [Roslyn Source Code Generators](https://ignatandrei.github.io/RSCG_Examples/v2/)