.Net Framework使用Autofac实现依赖注入

.Net Framework使用Autofac实现依赖注入

前言

最近也是找了快2周的工作了,收到的面试邀请也就几个,然后有个面试题目是用asp.net mvc + Entityframework 做一个学生信息增删改查系统。因为题目要求了用Entityframework 也就是EF 那也就不上core了,web项目也是用Framework 4.8去做的。

本文的重点是IOC容器,在Framework 中是没有自带的IOC容器的,那么就需要使用第三方库去实现依赖注入,我这里用的是Autofac。

如果不使用IOC容器去管理类,那么操作数据库和使用类方法则是

using(MydbContext db = new MydbContext){
	db....
}
StudentService s = new StudentService();
s.Add();

使用方法

Nuget包

首先需要下载2个Nuget包,分别是:

dotnet add package Autofac --version 7.1.0
dotnet add package Autofac.Mvc5 --version 6.1.0

配置文件

然后在配置文件中,也就是Global.asax.cs文件

然后需要添加如下代码:

// 创建 Autofac 容器生成器
var builder = new ContainerBuilder();

// 注册 EF 上下文
builder.RegisterType<SchoolContext>().InstancePerRequest();

// 注册其他服务
builder.RegisterType<StudentService>().As<IStudentService>().InstancePerRequest();
// 注册控制器
builder.RegisterControllers(typeof(HomeController).Assembly);

// 构建容器
var container = builder.Build();
// 设置 ASP.NET MVC 的依赖解析器为 Autofac
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

上面我注入了一个SchoolContext数据库上下文服务,用于操作数据库

然后注册了StudentService服务,里面是增删改查代码

举个例子:

public interface IStudentService{
	//删除
	Task<int> DelAsync(int id);
}
public class StudentService:IStudentService
 {
        private readonly SchoolContext _dbContext;

        public StudentService(SchoolContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task<int> DelAsync(int id)
        {
            var student = _dbContext.Students.Include("Score").FirstOrDefault(s => s.Id == id);
            if (student != null)
            {
                // 删除关联的成绩表
                if (student.Score != null)
                {
                    _dbContext.Scores.Remove(student.Score);
                }

                // 删除学生
                _dbContext.Students.Remove(student);
                return await _dbContext.SaveChangesAsync();
            }
            return 0;
    }
}

上面StudentService类实现了IStudentService接口的方法,并且注入了SchoolContext依赖进行数据库操作。

 public class HomeController : Controller
 {
        private readonly IStudentService _studentService;
        public HomeController(IStudentService studentService)
        {
            _studentService = studentService;
        }
        public async Task<ActionResult> DelStudent(int id)
        {
            int result = await _studentService.DelAsync(id);
            if (result > 0)
            {
                TempData["SuccessMessage"] = "学生信息删除成功";
                return RedirectToAction("Index");   
            }
            TempData["SuccessMessage"] = "学生信息删除失败";
            return RedirectToAction("Index");   
        }
}

上面的控制器则是注入了IStudentService然后就可以调用它的删除学生信息的方法了。

我们需要注意的是需要把数据库上下文和服务类交给容器去管理。

// 注册 EF 上下文
builder.RegisterType<SchoolContext>().InstancePerRequest();
// 注册其他服务
builder.RegisterType<StudentService>().As<IStudentService>().InstancePerRequest();
// 注册控制器
builder.RegisterControllers(typeof(HomeController).Assembly);

同时也要注册控制器,一开始我去写的的时候没有注册控制器,然后会报构造函数不能为空的错误!

生命周期

  1. InstancePerDependency:每次解析时都创建一个新的实例。这是默认的生命周期管理方式。
  2. SingleInstance:整个应用程序中只创建一个实例,并在后续的解析中重用该实例。
  3. InstancePerLifetimeScope:每个生命周期范围内只创建一个实例。生命周期范围可以通过Autofac的BeginLifetimeScope()方法创建。
  4. InstancePerMatchingLifetimeScope:与InstancePerLifetimeScope类似,但只有在解析时与指定的生命周期范围匹配时才会创建实例。
  5. InstancePerRequest:在Web应用程序中,每个HTTP请求都创建一个新的实例。这通常用于在Web API或MVC应用程序中注册服务。
  6. InstancePerOwned:在每个Owned<T>上创建一个新的实例。Owned<T>是一个特殊的类型,用于在需要时创建和释放实例。

参考资料

posted @ 2023-09-12 12:49  妙妙屋(zy)  阅读(396)  评论(0编辑  收藏  举报