一、準(zhǔn)備工作通過程序包管理器控制臺(tái)安裝AutoFac: Install-Package Autofac.Extensions.DependencyInjection 創(chuàng)建新類庫(kù)(.NetCore 2.2類庫(kù)),存放接口跟實(shí)現(xiàn)類,命名為NetCoreWebApi.Repository。 創(chuàng)建用戶倉(cāng)儲(chǔ)接口在類庫(kù)項(xiàng)目上右鍵->添加->新建文件夾,命名為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">實(shí)體對(duì)象</param> int Add(TbUser entity); /// <summary> /// 刪除用戶 /// </summary> /// <param name="entity">實(shí)體對(duì)象</param> int Remove(TbUser entity); /// <summary> /// 編輯用戶 /// </summary> /// <param name="entity">實(shí)體對(duì)象</param> /// <returns></returns> int Update(TbUser entity); /// <summary> /// 獲取所有 /// </summary> /// <returns></returns> IList<TbUser> GetAll(); } } 創(chuàng)建用戶接口實(shí)現(xiàn)類在類庫(kù)項(xiàng)目上右鍵->添加->新建文件夾,命名為Implement,存放接口實(shí)現(xiàn)類。在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> /// 業(yè)務(wù)處理 /// </summary> public class UserRepository:IUserRepository { private readonly MyDbContext _dbContext; /// <summary> /// 構(gòu)造函數(shù) /// </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變?yōu)镮ServiceProvider public static IContainer ApplicationContainer { get; set; } /// <summary> /// //負(fù)責(zé)注入服務(wù) /// </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); //注冊(cè)倉(cāng)儲(chǔ),IUserRepository接口到UserRepository的映射 builder.RegisterType<UserRepository>().As<IUserRepository>(); //構(gòu)造 ApplicationContainer = builder.Build(); //將AutoFac反饋到管道中 return new AutofacServiceProvider(ApplicationContainer); } 三、測(cè)試在項(xiàng)目上右鍵->添加->新建文件夾,命名為Controllers,存放相應(yīng)的控制器。在Controllers文件夾下面新建一個(gè)控制器: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> /// 構(gòu)造函數(shù) /// </summary> /// <param name="userRepository"></param> public UserController(IUserRepository userRepository) { _userRepository = userRepository; } /// <summary> /// 創(chuàng)建用戶 /// </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 運(yùn)行之后,先用Postman調(diào)創(chuàng)建用戶接口
接下來測(cè)試下查詢用戶 好了,你們自己測(cè)下寫的有沒有問題。 |
|