API是提供数据的接口,也可以理解为是一个方法,不过这个方法是对外公开的,可以通过Http请求来调用;
我们首先要知道的是数据都是存在数据里面的,所以我们需要一个访问数据的帮助类来提升我们访问数据库的效率;
在Microsoft.EntityFrameworkCore有一个DbContext类,提供了许多有关数据库操作的方法,增删改查等等,基本上你能想到的都有;
2.按照规范创建一个API数据帮助有了之后,我们改通过怎么样的方式来调用这些帮助方法,来达到操作数据库的目的呢?
- 首先你需要创建一个API控制器
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace BusinessIntelligence.Api.Controllers { ////// 第一个控制器 /// [Route("api/[controller]")] [ApiController] public class FirstController : ControllerBase { } }
PS:如果你是一个空的项目,框架还没有搭建起来,你还需要创建一个ASP.NET Core Web API项目,然后在项目里面创建控制器
2.创建一个Action`
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace BusinessIntelligence.Api.Controllers { ////// 第一个控制器 /// [Route("api/[controller]")] [ApiController] public class FirstController : ControllerBase { ////// 第一个方法 /// ///public dynamic FirstAction() { return 1; } } }
3.创建一个接口类
为什么要创建接口类呢?
解耦,好扩展,高内聚,低耦合这些都是接口的好处,但是前面这些我也不是很懂;
简单来说就是你写了一个方法,这个方法能在很多的地方用到,为了方便调用,把接口放到了一个容易调用的地方;
这对代码分类也很有帮助,为了不让代码堆积在一起;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BusinessIntelligence.Interface { public interface IFirstInterface { bool List(); bool Add(); bool Update(); bool Delete(); } }
到这里我们成功的定义了一个接口,里面有4个方法,
List是列表,Add是添加,Update是更新,Delete是删除;
接下来我要说明的是,接口是抽象的,没有具体实现的,公共的;
没有具体实现肯定是不行的,我们还没有调用数据库操作方法;
所以我们要实现这个接口,创建一个实现他的类
4.创建一个实现接口的类
using BusinessIntelligence.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BusinessIntelligence.Service { public class FiastService : IFirstInterface { public bool Add() { throw new NotImplementedException(); } public bool Delete() { throw new NotImplementedException(); } public bool List() { throw new NotImplementedException(); } public bool Update() { throw new NotImplementedException(); } } }
这就是一个实现了IFirstInterface接口的类,在这里我们可以定义增删改查的具体实现
PS:创建这个类也是有要求的,一般来说,解决方案里面会有一个专门存放这些操作数据库的类;
接下来我们讲怎么调用操作数据库的方法(微软提供那个),
5.调用操作数据库的方法
using BusinessIntelligence.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BusinessIntelligence.SqlServerHelper; using BusinessIntelligence.Entities.SqlServer; namespace BusinessIntelligence.Service { public class FirstService : IFirstInterface { private readonly IRepository_repository; public FirstService(IRepository repository) { _repository = repository; } /// /// 列表 /// ///public List List() { return _repository.GetAll().ToList(); } } }
这里我创建了一个私有的,只读的IRepository类型的一个变量,
这个类我们一般管它叫仓储(Repository),
在构造函数里面我们给这个变量赋值,这样就可以调用仓促中的方法了,
_repository.GetAll().ToList(),这段代码就是,调用仓储中的GetAll(获取所有数据)方法,并且转换成List列表的形式返回出去;
可以看到我改变了这个方法的返回值类型,来接受返回的数据,需要注意的是,接口FirstInterface,里面的这个方法的返回值类型也需要同步改变;
到这里我们一个方法的具体实现就完成了,在Controller里面调用这个方法,一个API就基本实现了;
6.在控制器中调用实现好的方法
using BusinessIntelligence.Interface; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace BusinessIntelligence.Api.Controllers { ////// 第一个控制器 /// [Route("api/[controller]")] [ApiController] public class FirstController : ControllerBase { private readonly IFirstInterface _firstInterface; ////// 构造函数 /// /// public FirstController(IFirstInterface firstInterface) { _firstInterface = firstInterface; } ////// 第一个方法 /// ///public dynamic FirstAction() { return _firstInterface.List(); } } }
这里跟上一步很像,创建一个私有的,只读的,IFirstInterface(接口)的变量,然后在构造函数中给他赋值,然后调用这个接口下面的方法,一个简单的API就完成了;
进阶知识这里有一些我的文章中没有提到的一些进阶知识,感兴趣的可以搜索一下
依赖注入,DbContext的配置;