欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用SqlSugarClient進行數據訪問并實現了統一的批量依賴注入(示例代碼)

 更新時間:2024年05月05日 10:15:24   作者:摩詰  
M為 BaseDto 請用C# 給出一個案例,支持不同表對應不同的業(yè)務邏輯層,然后不同倉儲實例,不同表的業(yè)務,都實現統一的批量依賴注入,下面通過示例給大家演示如何使用SqlSugarClient進行數據訪問,并實現了統一的批量依賴注入,感興趣的朋友跟隨小編一起看看吧

倉儲層當前有接口 IRepository<T> 抽象類  BaseRepository<T> 業(yè)務邏輯層有抽象類 BaseBusiness<M, E> 接口 IBusiness<M, E>,其中使用ISqlSugarClient,其中還有E表示 BaseEntity,M為 BaseDto 請用C# 給出一個案例,支持不同表對應不同的業(yè)務邏輯層,然后不同倉儲實例,不同表的業(yè)務,都實現統一的批量依賴注入。

以下是一個更新后的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);
    }
    // 其他倉儲操作方法的實現...
}
// 業(yè)務邏輯層
public interface IBusiness<M, E> where M : BaseDto where E : BaseEntity
{
    void Process(M model);
    // 其他業(yè)務邏輯方法...
}
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è)務邏輯方法...
}
// 具體業(yè)務邏輯類
public class ProductBusiness : BaseBusiness<ProductDto, ProductEntity>
{
    public ProductBusiness(IRepository<ProductEntity> repository) : base(repository)
    {
    }
    public override void Process(ProductDto model)
    {
        // 實現產品業(yè)務邏輯
        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)
    {
        // 實現客戶業(yè)務邏輯
        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);
        // 解析并實例化業(yè)務邏輯類
        var businesses = injector.ResolveBusinesses<BaseDto, BaseEntity>();
        // 使用業(yè)務邏輯類進行操作
        foreach (var business in businesses)
        {
            // 處理業(yè)務邏輯
            business.Process(new ProductDto { Name = "Sample Product" });
            business.Process(new CustomerDto { FirstName = "John", LastName = "Doe" });
        }
    }
}

到此這篇關于C#使用SqlSugarClient進行數據訪問并實現了統一的批量依賴注入(示例代碼)的文章就介紹到這了,更多相關SqlSugarClient批量依賴注入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 新手必看Unity2019 2020保姆級安裝教程

    新手必看Unity2019 2020保姆級安裝教程

    這篇文章主要介紹了Unity2019 2020安裝教程,本文分步驟通過圖文并茂的形式給大家介紹Unity2019 2020安裝方法,需要的朋友可以參考下
    2021-05-05
  • 詳解LINQ入門(下篇)

    詳解LINQ入門(下篇)

    這篇文章主要介紹了詳解LINQ入門(下篇),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • C# 禁用鼠標中間鍵的方法

    C# 禁用鼠標中間鍵的方法

    關于?。? System.Windows.Forms.NumericUpDown 控件,如何禁用鼠標中間鍵?
    2013-03-03
  • 小菜編程成長記(一 面試受挫——代碼無錯就是好?)

    小菜編程成長記(一 面試受挫——代碼無錯就是好?)

    小菜編程成長記(一 面試受挫——代碼無錯就是好?)...
    2006-10-10
  • C#之Socket(套接字)通信

    C#之Socket(套接字)通信

    這篇文章介紹了C#之Socket(套接字)通信,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C# GetMethod方法的應用實例講解

    C# GetMethod方法的應用實例講解

    GetMethod 是獲取當前 Type 的特定方法,具有多個重載, GetMethod 即使用指定的綁定約束搜索指定方法,本文給大家介紹了C# GetMethod方法的應用實例,需要的朋友可以參考下
    2024-04-04
  • C#實現動態(tài)數據繪圖graphic的方法示例

    C#實現動態(tài)數據繪圖graphic的方法示例

    這篇文章主要介紹了C#實現動態(tài)數據繪圖graphic的方法,結合實例形式分析了C#根據動態(tài)數據繪制2D數據表格的相關操作技巧,需要的朋友可以參考下
    2017-09-09
  • C#驗證用戶輸入信息是否包含危險字符串的方法

    C#驗證用戶輸入信息是否包含危險字符串的方法

    這篇文章主要介紹了C#驗證用戶輸入信息是否包含危險字符串的方法,可針對and、or、exec、insert、select等SQL操作技巧進行過濾操作,非常具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C#隊列Queue用法實例分析

    C#隊列Queue用法實例分析

    這篇文章主要介紹了C#隊列Queue用法,實例分析了隊列的功能、定義及相關使用技巧,需要的朋友可以參考下
    2015-05-05
  • C#并發(fā)編程之Task類詳解

    C#并發(fā)編程之Task類詳解

    Task是建立在線程池之上的一種多線程技術,它的出現使Thread成為歷史。其使用方法非常簡單,本文就來通過幾個示例為大家講講它的具體使用吧
    2023-03-03

最新評論