C#使用SqlSugarClient进行数据访问并实现了统一的批量依赖注入(示例代码)
作者:摩诘
M为 BaseDto 请用C# 给出一个案例,支持不同表对应不同的业务逻辑层,然后不同仓储实例,不同表的业务,都实现统一的批量依赖注入,下面通过示例给大家演示如何使用SqlSugarClient进行数据访问,并实现了统一的批量依赖注入,感兴趣的朋友跟随小编一起看看吧
仓储层当前有接口 IRepository<T> 抽象类 BaseRepository<T> 业务逻辑层有抽象类 BaseBusiness<M, E> 接口 IBusiness<M, E>,其中使用ISqlSugarClient,其中还有E表示 BaseEntity,M为 BaseDto 请用C# 给出一个案例,支持不同表对应不同的业务逻辑层,然后不同仓储实例,不同表的业务,都实现统一的批量依赖注入。
以下是一个更新后的C#示例,演示了如何使用ISqlSugarClient进行数据访问,并实现了统一的批量依赖注入:
using System; using System.Collections.Generic; using SqlSugar; // 实体类 public class BaseEntity { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } // 其他实体属性... } public class ProductEntity : BaseEntity { public string Name { get; set; } // 其他产品实体属性... } public class CustomerEntity : BaseEntity { public string FirstName { get; set; } public string LastName { get; set; } // 其他客户实体属性... } // 数据传输对象(DTO) public class BaseDto { // 公共DTO属性... } public class ProductDto : BaseDto { public string Name { get; set; } // 其他产品DTO属性... } public class CustomerDto : BaseDto { public string FirstName { get; set; } public string LastName { get; set; } // 其他客户DTO属性... } // 仓储层 public interface IRepository<T> { void Add(T entity); void Update(T entity); void Delete(T entity); T GetById(int id); // 其他仓储操作方法... } public abstract class BaseRepository<T> : IRepository<T> where T : BaseEntity { protected readonly ISqlSugarClient _db; public BaseRepository(ISqlSugarClient db) { _db = db; } public void Add(T entity) { _db.Insertable(entity).ExecuteCommand(); } public void Update(T entity) { _db.Updateable(entity).ExecuteCommand(); } public void Delete(T entity) { _db.Deleteable<T>().In(entity.Id).ExecuteCommand(); } public T GetById(int id) { return _db.Queryable<T>().InSingle(id); } // 其他仓储操作方法的实现... } // 业务逻辑层 public interface IBusiness<M, E> where M : BaseDto where E : BaseEntity { void Process(M model); // 其他业务逻辑方法... } public abstract class BaseBusiness<M, E> : IBusiness<M, E> where M : BaseDto where E : BaseEntity { protected readonly IRepository<E> _repository; public BaseBusiness(IRepository<E> repository) { _repository = repository; } public abstract void Process(M model); // 其他业务逻辑方法... } // 具体业务逻辑类 public class ProductBusiness : BaseBusiness<ProductDto, ProductEntity> { public ProductBusiness(IRepository<ProductEntity> repository) : base(repository) { } public override void Process(ProductDto model) { // 实现产品业务逻辑 Console.WriteLine("Processing product: " + model.Name); } } public class CustomerBusiness : BaseBusiness<CustomerDto, CustomerEntity> { public CustomerBusiness(IRepository<CustomerEntity> repository) : base(repository) { } public override void Process(CustomerDto model) { // 实现客户业务逻辑 Console.WriteLine("Processing customer: " + model.FirstName + " " + model.LastName); } } // 批量依赖注入容器 public class DependencyInjector { private readonly ISqlSugarClient _db; public DependencyInjector(ISqlSugarClient db) { _db = db; } public IEnumerable<BaseBusiness<M, E>> ResolveBusinesses<M, E>() where M : BaseDto where E : BaseEntity { var repositoryType = typeof(IRepository<E>); var businessType = typeof(IBusiness<M, E>); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var businessTypes = new List<Type>(); foreach (var assembly in assemblies) { var types = assembly.GetTypes(); foreach (var type in types) { if (type.BaseType != null && type.BaseType.IsGenericType) { var baseType = type.BaseType.GetGenericTypeDefinition(); if (baseType == businessType) { businessTypes.Add(type); } } } } foreach (var businessTypeItem in businessTypes) { var repositoryGenericType = repositoryType.MakeGenericType(businessTypeItem.GetGenericArguments()); var repository = Activator.CreateInstance(repositoryGenericType, _db); var business = Activator.CreateInstance(businessTypeItem, repository); yield return (BaseBusiness<M, E>)business; } } } // 使用示例 class Program { static void Main(string[] args) { // 模拟ISqlSugarClient的实例 var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "YourConnectionString", DbType = DbType.SqlServer, IsAutoCloseConnection = true, }); // 实例化依赖注入容器 var injector = new DependencyInjector(db); // 解析并实例化业务逻辑类 var businesses = injector.ResolveBusinesses<BaseDto, BaseEntity>(); // 使用业务逻辑类进行操作 foreach (var business in businesses) { // 处理业务逻辑 business.Process(new ProductDto { Name = "Sample Product" }); business.Process(new CustomerDto { FirstName = "John", LastName = "Doe" }); } } }
到此这篇关于C#使用SqlSugarClient进行数据访问并实现了统一的批量依赖注入(示例代码)的文章就介绍到这了,更多相关SqlSugarClient批量依赖注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!