ASP.NET Core 2.2 WebApi 系列【三】AutoFac 仓储接口的依赖注入
程序员文章站
2022-06-13 07:57:48
一、准备工作 通过程序包管理器控制台安装AutoFac: Install-Package Autofac.Extensions.DependencyInjection 创建新类库(.NetCore 2.2类库),存放接口跟实现类,命名为NetCoreWebApi.Repository。 创建用户仓储 ......
一、准备工作
通过程序包管理器控制台安装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调创建用户接口
接下来测试下查询用户
好了,你们自己测下写的有没有问题。
上一篇: 23种常用设计模式之原型模式