Skip to content

Commit 6b35774

Browse files
20250708-084231
1 parent 6b4c685 commit 6b35774

File tree

13 files changed

+262
-0
lines changed

13 files changed

+262
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ConfigServices
8+
{
9+
/// <summary>
10+
/// 实现类可以考虑在其它项目中创建,这里图省事直接创建在类库项目中
11+
/// </summary>
12+
public class EnvVarConfigService : IConfigService
13+
{
14+
public string GetValue(string name)
15+
{
16+
return Environment.GetEnvironmentVariable(name) ?? $"{name}_not_found";
17+
}
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ConfigServices
8+
{
9+
public interface IConfigService
10+
{
11+
public string GetValue(string name);
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace LogServices
8+
{
9+
/// <summary>
10+
/// 实现类可以考虑在其它项目中创建,这里图省事直接创建在类库项目中
11+
/// </summary>
12+
public class ConsoleLogProvider : ILogProvider
13+
{
14+
public void LogError(string msg)
15+
{
16+
Console.WriteLine($"Error: {msg}");
17+
}
18+
19+
public void LogInfo(string msg)
20+
{
21+
Console.WriteLine($"Info: {msg}");
22+
}
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace LogServices
8+
{
9+
public interface ILogProvider
10+
{
11+
public void LogError(string msg);
12+
13+
public void LogInfo(string msg);
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MailSender", "MailSender\MailSender.csproj", "{1EEBA1CB-F8BF-450F-AD95-EFC953F61749}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogServices", "LogServices\LogServices.csproj", "{7FD0A4A6-A540-4AB5-9152-8CAF4BE1526A}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MailServices", "MailServices\MailServices.csproj", "{97268C9A-9677-45EF-A6AD-1CC138F339AD}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigServices", "ConfigServices\ConfigServices.csproj", "{8655EEC6-8CE6-464F-9A28-BB53551CB9CC}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{1EEBA1CB-F8BF-450F-AD95-EFC953F61749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{1EEBA1CB-F8BF-450F-AD95-EFC953F61749}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1EEBA1CB-F8BF-450F-AD95-EFC953F61749}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{1EEBA1CB-F8BF-450F-AD95-EFC953F61749}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{7FD0A4A6-A540-4AB5-9152-8CAF4BE1526A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{7FD0A4A6-A540-4AB5-9152-8CAF4BE1526A}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{7FD0A4A6-A540-4AB5-9152-8CAF4BE1526A}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{7FD0A4A6-A540-4AB5-9152-8CAF4BE1526A}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{97268C9A-9677-45EF-A6AD-1CC138F339AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{97268C9A-9677-45EF-A6AD-1CC138F339AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{97268C9A-9677-45EF-A6AD-1CC138F339AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{97268C9A-9677-45EF-A6AD-1CC138F339AD}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{8655EEC6-8CE6-464F-9A28-BB53551CB9CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{8655EEC6-8CE6-464F-9A28-BB53551CB9CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{8655EEC6-8CE6-464F-9A28-BB53551CB9CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{8655EEC6-8CE6-464F-9A28-BB53551CB9CC}.Release|Any CPU.Build.0 = Release|Any CPU
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {E077E5BE-EB87-4D24-99B4-4D5311FCB9F8}
42+
EndGlobalSection
43+
EndGlobal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\ConfigServices\ConfigServices.csproj" />
16+
<ProjectReference Include="..\LogServices\LogServices.csproj" />
17+
<ProjectReference Include="..\MailServices\MailServices.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using ConfigServices;
2+
using LogServices;
3+
using MailServices;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace MailSender
7+
{
8+
internal class Program
9+
{
10+
/// <summary>
11+
/// Install-Package Microsoft.Extensions.DependencyInjection
12+
/// using Microsoft.Extensions.DependencyInjection
13+
/// </summary>
14+
/// <param name="args"></param>
15+
static void Main(string[] args)
16+
{
17+
ServiceCollection services = new ServiceCollection();
18+
// 注册服务
19+
services.AddScoped<ILogProvider, ConsoleLogProvider>();
20+
services.AddScoped<IConfigService, EnvVarConfigService>();
21+
services.AddScoped<IMailService, MailService>();
22+
using (ServiceProvider sp = services.BuildServiceProvider())
23+
{
24+
// 根对象需要用ServiceLocator的方式去获取,关联的对象可以直接注入
25+
IMailService mailService = sp.GetRequiredService<IMailService>();
26+
mailService.Send("Hello", "[email protected]", "hello?");
27+
}
28+
29+
// Console.ReadLine();
30+
}
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MailServices
8+
{
9+
public interface IMailService
10+
{
11+
public void Send(string title, string to, string body);
12+
}
13+
}

0 commit comments

Comments
 (0)