目前.net core 项目下用的最多的IOC框架就是Autofac。

在Nuget中引入两个Autofac.Extras.DynamicProxy、Autofac.Extensions.DependencyInjection

.Net Core6.0 WebAPI项目框架搭建七:IOC框架Autofac_ide

 在SetUp文件夹下新建AutofacModuleRegister.cs,并继承Autofac.Module,重写Load方法,添加如下代码:

public class AutofacModuleRegister : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            //注册Service
            var assemblysServices = Assembly.Load("Web.Core.Services");
            builder.RegisterAssemblyTypes(assemblysServices)
                .InstancePerDependency()//瞬时单例
               .AsImplementedInterfaces()////自动以其实现的所有接口类型暴露(包括IDisposable接口)
               .EnableInterfaceInterceptors(); //引用Autofac.Extras.DynamicProxy;


            //注册Repository
            var assemblysRepository = Assembly.Load("Web.Core.Repository");
            builder.RegisterAssemblyTypes(assemblysRepository)
                .InstancePerDependency()//瞬时单例
               .AsImplementedInterfaces()////自动以其实现的所有接口类型暴露(包括IDisposable接口)
               .EnableInterfaceInterceptors(); //引用Autofac.Extras.DynamicProxy;
        }
    }

在program.cs中注入Autofac。

//注入Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
    //调用程序集注册存入方法
    builder.RegisterModule(new AutofacModuleRegister());
});

然后再BaseService里面改写方法,直接通过构造函数注入

//public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();
        private readonly IBaseRepository<TEntity> baseDal;

        public BaseService(IBaseRepository<TEntity> baseDal)
        {
            this.baseDal = baseDal;
        }

StudentService.cs也要改造一下:

private readonly IStudentRepository _studentRepository;
        public StudentService(IBaseRepository<Student> baseDal, IStudentRepository studentRepository) : base(baseDal)
        {
            _studentRepository = studentRepository;
        }

StudentController也要改造一下,通过构造函数注入

private readonly IStudentService _studentService;

        public StudentController(IStudentService studentService)
        {
            _studentService = studentService;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(Student student)
        {
            //IStudentService userService = new StudentService();
            var count = await _studentService.Add(student);
            return Ok(count);
        }

新建接口读取所有学生的数据来测试Autofac:

StudentController添加接口:

/// <summary>
        /// 查询所有学生
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStudent()
        {
            var student = await _studentService.GetStudent();
            return Ok(student);
        }

StudentService添加代码:

public async Task<List<Student>> GetStudent()
        {
            return await _studentRepository.Query();
        }

IStudentService添加代码:

/// <summary>
        /// 查询所有学生列表
        /// </summary>
        /// <returns></returns>
        Task<List<Student>> GetStudent();

运行项目显示所有学生列表:

.Net Core6.0 WebAPI项目框架搭建七:IOC框架Autofac_.netcore_02