.Net Core基础知识

IOC

IOC基本介绍

IOC的实现方式:

  • 服务定位器(ServiceLocator),例如: ServiceLocator.GetService<XXXService>
  • DI (Dependency Injection)

IOC中的一些概念:

  • 服务:我们需要容器帮我们创建的对象
  • 注册服务:将对象注册到容器中
  • 服务容器:负责管理注册的对象
  • 查询服务:创建和关联对象
  • 对象生命周期:
    • Transient:瞬态,用完就销毁,每次获取都是重新创建一个新的对象
    • Scoped:范围,用完不销毁,指定范围内获取的都是同一个对象
    • Singleton:单例,全局获取的都是同一个对象。建议在创建无状态对象时使用

.NET Core中IOC的使用

.NET控制服务反转组件取名为DependencyInjection,同时包含了ServiceLocator的功能。

ServiceLocator

1、Install-Package
Microsoft.Extensions.DependencyInjection

2、获取服务
创建ServiceCollection对象,注册服务,调用ServiceCollection中的BuilderServiceProvider()方法,创建ServiceProvider容器,获取服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Program
{
static void Main(string[] args)
{
// 1、创建ServiceCollection对象
ServiceCollection services = new ServiceCollection();
// 2、注册服务
services.AddTransient<IUserService,UserService>();
// 3、创建SerivceProvider容器
using (ServiceProvider provider = services.BuildServiceProvider())
{
// 4、从容器中获取服务
UserInfo user = new() { Name="zhangyuzhen",Age=22 };
var userInfo = provider.GetService<IUserService>().ShowUserInfo(user);
Console.WriteLine(userInfo);
}
}
}

DI

1.依赖注入是具有传染性的,如果一个类的实例对象是通过DI创建的,那么这个类的构造器中所有声明的服务都将会使用DI创建

2.默认构造函数注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class UserController
{

private readonly IUserService _userService;
private readonly ILogService _logService;

// Core DI
public UserController(IUserService userService, ILogService logService)
{
_userService = userService;
_logService = logService;
}

public void Test()
{
_userService.Print("调用userService接口!");
_logService.Log("调用LogService接口!");
}
}

class Program
{
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddSingleton<ILogService,LogService>();
services.AddSingleton<IUserService,UserService>();
services.AddSingleton<UserController>();
using (ServiceProvider provider = services.BuildServiceProvider())
{
provider.GetService<UserController>().Test();
}
}
}

配置系统

Json文件配置

1、Install Package

Microsoft.Extensions.Configuration

Microsoft.Extensions.Configuration.Json
绑定读取配置,可将配置文件内容封装到类中:Microsoft.Extensions.Configuration.Binder

2、读取配置文件

1
2
3
4
5
6
7
8
9
{
"Name": "张昱侦",
"Age": 22,
"Address": {
"used": "武汉",
"now": "成都"
},
"Server": {"Ip": "127.0.0.1","Port": 8801}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Program
{
static void Main(string[] args)
{
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("config.json",optional:true,reloadOnChange:true);
IConfigurationRoot config = configurationBuilder.Build();
string name = config["Name"];
Console.WriteLine("Name: "+ name);
int age = int.Parse(config["Age"]);
Console.WriteLine("Age: " + age);
string addressNow = config.GetSection("Address:now").Value;
Console.WriteLine("AddressNow: "+ addressNow);
// 绑定读取配置
Server server = config.GetSection("Server").Get<Server>();
Console.WriteLine(server);// 服务器地址为:127.0.0.1:8801
}
}
class Server
{
public string Ip { get; set; }
public int Port { get; set; }

public override string ToString()
{
return $"服务器地址为:{Ip}:{Port}";
}
}

选项方式读取(推荐使用)

1、Install Package
Microsoft.Extensions.Options
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.Binder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class ConfigService
{
private readonly IOptionsSnapshot<Server> _option;
// Core DI
public ConfigService(IOptionsSnapshot<Server> option)
{
_option = option;
}

public Server GetServer()
{
return _option.Value;
}
}

class Program
{
static void Main(string[] args)
{
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("config.json",optional:true,reloadOnChange:true);
IConfigurationRoot config = configurationBuilder.Build();

ServiceCollection services = new ServiceCollection();
// 绑定配置源到指定对象上
services.AddOptions().Configure<Server>(s => config.GetSection("Server").Bind(s));
services.AddScoped<ConfigService>();
using(ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Server server = serviceProvider.GetService<ConfigService>().GetServer();
Console.WriteLine(server);
}
}
}

public class Server
{
public string Ip { get; set; }
public int Port { get; set; }

public override string ToString()
{
return $"服务器地址为:{Ip}:{Port}";
}
}

日志系统

明天再学,累了。