使用了AutoFac做依赖注入的处理
一、nuget加载AutoFac
二、Program.cs中注入代码
#region Autofac 自动注册 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder => { try { var assemblysRepository = Assembly.Load("Randy.Fangv8.Repository"); var assemblysServices = Assembly.Load("Randy.Fangv8.Service"); builder.RegisterAssemblyTypes(assemblysRepository).Where(t => t.Name.StartsWith("TB") && t.Name.EndsWith("Repository")).AsSelf().AsImplementedInterfaces(); builder.RegisterAssemblyTypes(assemblysServices).Where(t => t.Name.StartsWith("TB") && t.Name.EndsWith("Service")).AsSelf().AsImplementedInterfaces(); } catch (Exception ex) { throw new Exception(ex.Message + "\n" + ex.InnerException); } }); #endregion
会自动注册模块Randy.Fangv8.Repository下的所有以TB开头,以Repository的类
会自动注册模块Randy.Fangv8.Service下的所有以TB开头,以Service的类
三、检查文件
文件一、IBaseRepository.cs
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Repository { public interface IBaseRepository<TEntity> where TEntity : class, new () { #region 查询 /// <summary> /// 通过主键ID获取数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<TEntity> GetById<T>(T id); #endregion #region 新增 /// <summary> /// 写入实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public Task<bool> Add(TEntity model); #endregion #region 修改 /// <summary> /// 更新实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public Task<bool> Update(TEntity model); #endregion #region 删除 /// <summary> /// 根据ID删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<bool> DeleteById<T>(T id); /// <summary> /// 根据多个ID删除 /// </summary> /// <param name="ids"></param> /// <returns></returns> public Task<int> DeleteByIds<T>(T[] ids); #endregion } }
文件二 :BaseRepository.cs
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Repository { public class BaseRepository<TEntity>:IBaseRepository<TEntity> where TEntity : class, new () { private ISqlSugarClient _db; /// <summary> /// 构造函数依赖注入 /// </summary> /// <param name="db"></param> public BaseRepository(ISqlSugarClient db) { _db = db; } #region 查询 /// <summary> /// 通过主键ID获取数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<TEntity> GetById<T>(T id) { return await _db.Queryable<TEntity>().InSingleAsync(id); } #endregion #region 新增 /// <summary> /// 写入实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task<bool> Add(TEntity model) { var i = await _db.Insertable(model).ExecuteCommandAsync(); return i > 0; } #endregion #region 修改 /// <summary> /// 更新实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task<bool> Update(TEntity model) { //这种方式会以主键为条件 var i = await _db.Updateable(model).ExecuteCommandAsync(); return i > 0; } #endregion #region 删除 /// <summary> /// 根据ID删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<bool> DeleteById<T>(T id) { var i = await _db.Deleteable<TEntity>().In(id).ExecuteCommandAsync(); return i > 0; } /// <summary> /// 根据多个ID删除 /// </summary> /// <param name="ids"></param> /// <returns></returns> public async Task<int> DeleteByIds<T>(T[] ids) { return await _db.Deleteable<TEntity>().In(ids).ExecuteCommandAsync(); } #endregion } }
文件三:ITBTestRepository.cs
using Randy.Fangv8.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Repository { public interface ITBTestRepository : IBaseRepository<TBTest> { } }
文件四:TBTestRepository.cs
using Randy.Fangv8.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Repository { public class TBTestRepository : BaseRepository<TBTest>, ITBTestRepository { public TBTestRepository(ISqlSugarClient sqlSugar) : base(sqlSugar) { } } }
文件五:IBaseService.cs
using Randy.Fangv8.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Service { public interface IBaseService<TEntity> where TEntity : class, new() { #region 查询 /// <summary> /// 通过主键ID获取数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<TEntity> GetById<T>(T id); #endregion #region 新增 /// <summary> /// 写入实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public Task<bool> Add(TEntity model); #endregion #region 修改 /// <summary> /// 更新实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public Task<bool> Update(TEntity model); #endregion #region 删除 /// <summary> /// 根据ID删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public Task<bool> DeleteById<T>(T id); /// <summary> /// 根据多个ID删除 /// </summary> /// <param name="ids"></param> /// <returns></returns> public Task<int> DeleteByIds<T>(T[] ids); #endregion } }
文件六:BaseService.cs
using Randy.Fangv8.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Service { public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new() { private readonly IBaseRepository<TEntity> _repository; /// <summary> /// 构造函数 /// </summary> /// <param name="repository"></param> public BaseService(IBaseRepository<TEntity> repository) { _repository = repository; } #region 查询 /// <summary> /// 通过主键ID获取数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<TEntity> GetById<T>(T id) { return await _repository.GetById(id); } #endregion #region 新增 /// <summary> /// 写入实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task<bool> Add(TEntity model) { return await _repository.Add(model); } #endregion #region 修改 /// <summary> /// 更新实体数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task<bool> Update(TEntity model) { return await _repository.Update(model); } #endregion #region 删除 /// <summary> /// 根据ID删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<bool> DeleteById<T>(T id) { return await _repository.DeleteById(id); } /// <summary> /// 根据多个ID删除 /// </summary> /// <param name="ids"></param> /// <returns></returns> public async Task<int> DeleteByIds<T>(T[] ids) { return await _repository.DeleteByIds(ids); } #endregion } }
文件七:ITBTestService.cs
using Randy.Fangv8.Model; using Randy.Fangv8.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Service { public interface ITBTestService : IBaseService<TBTest> { } }
文件八:TBTestService.cs
using Randy.Fangv8.Model; using Randy.Fangwv8.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Randy.Fangv8.Service { public class TBTestService : BaseService<TBTest>, ITBTestService { public TBTestService(ITBTestRepository repository) : base(repository) { } } }
四、测试执行
1、定义和注入
//注入配置文件 private readonly IOptionsSnapshot<TenantConfig> _tenantConfig; private readonly ILogger<TestController> _logger; private readonly ISqlSugarClient _db; private readonly ITBTestRepository _testRepository; private readonly ITBTestService _testService; /// <summary> /// 构造函数依赖注入TBTestService testService, /// </summary> /// <param name="tenantConfig"></param> /// <param name="logger"></param> /// <param name="db"></param> /// <param name="testRepository"></param> /// <param name="testService"></param> public TestController(IOptionsSnapshot<TenantConfig> tenantConfig, ILogger<TestController> logger, ISqlSugarClient db, ITBTestRepository testRepository, ITBTestService testService) { _tenantConfig = tenantConfig; _logger = logger; _db = db; _testRepository = testRepository; _testService = testService; }
2、调用
//测试Sqlsugar //var modTest = _db.Queryable<TBTest>().InSingle(1); //return Ok(modTest); //测试Repository读取数据 //var modTest = await _testRepository.GetById<int>(1); //return Ok(modTest); //测试Service读取数据 var modTest = await _testService.GetById<int>(1); return Ok(modTest);
3、结果
(完毕)
标签:AutoFac,Task,13,Fangv8,System,管理软件,Randy,using,public From: https://www.cnblogs.com/randytech/p/17260389.html