Skip to content

Commit

Permalink
#7 Support Auto Inject
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Mar 26, 2024
1 parent ac1093a commit 6b06434
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Biwen.AutoClassGen.TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Biwen.AutoClassGen.TestConsole.Decors;
using Biwen.AutoClassGen.TestConsole.Dtos;
using Biwen.AutoClassGen.TestConsole.Entitys;
using Biwen.AutoClassGen.TestConsole.Services;


var builder = WebApplication.CreateBuilder();
Expand All @@ -16,7 +17,7 @@
// add auto decor
builder.Services.AddAutoDecor();


// add auto inject
builder.Services.AddAutoInject();


Expand All @@ -35,6 +36,14 @@
Console.WriteLine(result2);


// get auto inject svc
var svcInject = scope.ServiceProvider.GetRequiredService<ITest2Service>();
var result3 = svcInject.Say2("from auto inject");
Console.WriteLine(result3);




Biwen.AutoClassGen.Models.QueryRequest queryRequest = new()
{
PageLen = 10,
Expand Down
12 changes: 11 additions & 1 deletion Biwen.AutoClassGen.TestConsole/Services/TestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ITest2Service

[AutoInject<TestService>]
[AutoInject<ITestService>(ServiceLifetime.Singleton)]
[AutoInject<ITest2Service>(ServiceLifetime.Scoped)]
//[AutoInject<ITest2Service>(ServiceLifetime.Scoped)]
public class TestService : ITestService, ITest2Service
{
public string Say(string message)
Expand All @@ -28,4 +28,14 @@ public string Say2(string message)
return message;
}
}


[AutoInject<ITest2Service>]
public class TestService2 : ITest2Service
{
public string Say2(string message)
{
return message;
}
}
}
1 change: 1 addition & 0 deletions Biwen.AutoClassGen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Biwen.AutoClassGen", "Biwen
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{02E9602B-EF18-4AB5-A4CB-88331BC04249}"
ProjectSection(SolutionItems) = preProject
Gen-AutoInject.md = Gen-AutoInject.md
Gen-Decor.md = Gen-Decor.md
Gen-Dto.md = Gen-Dto.md
Gen-request.md = Gen-request.md
Expand Down
93 changes: 93 additions & 0 deletions Gen-AutoInject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## Usage

```c#

//C#11(NET7+) support generic attribute
[AutoDto<T>(ServiceLifetime.Scoped)]

```

### Your Service

```c#

public interface ITestService
{
string Say(string message);
}

public interface ITest2Service
{
string Say2(string message);
}

[AutoInject<TestService>]
[AutoInject<ITestService>(ServiceLifetime.Singleton)]
//[AutoInject<ITest2Service>(ServiceLifetime.Scoped)]
public class TestService : ITestService, ITest2Service
{
public string Say(string message)
{
return $"hello {message}";
}

public string Say2(string message)
{
return message;
}
}

[AutoInject<ITest2Service>]
public class TestService2 : ITest2Service
{
public string Say2(string message)
{
return message;
}
}


```


### Enjoy

```c#
// <auto-generated />
#pragma warning disable
namespace Microsoft.Extensions.DependencyInjection
{
using Biwen.AutoClassGen.TestConsole.Services;

public static class ServiceCollectionExtension
{
/// <summary>
/// 自动注册标注的服务
/// </summary>
/// <param name = "services"></param>
/// <returns></returns>
public static IServiceCollection AddAutoInject(this IServiceCollection services)
{
services.AddScoped<TestService>();
services.AddSingleton<ITestService, TestService>();
services.AddScoped<ITest2Service, TestService2>();
return services;
}
}
}
#pragma warning restore
```

### Then .AddAutoInject()

- 服务将自动注册到DI容器中

```c#

// add auto inject
builder.Services.AddAutoInject();

```


### Report Diagnostic Code
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ If all this code needs to be written, it will add a lot of work, so Biwen.AutoCl
- AOP & Decorator



[中文](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/README-zh.md)

### Usage
Expand All @@ -22,6 +21,7 @@ dotnet add package Biwen.AutoClassGen.Attributes
- [Gen DTO Usage doc](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-Dto.md)
- [Gen Request Usage doc](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-request.md)
- [Gen Decoration Usage doc](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-Decor.md)
- [Gen AutoInject Usage doc](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-AutoInject.md)

### Used by
#### if you use this library, please tell me, I will add your project here.
Expand Down

0 comments on commit 6b06434

Please sign in to comment.