文章目录
前言
ABP vNext 框架是通过遵循软件创建现代web应用程序和API,设计更加合理,更加细粒度的模块化设计。地磅无人值守项目 使用vNext版本为Volo.Abp 4.4,数据库SQL Server 2019,也可以使用其他的数据库。如果是SQL Server,版本不能低于2008。
该文章适合对abp有一定了解的读者。本人才疏学浅,欢迎各大道友前来交流、斧正,拜谢。
一、Domain项目创建实体类
以车辆信息为例子
1.创建实体类(该类对应 数据库字段)
using System;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;
namespace FloorScale.Base.Cars
{
/// <summary>
/// 车辆信息
/// </summary>
public class Car : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
public Guid? TenantId { get; set; }
/// <summary>
/// 车牌号码
/// </summary>
public string LicensePlate { get; set; }
/// <summary>
/// 车主姓名
/// </summary>
public string CarOwnerName { get; set; }
/// <summary>
/// 车主身份证
/// </summary>
public string CarOwnerIDCard { get; set; }
/// <summary>
/// 车主电话
/// </summary>
public string CarOwnerPhone { get; set; }
/// <summary>
/// 车辆类型编号
/// </summary>
public Guid? TypeId { get; set; }
/// <summary>
/// 送货单位编号
/// </summary>
public Guid? ShipperId { get; set; }
/// <summary>
/// 收货单位编号
/// </summary>
public Guid? ReceivingUnitId { get; set; }
/// <summary>
/// 物料编号
/// </summary>
public Guid? MaterialId { get; set; }
/// <summary>
/// 物料规格编号
/// </summary>
public Guid? MaterialFormatId { get; set; }
/// <summary>
/// 过磅类型编号
/// </summary>
public Guid? WeightTypeId { get; set; }
/// <summary>
/// 状态
/// </summary>
public bool Status { get; set; }
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Orders { get; set; }
}
}
2.创建表接口类
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
namespace FloorScale.Base.Cars
{
/// <summary>
/// 车辆信息
/// </summary>
public interface ICarRepository :
IRepository<Car, Guid>
{
Task<Car> FindByLicensePlateAsync(string input);
}
}
二、EntityFrameworkCore项目
1.创建表实现类
using FloorScale.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace FloorScale.Base.Cars
{
/// <summary>
/// 车辆信息
/// </summary>
public class CarRepository :
EfCoreRepository<
FloorScaleDbContext,
Car,
Guid>,
ICarRepository
{
public CarRepository(
IDbContextProvider<FloorScaleDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
}
}
创建
namespace FloorScale.EntityFrameworkCore
{
[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class FloorScaleDbContext :
AbpDbContext<FloorScaleDbContext>,
IIdentityDbContext,
ITenantManagementDbContext
{
// ... 加入
public DbSet<Car> Cars { get; set; }
// ...
}
}
三、程序包管理器控制台 创建数据库
就两行命令:
- add-migration “addCars”
- update-database
四、Contracts 项目创建实体映射类
1.创建实体映射类
using System;
using Volo.Abp.Application.Dtos;
namespace FloorScale.Base.Cars.Dtos
{
[Serializable]
public class CarDto : FullAuditedEntityDto<Guid>
{
public Guid? TenantId { get; set; }
/// <summary>
/// 车牌号码
/// </summary>
public string LicensePlate { get; set; }
/// <summary>
/// 车主
/// </summary>
public string CarOwnerName { get; set; }
/// <summary>
/// 车主身份证
/// </summary>
public string CarOwnerIDCard { get; set; }
/// <summary>
/// 车主电话
/// </summary>
public string CarOwnerPhone { get; set; }
/// <summary>
/// 车辆类型编号
/// </summary>
public Guid? TypeId { get; set; }
/// <summary>
/// 车辆类型名称
/// </summary>
public string TypeName { get; set; }
/// <summary>
/// 送货单位编号
/// </summary>
public Guid? ShipperId { get; set; }
/// <summary>
/// 送货单位
/// </summary>
public string ShipperName { get; set; }
/// <summary>
/// 收货单位编号
/// </summary>
public Guid? ReceivingUnitId { get; set; }
/// <summary>
/// 收货单位
/// </summary>
public string ReceivingUnitName { get; set; }
/// <summary>
/// 物料编号
/// </summary>
public Guid? MaterialId { get; set; }
/// <summary>
/// 物料名称
/// </summary>
public string MaterialName { get; set; }
/// <summary>
/// 物料规格编号
/// </summary>
public Guid? MaterialFormatId { get; set; }
/// <summary>
/// 物料规格名称
/// </summary>
public string MaterialFormatName { get; set; }
/// <summary>
/// 过磅类型编号
/// </summary>
public Guid? WeightTypeId { get; set; }
/// <summary>
/// 过磅类型名称
/// </summary>
public string WeightTypeName { get; set; }
/// <summary>
/// 状态
/// </summary>
public bool Status { get; set; }
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Orders { get; set; }
/// <summary>
/// 租户
/// </summary>
public string TenantName { get; set; }
}
}
五、Application 项目创建接口服务
1.创建映射绑定
using FloorScale.Base.WeightTypes;
using FloorScale.Base.WeightTypes.Dtos;
using FloorScale.Base.WeightUnits;
using FloorScale.Base.WeightUnits.Dtos;
using FloorScale.ThirdParty.WechatMinis;
using FloorScale.ThirdParty.WechatMinis.Dtos;
using Volo.Abp.TenantManagement;
namespace FloorScale
{
public class FloorScaleApplicationAutoMapperProfile : Profile
{
public FloorScaleApplicationAutoMapperProfile()
{
// ... 加入
CreateMap<Car, CarDto>();
CreateMap<Car, CarTreeviewDto>()
.ForMember(u => u.Name, options => options.MapFrom(input => input.LicensePlate))
.ForMember(u => u.Code, options => options.MapFrom(input => input.CarOwnerName));
CreateMap<CreateCarDto, Car>();
CreateMap<UpdateCarDto, Car>();
// ...
}
}
}
2.创建实现服务类
服务类会自动带有默认的增删查改接口,要根据自身业务进行重写。
using FloorScale.Base.Materials;
using FloorScale.Base.ReceivingUnits;
using FloorScale.Base.Shippers;
using FloorScale.Base.WeightTypes;
using FloorScale.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
namespace FloorScale.Base.Cars
{
/// <summary>
/// 车辆管理
/// </summary>
[ApiExplorerSettings(GroupName = FloorScaleAppService.SwaggerDocSystemApi)]
[Authorize(FloorScalePermissions.Car.Default)]
public class CarAppService :
CrudAppService<
Car,
CarDto,
Guid,
GetCarListDto,
CreateCarDto,
UpdateCarDto>,
ICarAppService
{
protected override string GetPolicyName { get; set; } = FloorScalePermissions.Car.Default;
protected override string GetListPolicyName { get; set; } = FloorScalePermissions.Car.Default;
protected override string CreatePolicyName { get; set; } = FloorScalePermissions.Car.Create;
protected override string UpdatePolicyName { get; set; } = FloorScalePermissions.Car.Update;
protected override string DeletePolicyName { get; set; } = FloorScalePermissions.Car.Delete;
private readonly ICarRepository _repository;
private readonly CarManager _manager;
private readonly ICarTypeRepository _typeRepository;
private readonly IShipperRepository _shipperRepository;
private readonly IReceivingUnitRepository _receivingUnitRepository;
private readonly IMaterialRepository _materialRepository;
private readonly IMaterialFormatRepository _formatRepository;
private readonly IWeightTypeRepository _weightTypeRepository;
public CarAppService(
ICarRepository repository,
CarManager manager,
ICarTypeRepository typeRepository,
IShipperRepository shipperRepository,
IReceivingUnitRepository receivingUnitRepository,
IMaterialRepository materialRepository,
IMaterialFormatRepository formatRepository,
IWeightTypeRepository weightTypeRepository)
: base(repository)
{
_repository = repository;
_manager = manager;
_typeRepository = typeRepository;
_shipperRepository = shipperRepository;
_receivingUnitRepository = receivingUnitRepository;
_materialRepository = materialRepository;
_formatRepository = formatRepository;
_weightTypeRepository = weightTypeRepository;
}
}
}
六、试运行
总结
Abp vNext 框架真是方便,减轻不少项目开发的工作量。个人感觉不够好的地方是,好像技术难度加大了些。后期估计习惯就好。
项目还有很多相关业务,这里无法一 一 举例,直接放个在线项目吧。
前端:https://dibang.caishiben.com
接口服务:https://scaleapi.caishiben.com
接口文档:https://scaleapi.caishiben.com/swagger/index.html