【愚公系列】2022年02月 .NET架构班 008-ABP vNext 仓储层和服务层优化
【摘要】 一、GUID的自动生成 1.条件public IGuidGenerator GuidGenerator { get; set; } // Guid生成器 2.使用Product服务层public void Create(CreateProductDto createProductDto){ // 1、AutoMapper自动映射实体 var configuration = ne...
一、GUID的自动生成
1.条件
public IGuidGenerator GuidGenerator { get; set; } // Guid生成器
2.使用
Product服务层
public void Create(CreateProductDto createProductDto)
{
// 1、AutoMapper自动映射实体
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CreateProductDto, Product>();
cfg.CreateMap<ProductImageCreateDto, ProductImage>();
});
IMapper mapper = configuration.CreateMapper();
Product product = new Product(GuidGenerator.Create());
product = mapper.Map<CreateProductDto, Product>(createProductDto, product);
// 1、先查询商品
/*Product product1 = _productRepository.GetProductByName(ProductTitle);
if (product1 != null)
{
throw new Exception("商品名称不能重复");
}*/
// 1、规则判断
// _ProductManager.CreateAsync(createProductDto.ProductTitle);
// 2、创建商品
//_productRepository.Create(product);
// 2.1 abp框架提供仓储实现
// _productRepository.InsertAsync(product);
// 2.2 abp框架提供增删改查
_ProductAbpRepository.InsertAsync(product).Wait();
}
Product模型类
public class Product : FullAuditedAggregateRoot<Guid>
{
// public Guid Id { set; get; } // 商品主键
public string ProductCode { set; get; } //商品编码
public string ProductUrl { set; get; } // 商品主图
public string ProductTitle { set; get; } //商品标题
public string ProductDescription { set; get; } // 图文描述
public decimal ProductVirtualprice { set; get; } //商品虚拟价格
public decimal ProductPrice { set; get; } //价格
public int ProductSort { set; get; } //商品序号
public int ProductSold { set; get; } //已售件数
public int ProductStock { set; get; } //商品库存
public string ProductStatus { set; get; } // 商品状态
public virtual ICollection<ProductImage> ProductImages { get; set; }
public Product()
{
ProductImages = new Collection<ProductImage>();
}
//接受GUID生成
public Product(Guid id):base(id)
{
ProductImages = new Collection<ProductImage>();
}
//接受产品名称
public Product(string ProductTitle)
{
// 业务规则:ProductTitle名称不能为空
if (ProductTitle == null)
{
throw new Exception("商品名称不能为空");
}
ProductImages = new Collection<ProductImage>();
}
/// <summary>
/// 添加商品图片
/// </summary>
public void AddProductImage(Guid ImageId, string ImageUrl)
{
// 1、创建一个商品图片
ProductImage productImage = new ProductImage(ImageId);
productImage.ImageUrl = ImageUrl;
// 2、添加到集合中
ProductImages.Add(productImage);
}
/// <summary>
/// 添加商品图片
/// </summary>
public void RemoveProductImage(Guid productImageId)
{
// 1、判断guid ,然后删除指定商品
// 2、添加到集合中
// ProductImages.Remove(productImage);
}
}
CreateProductDto 模型类
/// <summary>
/// 创建商品Dto
///
/// </summary>
public class CreateProductDto
{
public string ProductUrl { set; get; } // 商品主图
public string ProductTitle { set; get; } //商品标题
public string ProductDescription { set; get; } // 图文描述
public decimal ProductVirtualprice { set; get; } //商品虚拟价格
public decimal ProductPrice { set; get; } //价格
public ProductImageCreateDto[] ProductImages { set; get; } // 商品图片集合
}
public class ProductImageCreateDto
{
public ProductImageCreateDto()
{
Id = Guid.NewGuid();
}
public Guid Id { set; get; } // Guid
public string ImageUrl { set; get; } // 图片url
public string ImageStatus { set; get; } // 图片状态
}
ProductImage 模型类
/// <summary>
/// 商品图片
/// </summary>
public class ProductImage : FullAuditedEntity<Guid>
{
//[Key]
// public Guid Id { set; get; } // 主键
public Guid ProductId { set; get; } // 商品编号
public int ImageSort { set; get; } // 排序
public string ImageStatus { set; get; } // 状态(1:启用,2:禁用)
public string ImageUrl { set; get; } // 图片url
public ProductImage()
{
}
public ProductImage(Guid id):base(id)
{
}
}
二、仓储层优化
/// <summary>
/// 商品仓储
/// 1、做定制化
/// </summary>
public interface IProductAbpRepository : IRepository<Product, Guid>
{
IEnumerable<Product> GetProductByName(string ProductName);
/// <summary>
/// 查询和图片
/// </summary>
/// <returns></returns>
public IEnumerable<Product> GetProductAndImages();
}
/// <summary>
/// 商品仓储实现
/// </summary>
[Dependency(ServiceLifetime.Transient)]
public class ProductAbpRepository : EfCoreRepository<EBusinessDbContext, Product, Guid>, IProductAbpRepository
{
public ProductAbpRepository(
IDbContextProvider<EBusinessDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
public IEnumerable<Product> GetProductAndImages()
{
DbSet<Product> products = GetDbSetAsync().Result;
return products.Include(product => product.ProductImages).ToList();
}
/// <summary>
/// 根据商品名称,查询商品
/// </summary>
/// <param name="ProductName"></param>
/// <returns></returns>
public IEnumerable<Product> GetProductByName(string ProductName)
{
// 1、第一种实现
//EBusinessDbContext eBusinessDbContext = GetDbContextAsync().Result;
// 2、第二种实现,根据名称获取商品
DbSet<Product> products = GetDbSetAsync().Result;
return products.Where(product => product.ProductTitle == ProductName);
}
}
三、服务层优化
/// <summary>
/// 商品服务
/// </summary>
public interface IProductAppService : ICrudAppService<
ProductDto,
Guid,
PagedAndSortedResultRequestDto,
CreateProductDto,
UpdateProductDto>
{
/*IEnumerable<ProductDto> GetProducts();
ProductDto GetProductById(Guid id);
void Create(CreateProductDto createProductDto);
void Update(UpdateProductDto updateProductDto);
void Delete(DeleteProductDto deleteProductDto);
bool ProductExists(Guid id);*/
public List<ProductDto> GetProductAndImages();
public IEnumerable<ProductDto> GetProductByAttr(ProductAttrQueryDto createProductDto);
public ProductTotaLDto GetProductTotals();
}
/// <summary>
/// 商品服务实现
/// </summary>
//[RemoteService(IsEnabled = false)]
public class ProductAppService : CrudAppService<
Product,
ProductDto,
Guid,
PagedAndSortedResultRequestDto,
CreateProductDto,
UpdateProductDto>, IProductAppService
{
public IProductAbpRepository _productAbpRepository;
public ProductAppService(IProductAbpRepository repository)
: base(repository)
{
this._productAbpRepository = repository;
}
//[RemoteService(IsEnabled = false)]
public IEnumerable<ProductDto> GetProductByAttr(ProductAttrQueryDto createProductDto)
{
// 1、商品属性
IEnumerable<Product> products = _productAbpRepository.GetProductByName(createProductDto.productName);
// 2、转换成Dto(这样无法自动转换)
IEnumerable<ProductDto> productDtos =
ObjectMapper.Map<IEnumerable<Product>, IEnumerable<ProductDto>>(products);
return productDtos;
}
/// <summary>
/// 查询商品。同时查询图片
/// </summary>
/// <returns></returns>
public List<ProductDto> GetProductAndImages()
{
// 1、查询所有和图片
IEnumerable<Product> products = _productAbpRepository.GetProductAndImages();
// 2、然后映射
return ObjectMapper.Map<IEnumerable<Product>, List<ProductDto>>(products);
}
/// <summary>
/// 汇总接口
/// </summary>
/// <returns></returns>
public ProductTotaLDto GetProductTotals()
{
throw new NotImplementedException();
}
}
public class EBusinessApplicationAutoMapperProfile : Profile
{
public EBusinessApplicationAutoMapperProfile()
{
/* You can configure your AutoMapper mapping configuration here.
* Alternatively, you can split your mapping configurations
* into multiple profile classes for a better organization. */
CreateMap<Product, ProductDto>();
CreateMap<PagedAndSortedResultRequestDto, Product>();
CreateMap<CreateProductDto, Product>();
CreateMap<UpdateProductDto, Product>();
CreateMap<ProductAttrQueryDto, Product>();
CreateMap<ProductImageDto, ProductImage>();
CreateMap<ProductImage,ProductImageDto>();//配置dto映射
}
}
使用
/// <summary>
/// 商品服务实现
/// </summary>
//[RemoteService(IsEnabled = false)]
public class ProductAppService : CrudAppService<
Product,
ProductDto,
Guid,
PagedAndSortedResultRequestDto,
CreateProductDto,
UpdateProductDto>, IProductAppService
{
public IProductAbpRepository _productAbpRepository;
public ProductAppService(IProductAbpRepository repository)
: base(repository)
{
this._productAbpRepository = repository;
}
//[RemoteService(IsEnabled = false)]
public IEnumerable<ProductDto> GetProductByAttr(ProductAttrQueryDto createProductDto)
{
// 1、商品属性
IEnumerable<Product> products = _productAbpRepository.GetProductByName(createProductDto.productName);
// 2、转换成Dto(这样无法自动转换)
IEnumerable<ProductDto> productDtos =
ObjectMapper.Map<IEnumerable<Product>, IEnumerable<ProductDto>>(products);
return productDtos;
}
/// <summary>
/// 查询商品。同时查询图片
/// </summary>
/// <returns></returns>
public List<ProductDto> GetProductAndImages()
{
// 1、查询所有和图片
IEnumerable<Product> products = _productAbpRepository.GetProductAndImages();
// 2、然后映射
return ObjectMapper.Map<IEnumerable<Product>, List<ProductDto>>(products);
}
/// <summary>
/// 汇总接口
/// </summary>
/// <returns></returns>
public ProductTotaLDto GetProductTotals()
{
throw new NotImplementedException();
}
}
四、其他查下优化
1.子查询包含
public IEnumerable<Product> GetProductAndImages()
{
DbSet<Product> products = GetDbSetAsync().Result;
return products.Include(product => product.ProductImages).ToList();
}
2.全部查询包含
EBusinessDbContext中
builder.Entity<Product>(b =>
{
b.ConfigureByConvention();
b.HasMany(u => u.ProductImages).WithOne().HasForeignKey(ur => ur.ProductId).IsRequired();
});
3.查下懒加载
EBusinessEntityFrameworkCoreModule
Configure<AbpDbContextOptions>(options =>
{
/* The main point to change your DBMS.
* See also EBusinessMigrationsDbContextFactory for EF Core tooling. */
options.UseMySQL();
/*options.Configure<EBusinessDbContext>(context =>
{
context.UseMySQL().UseLazyLoadingProxies();
});*/
});
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)