简介

SmartSql-Starter

0. Why

  • 拥抱 跨平台 DotNet Core,是时候了。
  • 高性能、高生产力,史上最轻量级的ORM。107kb

1. So SmartSql

  • TargetFrameworks: .NETFramework 4.6 & .NETStandard 2.0
  • SmartSql = MyBatis + Cache(Memory | Redis) + ZooKeeper + R/W Splitting + ……

2. 主要特性 (√ 为已完成,未打 √ 为计划特性)

  • 1 ORM
    • 1.1 Sync
    • 1.2 Async
  • 2 XmlConfig & XmlStatement -> Sql
    • 2.1 SmartSqlMapConfig & SmartSqlMap √ (是的,你猜对了,和MyBatis一样,通过XML配置分离SQL。)
    • 2.2 Config Hot Update ->ConfigWatcher & Reload (配置文件热更新:当你需要修改Sql的时候,直接修改SqlMap配置文件,保存即可。)
  • 3 读写分离 √
    • 3.1 读写分离 √
    • 3.2 多读库 权重筛选 √ (配置多读库,根据读库权重选举读库)
  • 4 日志 √
    • 4.1 基于 Microsoft.Extensions.Logging.Abstractions (当你需要跟踪调试的时候一切都是那么一目了然)
  • 5 Dynamic Repository
    • 5.1 SmartSql.DyRepository √ (解放你的双手,你来定义仓储接口,我来实现数据库访问)
  • 6 查询缓存 √ (热数据缓存,一个配置轻松搞定)
    • 6.1 SmartSql.Cache.Memory √
    • 6.1.1 Fifo √
    • 6.1.2 Lru √
    • 6.2 SmartSql.Cache.Redis √
    • 6.3 缓存事务一致性
  • 7 分布式配置插件
    • 7.1 IConfigLoader √ (配置文件加载器)
    • 7.2 LocalFileConfigLoader √ √ (本地文件配置加载器)
    • 7.2.1 Load SmartSqlMapSource Xml √
    • 7.3.1 Load SmartSqlMapSource Directory √
    • 7.3 SmartSql.ZooKeeperConfig √ (ZooKeeper 分布式配置文件加载器)

3. 性能


BenchmarkDotNet=v0.10.14, OS=Windows 10.0.16299.431 (1709/FallCreatorsUpdate/Redstone3)
Intel Core i7-4710MQ CPU 2.50GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
Frequency=2435768 Hz, Resolution=410.5481 ns, Timer=TSC
.NET Core SDK=2.1.201
  [Host]     : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.7 (CoreCLR 4.6.26328.01, CoreFX 4.6.26403.03), 64bit RyuJIT

ORMTypeMethodMeanErrorStdDevRankGen 0Gen 1Gen 2Allocated
SmartSqlSmartSqlBenchmarksQuery101.6 ms0.2226 ms0.1738 ms12437.50001062.5000375.000013.37 MB
NativeNativeBenchmarksQuery_IsDBNull_GetValue101.7 ms0.4101 ms0.3635 ms12437.50001062.5000375.000013.37 MB
DapperDapperBenchmarksQuery104.4 ms1.3195 ms1.2342 ms23375.00001375.0000625.000017.64 MB
SmartSqlDapperSmartSqlDapperBenchmarksQuery105.7 ms1.1697 ms1.0941 ms33750.00001437.5000625.000019.47 MB
NativeNativeBenchmarksQuery_GetValue_DbNull107.4 ms1.0710 ms1.0018 ms43062.50001187.5000500.000016.42 MB
SqlSugarSqlSugarBenchmarksQuery108.9 ms0.4048 ms0.3787 ms52375.00001000.0000312.500013.09 MB
EFEFBenchmarksSqlQuery110.9 ms0.6922 ms0.6475 ms611062.5000--34.13 MB
ChloeChloeBenchmarksQuery14.5 ms2.2600 ms5.3711 ms72375.00001000.0000312.500013.07 MB
EFEFBenchmarksQuery_NoTracking126.4 ms1.3197 ms1.2344 ms85937.50002250.00001062.500030.16 MB
EFEFBenchmarksSqlQuery_NoTracking148.6 ms0.8290 ms0.7755 ms97437.50002937.50001250.000037.79 MB

4. 安装 (NuGet)

Install-Package SmartSql

5. 常规代码

查询

            ISmartSqlMapper SqlMapper = MapperContainer.Instance.GetSqlMapper();
            SqlMapper.Query<T_Test>(new RequestContext
            {
                Scope = "T_Test",
                SqlId = "GetList",
                Request = new { Ids = new long[] { 1, 2, 3, 4 } }
            });

事务

            try
            {
                ISmartSqlMapper SqlMapper = MapperContainer.Instance.GetSqlMapper();
                SqlMapper.BeginTransaction();
                //BizCode
                SqlMapper.CommitTransaction();
            }
            catch (Exception ex)
            {
                SqlMapper.RollbackTransaction();
                throw ex;
            }

6. 最佳实践

6.1 安装 SmartSql.DIExtension

Install-Package SmartSql.DIExtension

6.2 注入依赖

 services.AddSmartSql();
 services.AddRepositoryFactory();
 services.AddRepositoryFromAssembly((options) =>
 {
    options.AssemblyString = "SmartSql.Starter.Repository";
 });

6.3 定义仓储接口

    /// <summary>
    /// 属性可选: [SqlMap(Scope = "User")] ,不设置 则默认 Scope 模板:I{Scope}Repository
    /// 可传入自定义模板
    /// RepositoryBuilder builder=new RepositoryBuilder("I{Scope}DAL");
    /// </summary>
    public interface IUserRepository
    {
        /// <summary>
        /// 属性可选 [Statement(Execute = ExecuteBehavior.Auto,Id = "Query")]
        /// 默认 Execute:Auto ,自动判断 执行类型
        /// 默认 Id : 方法名
        /// </summary>
        /// <param name="reqParams"></param>
        /// <returns></returns>
        IEnumerable<User> Query(object reqParams);
        long GetRecord(object reqParams);
        User Get(object reqParams);
        long Insert(User entity);
        int Update(User entity);
        int Delete(User enttiy);
    }

6.4 尽情享用

    public class UserService
    {
        private readonly ISmartSqlMapper _smartSqlMapper;
        private readonly IUserRepository _userRepository;

        public UserService(
             ISmartSqlMapper smartSqlMapper
            , IUserRepository userRepository)
        {
            _smartSqlMapper = smartSqlMapper;
            _userRepository = userRepository;
        }

        public long Add(AddRequest request)
        {
            int existsNum = _userRepository.Exists(new { request.UserName });
            if (existsNum > 0)
            {
                throw new ArgumentException($"{nameof(request.UserName)} has already existed!");
            }
            return _userRepository.Add(new Entitiy.User
            {
                UserName = request.UserName,
                Password = request.Password,
                Status = Entitiy.UserStatus.Ok,
                CreationTime = DateTime.Now,
            });
        }

        public void UseTransaction()
        {
            try
            {
                _smartSqlMapper.BeginTransaction();
                //Biz();
                _smartSqlMapper.CommitTransaction();
            }
            catch (Exception ex)
            {
                _smartSqlMapper.RollbackTransaction();
                throw ex;
            }
        }
    }

7. 文档地址

8. 技术交流

点击链接加入QQ群【SmartSql 官方交流群】:604762592

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐