日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

如何使用ISqlSugarClient进行数据访问,并实现了统一的批量依赖注入

發布時間:2025/3/8 16 如意码农
生活随笔 收集整理的這篇文章主要介紹了 如何使用ISqlSugarClient进行数据访问,并实现了统一的批量依赖注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

倉儲層當前有接口 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" });
}
}
}

原創,轉載請說明出處。

總結

以上是生活随笔為你收集整理的如何使用ISqlSugarClient进行数据访问,并实现了统一的批量依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。