Skip to content

数据库定义

实体定义

c#
[Table("basic_chain")]
public class BasicChain : EntityBase<int>
{
    public string ChainName { get; set; } = string.Empty;

    public string LinkName { get; set; } = string.Empty;

    public string LinKTel { get; set; } = string.Empty;
}

基础服务定义

c#
namespace JingJian.Package.SimpleAService.Services.Interfaces
{
    public interface IBasicChainService : IAppService<BasicChain, int>
    {
        
    }

	public class BasicChainService : AppService<BasicChain, int>, IAppService<BasicChain, int>, IBasicChainService
    {
        public override string CacheKey => "BasicChainList";

        public BasicChainService(
            IRepository<BasicChain, int> repository)
            : base(repository)
        {
        }
    }
}

缓存服务定义

c#
public interface IBasicChainService : ICacheAppService<BasicChain, int>
{ }

public class BasicChainService : CacheAppService<BasicChain, int>, ICacheAppService<BasicChain, int>, IBasicChainService
{
    public override string CacheKey => "BasicChainList";

    public BasicChainService(
        IRepository<BasicChain, int> repository,
        ICache cache)
        : base(repository, cache)
    { }
}

事务使用

在AppService中,可以直接使用事务,因为在Scope中,所有DataContext是共享的,所以事务在所有AppService中均生效。

c#
public async Task TestTransactionAsync()
{
    using (var transaction = BeginTransaction())
    {
        // 业务代码
        // ......
        await transaction.CommitTransactionAsync();
    }
}

使用只读库

只读库使用

c#
[HttpGet("query/readonly")]
public async Task<User> ReadQueryAsync()
{
    return await _userService.FirstOrDefaultAsync(x => true, options => options.UseReadOnlyDataBase = true);
}

[HttpGet("query/readonly2")]
public async Task<User> ReadQuery2Async()
{
    return _userService.ReadOnlyEntities.FirstOrDefault();
}

只读库配置

js
{
  ......
  "ConnectionStrings": {
    "Center_MySql": "......",
    "ReadOnly_MySql": "......" // 只读库链接配置
  },
  ......
}

事务生命周期

c#

public Action CommitSuccess = () => {}

public async Task TestTransactionAsync(commitSuccess: CommitSuccess)
{
    using (var transaction = BeginTransaction())
    {
        // 业务代码
        // ......
        await transaction.CommitTransactionAsync();
    }
}

可使用的生命周期方法

  • BeforeCommit
  • CommitSuccess
  • BeforeRollback
  • RollbackSuccess

数据库索引

在升级时,默认不会升级索引信息,索引信息分别在“行存索引”和“列存索引”中管理。

行存索引

数据库行存索引为标准索引,可在平台端的“行存索引”菜单配置,定期将行存索引批量执行到数据库中

列存索引

数据库列存索引,针对需要sum,count,avg等统计语句的表,添加索引,可在平台端的“列存索引”菜单配置,定期将行存索引批量执行到数据库中

广州宝点数字化科技