The warehousing layer currently has an interface IRepository<T> Abstract class BaseRepository<T> The business logic layer has an abstract class BaseBusiness<M, E> interface IBusiness<M, E>, which uses ISqlSugarClient, and E also represents BaseEntity, and M is BaseDto. Please use C# to give a case, which supports different business logic layers corresponding to different tables, and then different warehousing instances and businesses of different tables all realize unified batch dependency injection.
Here is an updated C# example that demonstrates how to use ISqlSugarClient for data access and implements unified batch dependency injection:
using System; using ; using SqlSugar; // Entity classpublic class BaseEntity { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } // Other entity attributes...} public class ProductEntity : BaseEntity { public string Name { get; set; } // Other product entity attributes...} public class CustomerEntity : BaseEntity { public string FirstName { get; set; } public string LastName { get; set; } // Other customer entity attributes...} // Data Transfer Object (DTO)public class BaseDto { // Public DTO attributes...} public class ProductDto : BaseDto { public string Name { get; set; } // Other product DTO attributes...} public class CustomerDto : BaseDto { public string FirstName { get; set; } public string LastName { get; set; } // Other customers DTO attributes...} // Warehouse layerpublic interface IRepository<T> { void Add(T entity); void Update(T entity); void Delete(T entity); T GetById(int id); // Other warehousing operation methods...} 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().ExecuteCommand(); } public T GetById(int id) { return _db.Queryable<T>().InSingle(id); } // Implementation of other warehousing operation methods...} // Business logic layerpublic interface IBusiness<M, E> where M : BaseDto where E : BaseEntity { void Process(M model); // Other business logic methods...} 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); // Other business logic methods...} // Specific business logic classpublic class ProductBusiness : BaseBusiness<ProductDto, ProductEntity> { public ProductBusiness(IRepository<ProductEntity> repository) : base(repository) { } public override void Process(ProductDto model) { // Implement product business logic ("Processing product: " + ); } } public class CustomerBusiness : BaseBusiness<CustomerDto, CustomerEntity> { public CustomerBusiness(IRepository<CustomerEntity> repository) : base(repository) { } public override void Process(CustomerDto model) { // Implement customer business logic ("Processing customer: " + + " " + ); } } // Batch dependency injection containerpublic 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 = (); var businessTypes = new List<Type>(); foreach (var assembly in assemblies) { var types = (); foreach (var type in types) { if ( != null && ) { var baseType = (); if (baseType == businessType) { (type); } } } } foreach (var businessTypeItem in businessTypes) { var repositoryGenericType = (()); var repository = (repositoryGenericType, _db); var business = (businessTypeItem, repository); yield return (BaseBusiness<M, E>)business; } } } // Use exampleclass Program { static void Main(string[] args) { // Simulate an instance of ISqlSugarClient var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "YourConnectionString", DbType = , IsAutoCloseConnection = true, }); // Instantiate the dependency injection container var injector = new DependencyInjector(db); // parse and instantiate business logic classes var businesses = <BaseDto, BaseEntity>(); // Use business logic class to operate foreach (var business in businesses) { // Handle business logic (new ProductDto { Name = "Sample Product" }); (new CustomerDto { FirstName = "John", LastName = "Doe" }); } } }
This is the end of this article about C# using SqlSugarClient for data access and implementing unified batch dependency injection (sample code). For more related SqlSugarClient batch dependency injection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!