C#使用SqlSugarClient進(jìn)行數(shù)據(jù)訪問并實(shí)現(xiàn)了統(tǒng)一的批量依賴注入(示例代碼)
倉儲(chǔ)層當(dāng)前有接口 IRepository<T> 抽象類 BaseRepository<T> 業(yè)務(wù)邏輯層有抽象類 BaseBusiness<M, E> 接口 IBusiness<M, E>,其中使用ISqlSugarClient,其中還有E表示 BaseEntity,M為 BaseDto 請(qǐng)用C# 給出一個(gè)案例,支持不同表對(duì)應(yīng)不同的業(yè)務(wù)邏輯層,然后不同倉儲(chǔ)實(shí)例,不同表的業(yè)務(wù),都實(shí)現(xiàn)統(tǒng)一的批量依賴注入。
以下是一個(gè)更新后的C#示例,演示了如何使用ISqlSugarClient進(jìn)行數(shù)據(jù)訪問,并實(shí)現(xiàn)了統(tǒng)一的批量依賴注入:
using System;
using System.Collections.Generic;
using SqlSugar;
// 實(shí)體類
public class BaseEntity
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
// 其他實(shí)體屬性...
}
public class ProductEntity : BaseEntity
{
public string Name { get; set; }
// 其他產(chǎn)品實(shí)體屬性...
}
public class CustomerEntity : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
// 其他客戶實(shí)體屬性...
}
// 數(shù)據(jù)傳輸對(duì)象(DTO)
public class BaseDto
{
// 公共DTO屬性...
}
public class ProductDto : BaseDto
{
public string Name { get; set; }
// 其他產(chǎn)品DTO屬性...
}
public class CustomerDto : BaseDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
// 其他客戶DTO屬性...
}
// 倉儲(chǔ)層
public interface IRepository<T>
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
T GetById(int id);
// 其他倉儲(chǔ)操作方法...
}
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);
}
// 其他倉儲(chǔ)操作方法的實(shí)現(xiàn)...
}
// 業(yè)務(wù)邏輯層
public interface IBusiness<M, E> where M : BaseDto where E : BaseEntity
{
void Process(M model);
// 其他業(yè)務(wù)邏輯方法...
}
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);
// 其他業(yè)務(wù)邏輯方法...
}
// 具體業(yè)務(wù)邏輯類
public class ProductBusiness : BaseBusiness<ProductDto, ProductEntity>
{
public ProductBusiness(IRepository<ProductEntity> repository) : base(repository)
{
}
public override void Process(ProductDto model)
{
// 實(shí)現(xiàn)產(chǎn)品業(yè)務(wù)邏輯
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)
{
// 實(shí)現(xiàn)客戶業(yè)務(wù)邏輯
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的實(shí)例
var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "YourConnectionString",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
});
// 實(shí)例化依賴注入容器
var injector = new DependencyInjector(db);
// 解析并實(shí)例化業(yè)務(wù)邏輯類
var businesses = injector.ResolveBusinesses<BaseDto, BaseEntity>();
// 使用業(yè)務(wù)邏輯類進(jìn)行操作
foreach (var business in businesses)
{
// 處理業(yè)務(wù)邏輯
business.Process(new ProductDto { Name = "Sample Product" });
business.Process(new CustomerDto { FirstName = "John", LastName = "Doe" });
}
}
}到此這篇關(guān)于C#使用SqlSugarClient進(jìn)行數(shù)據(jù)訪問并實(shí)現(xiàn)了統(tǒng)一的批量依賴注入(示例代碼)的文章就介紹到這了,更多相關(guān)SqlSugarClient批量依賴注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法
這篇文章主要介紹了WPF 在image控件用鼠標(biāo)拖拽出矩形的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
C# IQueryable<T>揭開表達(dá)式樹的神秘面紗
這篇文章主要介紹了C# IQueryable<T>表達(dá)式樹,對(duì)IQueryable<T>感興趣的同學(xué),必須要仔細(xì)看一下2021-04-04
Visual C#中如何使用IComparable和IComparer接口
這篇文章主要介紹了C#中使用IComparable和IComparer接口,在本例中,該對(duì)象被用作第二個(gè)參數(shù)被傳遞給Array.Sort的接受IComparer實(shí)例的重載方法,需要的朋友可以參考下2023-04-04
C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式小結(jié)【3種方式】
這篇文章主要介紹了C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式,結(jié)合實(shí)例形式總結(jié)分析了使用Hnetcfg.dll、Shell32.dll及setupapi.dll三種啟用與禁用本地網(wǎng)絡(luò)的操作方法,需要的朋友可以參考下2016-07-07

