Admin.Net第七章:二次开发实战-创建自定义模块
目录#
1. 二次开发概述#
1.1 开发模式选择#
Admin.NET推荐的二次开发模式是创建独立的应用层项目,这样可以:
- 与主框架解耦,便于升级
- 独立管理业务代码
- 保持代码结构清晰
推荐项目结构:
Admin.NET/
├── Admin.NET.Core/ # 核心层(不修改)
├── Admin.NET.Application/ # 示例应用层(参考用)
├── Admin.NET.Web.Core/ # Web核心层(不修改)
├── Admin.NET.Web.Entry/ # Web入口层
├── MyCompany.Application/ # 自定义应用层(新建)
│ ├── Entity/ # 业务实体
│ ├── Service/ # 业务服务
│ ├── Dto/ # 数据传输对象
│ ├── EventBus/ # 事件处理
│ └── Const/ # 常量定义
└── Plugins/ # 插件项目
1.2 创建应用层项目#
步骤1:创建类库项目
dotnet new classlib -n MyCompany.Application -f net8.0
步骤2:添加项目引用
在MyCompany.Application.csproj中添加:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- 引用Core层 -->
<ProjectReference Include="..\Admin.NET.Core\Admin.NET.Core.csproj" />
</ItemGroup>
</Project>
步骤3:在Web.Entry中引用
<ItemGroup>
<ProjectReference Include="..\MyCompany.Application\MyCompany.Application.csproj" />
</ItemGroup>
步骤4:创建GlobalUsings.cs
// MyCompany.Application/GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.ComponentModel;
global using System.ComponentModel.DataAnnotations;
global using System.Linq;
global using System.Threading.Tasks;
global using Admin.NET.Core;
global using Furion.DynamicApiController;
global using Mapster;
global using Microsoft.AspNetCore.Mvc;
global using SqlSugar;
2. 创建业务实体#
2.1 实体设计原则#
- 继承合适的基类(EntityBase、EntityBaseData、EntityTenant)
- 使用SqlSugar特性标注
- 添加注释和数据校验
- 考虑索引设计
2.2 创建实体类#
// MyCompany.Application/Entity/Product.cs
namespace MyCompany.Application;
/// <summary>
/// 产品表
/// </summary>
[SugarTable(null, "产品表")]
public class Product : EntityTenant
{
/// <summary>
/// 产品编码
/// </summary>
[SugarColumn(ColumnDescription = "产品编码", Length = 64)]
[Required, MaxLength(64)]
public string Code { get; set; }
/// <summary>
/// 产品名称
/// </summary>
[SugarColumn(ColumnDescription = "产品名称", Length = 128)]
[Required, MaxLength(128)]
public string Name { get; set; }
/// <summary>
/// 产品分类Id
/// </summary>
[SugarColumn(ColumnDescription = "产品分类Id")]
public long CategoryId { get; set; }
/// <summary>
/// 产品分类
/// </summary>
[Navigate(NavigateType.OneToOne, nameof(CategoryId))]
public ProductCategory Category { get; set; }
/// <summary>
/// 规格型号
/// </summary>
[SugarColumn(ColumnDescription = "规格型号", Length = 256)]
public string? Specification { get; set; }
/// <summary>
/// 单位
/// </summary>
[SugarColumn(ColumnDescription = "单位", Length = 16)]
public string? Unit { get; set; }
/// <summary>
/// 单价
/// </summary>
[SugarColumn(ColumnDescription = "单价", DecimalDigits = 2)]
public decimal Price { get; set; }
/// <summary>
/// 库存数量
/// </summary>
[SugarColumn(ColumnDescription = "库存数量")]
public int StockQty { get; set; }
/// <summary>
/// 产品图片
/// </summary>
[SugarColumn(ColumnDescription = "产品图片", Length = 512)]
public string? ImageUrl { get; set; }
/// <summary>
/// 产品描述
/// </summary>
[SugarColumn(ColumnDescription