【小5聊】.Net Core基础之举例简单理解依赖注入

【实例化注入】

一般是注入实例化接口类,实例化类比较符合模式化面向对象化编程开发

 

【依赖注入好处】

解耦:表现在功能方法的改变,只需要修改一个地方或者尽可能少的地方

性能:用到的时候才实例化,节省内存空间的占用

 

【依赖注入分析】

以下仅为举例说明,实际业务肯定也不会这样子定义,辅助理解 

 实例化接口类普通实例化类依赖注入实例化接口类
基类

类:INoticeClass

方法:Send()

/

类:INoticeClass

方法:Send()

实现类

类:

EmailClass:INoticeClass、

PhoneMessage:INoticeClass、

WeiXinInfo:INoticeClass

 

方法:实现接口不同类型通知信息发送功能

类:

NoticeEmailClass、

NoticePhoneMessageClass、

NoticeWeiXinInfoClass

方法:Send()

类:EmailClass:INoticeClass

方法:实现接口通知信息发送功能

 

类:PhoneMessage:INoticeClass

方法:实现接口通知信息发送功能

A方法内

new EmailClass().Send();

new EmailClass().Send();

/

A方法外

INoticeClass notice=new EmailClass();

notice.Send("邮箱方法发送通知信息!");

NoticeEmailClass notice=new NoticeEmailClass();

notice.Send("邮箱方法发送通知信息!");

/
A构造函数

public AController()

{

  INoticeClass notice=new EmailClass();

  notice.Send("邮箱方法发送通知信息!");

}

public AController()

{

   NoticeEmailClass notice=new NoticeEmailClass();

   notice.Send("邮箱方法发送通知信息!");

}

private readonly INotice _notice;

public AController(INotice notice)

{

  _notice=notice; //此步骤已经是实例化

  _notice.Send("邮箱方法发送通知信息!");

}

B构造函数

public BController()

{

  INoticeClass notice=new EmailClass();

  notice.Send("邮箱方法发送通知信息!");

}

public AController()

{

   NoticeEmailClass notice=new NoticeEmailClass();

   notice.Send("邮箱方法发送通知信息!");

}

同上
C构造函数

public CController()

{

  INoticeClass notice=new EmailClass();

  notice.Send("邮箱方法发送通知信息!");

}

public AController()

{

   NoticeEmailClass notice=new NoticeEmailClass();

   notice.Send("邮箱方法发送通知信息!");

}

同上
startup启动类//

services.AddSingleton<INoticeClass, EmailClass>();

 

services.AddSingleton<INoticeClass, WeiXinInfo>();

备注如果改为手机短信或微信通知,那么就需要修改三个地方(多个地方需要修改)同左只需要在启动类方法里修改,依赖注入不同的接口和实现类(所谓的注册服务)大大提高了解耦

 

 

思考:Singleton、Scoped、Transient,这三个类又有什么区别呢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
.NET Core 中,依赖注入是一个内置的功能。可以使用 .NET Core 内置的容器进行依赖注入,也可以使用第三方的依赖注入容器,如 Autofac、Ninject 等。 下面介绍如何在 .NET Core 中使用内置的依赖注入容器。 首先,需要在项目中添加 Microsoft.Extensions.DependencyInjection 包。可以通过 NuGet 包管理器或者在 csproj 文件中添加以下代码来添加: ```xml <ItemGroup> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" /> </ItemGroup> ``` 然后,在 Startup 类的 ConfigureServices 方法中进行服务注册: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddTransient<IMyService, MyService>(); // 其他服务注册 } ``` 上面的代码注册了一个名为 `IMyService` 的服务,并指定其实现类型为 `MyService`,并且声明其生命周期为“瞬态”,即每次请求都会创建一个新的实例。 最后,在需要使用服务的地方,可以通过构造函数注入的方式获取服务: ```csharp public class MyController : Controller { private readonly IMyService _myService; public MyController(IMyService myService) { _myService = myService; } // 其他代码 } ``` 上面的代码中,通过构造函数注入的方式获取了 `IMyService` 服务的实例。当创建 `MyController` 实例时,依赖注入容器会自动创建 `IMyService` 的实例并传递给构造函数。 这就是在 .NET Core 中使用内置的依赖注入容器的基本步骤。除了瞬态生命周期,还有单例生命周期和作用域生命周期可以选择。具体可以参考 Microsoft 的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈小5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值