一、准备工作
通过程序包管理器控制台安装AutoFac:
Install-Package Autofac.Extensions.DependencyInjection
创建新类库(.NetCore 2.2类库),存放接口跟实现类,命名为NetCoreWebApi.Repository。
创建用户仓储接口
在类库项目上右键->添加->新建文件夹,命名为Interface,存放接口类。在Interface文件夹下面新建类:IUserRepository,属性如下:
using System.Collections.Generic; using NetCoreWebApi.Model.Models; namespace NetCoreWebApi.Repository.Interface { /// <summary> /// 用户接口 /// </summary> public interface IUserRepository { /// <summary> /// 添加用户 /// </summary> /// <param name="entity">实体对象</param> int Add(TbUser entity); /// <summary> /// 删除用户 /// </summary> /// <param name="entity">实体对象</param> int Remove(TbUser entity); /// <summary> /// 编辑用户 /// </summary> /// <param name="entity">实体对象</param> /// <returns></returns> int Update(TbUser entity); /// <summary> /// 获取所有 /// </summary> /// <returns></returns> IList<TbUser> GetAll(); } }
创建用户接口实现类
在类库项目上右键->添加->新建文件夹,命名为Implement,存放接口实现类。在Implement文件夹下面新建类:UserRepository,属性如下:
using System.Collections.Generic; using System.Linq; using NetCoreWebApi.Model; using NetCoreWebApi.Model.Models; using NetCoreWebApi.Repository.Interface; namespace NetCoreWebApi.Repository.Implement { /// <summary> /// 业务处理 /// </summary> public class UserRepository:IUserRepository { private readonly MyDbContext _dbContext; /// <summary> /// 构造函数 /// </summary> /// <param name="dbContext"></param> public UserRepository(MyDbContext dbContext) { _dbContext = dbContext; } /// <summary> /// 添加用户 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Add(TbUser entity) { _dbContext.TbUsers.Add(entity); return _dbContext.SaveChanges(); } /// <summary> /// 删除用户 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Remove(TbUser entity) { _dbContext.TbUsers.Remove(entity); return _dbContext.SaveChanges(); } /// <summary> /// 编辑用户 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Update(TbUser entity) { return _dbContext.SaveChanges(); } /// <summary> /// 查询用户 /// </summary> /// <returns></returns> public IList<TbUser> GetAll() { return _dbContext.TbUsers.ToList(); } } }
二、配置注入
打开Startup.cs类
把ConfigureServices方法的返回值由void变为IServiceProvider
public static IContainer ApplicationContainer { get; set; } /// <summary> /// //负责注入服务 /// </summary> /// <param name="services"></param> /// <returns></returns> public IServiceProvider ConfigureServices(IServiceCollection services) { var connectionStr = Configuration.GetConnectionString("SqlServer"); services.AddDbContext<MyDbContext> (options => options.UseSqlServer(connectionStr, e => e.MigrationsAssembly("NetCoreWebApi.Model"))); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //初始化容器 var builder = new ContainerBuilder(); //管道寄居 builder.Populate(services); //注册仓储,IUserRepository接口到UserRepository的映射 builder.RegisterType<UserRepository>().As<IUserRepository>(); //构造 ApplicationContainer = builder.Build(); //将AutoFac反馈到管道中 return new AutofacServiceProvider(ApplicationContainer); }
三、测试
在项目上右键->添加->新建文件夹,命名为Controllers,存放相应的控制器。在Controllers文件夹下面新建一个控制器:UserController,如下:
using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using NetCoreWebApi.Model.Models; using NetCoreWebApi.Repository.Interface; namespace NetCoreWebApi.Controllers { /// <summary> /// 用户模块 /// </summary> [Route("api/user")] [ApiController] public class UserController : ControllerBase { private readonly IUserRepository _userRepository; /// <summary> /// 构造函数 /// </summary> /// <param name="userRepository"></param> public UserController(IUserRepository userRepository) { _userRepository = userRepository; } /// <summary> /// 创建用户 /// </summary> /// <returns></returns> [Route("createUser")] [HttpPost] public TbUser CreateUser() { var user = new TbUser { UserId = Guid.NewGuid().ToString("N"), CreateTime = DateTime.Now, UserName = "tenghao", Email = "tenghao510@qq.com" }; _userRepository.Add(user); return user; } /// <summary> /// 查询用户 /// </summary> /// <returns></returns> [Route("getUser")] [HttpGet] public IList<TbUser> GetUser() { return _userRepository.GetAll(); } } }
Ctrl+F5 运行之后,先用Postman调创建用户接口
接下来测试下查询用户
好了,你们自己测下写的有没有问题。