C#使用SqlSugarClient进行数据访问并实现了统一的批量依赖注入(示例代码)

 更新时间:2024年05月05日 10:15:24   作者:摩诘  
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批量依赖注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • WPF 在image控件用鼠标拖拽出矩形的实现方法

    WPF 在image控件用鼠标拖拽出矩形的实现方法

    这篇文章主要介绍了WPF 在image控件用鼠标拖拽出矩形的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • C#建立测试用例系统的示例代码

    C#建立测试用例系统的示例代码

    这篇文章主要介绍了C#建立测试用例系统的方法,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • c#系列 list详情

    c#系列 list详情

    这篇文章主要介绍了c#系列 list,list 本质是一个数组,。就跟我们操作系统一样,提前申请内存大小。所以我们程序一般都有一个申请内存,实际使用内存,内存碎片这几个概念,下面俩看文章详细内容吧
    2021-10-10
  • C# IQueryable<T>揭开表达式树的神秘面纱

    C# IQueryable<T>揭开表达式树的神秘面纱

    这篇文章主要介绍了C# IQueryable<T>表达式树,对IQueryable<T>感兴趣的同学,必须要仔细看一下
    2021-04-04
  • 浅谈c# WPF中的PreviewTextInput

    浅谈c# WPF中的PreviewTextInput

    这篇文章主要介绍了浅谈c# WPF中PreviewTextInput的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#实现Json文件读写的方法代码

    C#实现Json文件读写的方法代码

    这篇文章主要给大家介绍了关于C#实现Json文件读写的相关资料,C# 读取JSON文件并读取某一个关键字的值可以使用Newtonsoft.Json库,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Visual C#中如何使用IComparable和IComparer接口

    Visual C#中如何使用IComparable和IComparer接口

    这篇文章主要介绍了C#中使用IComparable和IComparer接口,在本例中,该对象被用作第二个参数被传递给Array.Sort的接受IComparer实例的重载方法,需要的朋友可以参考下
    2023-04-04
  • C#实现启用与禁用本地网络的方式小结【3种方式】

    C#实现启用与禁用本地网络的方式小结【3种方式】

    这篇文章主要介绍了C#实现启用与禁用本地网络的方式,结合实例形式总结分析了使用Hnetcfg.dll、Shell32.dll及setupapi.dll三种启用与禁用本地网络的操作方法,需要的朋友可以参考下
    2016-07-07
  • 一句话清晰总结C#的协变和逆变

    一句话清晰总结C#的协变和逆变

    这篇文章介绍了C#协变和逆变的工作原理,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • C# XML文件操作之相机参数保存和读取

    C# XML文件操作之相机参数保存和读取

    XML是可扩展标记语言是一种数据语言,它将数据以一种简单的文本格式存储,可以被人类和几乎任何计算机理解。本文将利用C#实现相机参数读取并保存至XML文件,感兴趣的可以学习一下
    2022-11-11

最新评论