ZRAdmin项目学习二

423 阅读2分钟

一 各个模块调用流转

前端ui-->控制器层-->基础服务接口层-->仓储层-->sqlsugar数据层 model 层贯穿多个层。

对应 前端-->ArticleController-->IArticleService-->IBaseRepository--> ISimpleClient

1. 服务层

注意的地方是类上面的特性

[AppService(ServiceType = typeof(IArticleService), ServiceLifetime = LifeTime.Transient)] 这里有服务层接口和生命周期 在这个类里面调用 AppServiceExtensions--它又在Startup里调用

    /// <summary>
    /// 
    /// </summary>
    [AppService(ServiceType = typeof(IArticleService), ServiceLifetime = LifeTime.Transient)]
    public class ArticleService : BaseService<Article>, IArticleService
    {
    }

2. 仓储层

同样需要注意他们上面的特性写法

        /// <summary>
    /// 文章管理
    /// </summary>
    [AppService(ServiceLifetime = LifeTime.Transient)]
    public class ArticleRepository : BaseRepository<Article>
    {
        
    }

二 依赖的包

ZR.Admin.WebApi 依赖的包情况

image.png image.png

三 model 层

3.1 基类

这个类是一些表共有的东西,比如创建人,创建时间,修改人修改时间等

EpplusTable 这个是控制导入导出属性的

    [EpplusTable(PrintHeaders = true, AutofitColumns = true, AutoCalculate = true, ShowTotal = true)]
    public class SysBase
    {
        [SugarColumn(IsOnlyIgnoreUpdate = true)]//设置后修改不会有此字段
        [JsonProperty(propertyName: "CreateBy")]
        [EpplusIgnore]
        public string Create_by { get; set; }

        [SugarColumn(IsOnlyIgnoreUpdate = true)]//设置后修改不会有此字段
        [JsonProperty(propertyName: "CreateTime")]
        [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
        public DateTime Create_time { get; set; } = DateTime.Now;

        [JsonIgnore]
        [JsonProperty(propertyName: "UpdateBy")]
        [SugarColumn(IsOnlyIgnoreInsert = true)]
        [EpplusIgnore]
        public string Update_by { get; set; }

        //[JsonIgnore]
        [SugarColumn(IsOnlyIgnoreInsert = true)]//设置后插入数据不会有此字段
        [JsonProperty(propertyName: "UpdateTime")]
        [EpplusIgnore]
        public DateTime? Update_time { get; set; }

        public string Remark { get; set; }

        /// <summary>
        /// 搜索时间起始时间
        /// </summary>
        /// <summary>
        /// Write:需穿一个bool值,false时insert,update等操作会忽略此列(和Computed的作用差不多,看了源码也没发现与Computed有什么不一样的地方,有了解的朋友可以赐教下哈)
        /// ExplicitKey:指定此列为主键(不自动增长类型例如guid,ExplicitKey与Key地区别下面会详细讲)
        /// Key:指定此列为主键(自动增长主键),可忽略,忽略后默认查找
        /// [Computed]计算属性,打上此标签,对象地insert,update等操作会忽略此列
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        [JsonIgnore]
        [EpplusIgnore]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 用于搜索使用
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        [JsonIgnore]
        [EpplusIgnore]
        public DateTime? EndTime { get; set; }
    }

3.2 部门表

这个对应数据库中的表名 [SugarTable("sys_dept")]

[Tenant("0")] 对应数据库

     /// <summary>
    /// 部门表
    /// </summary>
    [SugarTable("sys_dept")]
    [Tenant("0")]
    public class SysDept: SysBase
    {
        /** 部门ID */ //主键并且是自增
        [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public long DeptId { get; set; }

        /** 父部门ID */
        public long ParentId { get; set; }

        /** 祖级列表 */
        public string Ancestors { get; set; }

        /** 部门名称 */
        public string DeptName { get; set; }

        /** 显示顺序 */
        public int OrderNum { get; set; }

        /** 负责人 */
        public string Leader { get; set; }

        /** 联系电话 */
        public string Phone { get; set; }

        /** 邮箱 */
        public string Email { get; set; }

        /** 部门状态:0正常,1停用 */
        public string Status { get; set; }

        /// <summary>
        /// 删除标志(0代表存在 2代表删除)
        /// </summary>
        [SugarColumn(IsOnlyIgnoreInsert = true)]
        public string DelFlag { get; set; }

        /** 父部门名称 */
        //[SugarColumn(IsIgnore = true)]
        //public string ParentName { get; set; }

        /// <summary>
        /// 子菜单
        /// </summary>
        public List<SysDept> children = new List<SysDept>();
    }