Scratch using IOC in .NetCore3.1 Web project

Foreword

  Now the programming world, IOC has become a standard, after all decoupling for code development and maintenance are greatly improved efficiency; and .NetCore the IOC comes with a function, the following .NetCore Web project record what the IOC and how to use integrated AutoFac; 

surroundings

  Development Environment: .NetCore3.1; Note: You need to install the corresponding version of the SDK;

Project Creation

  1. Select a template - ASP.NET Core Web Application;

 

  2. Add two projects DotNetCoreBll and DotNetCoreDal, choose a template when selecting  library (.Net Core);

  The final project structure

 

   Add these two assemblies purpose is to simulate the business process, DotNetCoreBll service processing; DotNetCoreDal obtaining data; DotNetCoreIoc set to start operation of the project, as FIG.

 

   Appear above the interface project will be created;

The traditional way - his own New 

  1. UserDal increased and in classes UserBll DotNetCoreDal DotNetCoreBll and in which code is as follows:

  Class UserDal:

  

 

   Class UserBll:

  

 

   2. Add a Api in the HomeController

  

 

   Then, run the project, visit http: // localhost: 5000 / Home / GetUserName  address, run as follows:

        

 

  In summary, it can be seen, the traditional way but also the normal development capabilities, but Mrs. strong coupling between the various assemblies, does not comply with the principles of software design, when large-scale project, the project is not good maintenance; from a development point of view , require developers to manually create objects, development efficiency is not high;

.NetCore comes with IOC

  Three strides away from:

    1 looking in the Web project Startup.cs files, open the file, in the process will be a corresponding type ConfigureServices poured into the container, as follows:

  

 

     2. 在调用控制器构造函数中进行注入, 这里是在HomeController中,如下:

  

 

     3. 在对应API中使用

  

 

     运行访问

       

 

   如上,正常访问,但是虽然减少了开发者手动new对象的过程,但是耦合性的问题还是存在的呀,所以这里引入依赖倒置原则,即依赖于抽象,不依赖于具体,所以我们通常会引入对应的抽象层,通常是接口的形式;

  抽象化,增加接口层,并添加对应的接口,项目结构如下:

  

 

    IUserBll 和  IUserDal 代码代码分别如下:

  

 

  将原来的UserBll和UserDal分别继承IUserBll和IUserDal,如下:

  

   IOC根据以上三步使用,修改对应的代码

  1.  在注入容器中进行优化:

  

 

   2. 构造函数注入,改为接口:

  

 

   3. 使用的地方不变

 

  运行如图,正常运行:

  

 

    通过这样优化,耦合性降低了,依赖于抽象,平时也都是这么用;

  缺点: 由于.NetCore自带的IOC,在注册到容器的时候,只能指定对应的类型,当项目类型比较多的时候,在注册时就会大量的写代码;

  措施: 通常我们会引入第三组件进行解决,因为第三方组件提供了注册程序集的功能,这里主要记录一下AutoFac在.NetCore项目中的使用;

.NetCore3.1 集成AutoFac

  1. 在Web项目中通过Nuget引入AutoFac包;

  

  2. 首先在 Startup.cs 中添加 ConfigureContainer 方法,  方法名不变;

  

 

   3. 修改 Program.cs 将默认ServiceProviderFactory指定为AutofacServiceProviderFactory

  

 

   

  弄完以上步骤,注释掉原来的注入

       

 

   运行项目,看结果正常:

  

 

总结

  综上所述,.NetCore项目中Ioc及集成Autofac的使用看着比较简单,但以上例子只修改了业务层, Dal层没有优化,你们可以参考业务层,可以练习一下Dal层的优化; 另外Autofac的知识点还不少,如果不知道的,自行百度学学,有问题随时沟通;

Guess you like

Origin www.cnblogs.com/zoe-zyq/p/12213314.html