Fork me on GitHub

Abpvnext笔记

1,依赖注入

EntityframeworkDemoSchemaDbMigrator : IDemoSchemaDbMigrator, ITransientDependency
约定:实现类后面的命令必须包含DemoSchemaDbMigrator

 

2,appsettings.json

①始终复制

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

②嵌入的资源

  <ItemGroup>
    <None Remove="Templates\Files\Hello.tpl" />
    <EmbeddedResource Include="Templates\Files\Hello.tpl" />
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="Localization\Files\*.json" />
    <Content Remove="Localization\Files\*.json" />
  </ItemGroup>

 

 

 

3,迁移程序执行流程

 

4,Hw_ScheduDbContextModelCreatingExtensions

             builder.Entity<TaskInfo>(b=>{
                b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "TaskInfos");
                b.ConfigureByConvention();
                b.Property(x=>x.Name).HasMaxLength(TaskInfoConsts.MaxNameLength).IsRequired();
                b.Property(x=>x.Remark).HasMaxLength(TaskInfoConsts.MaxRemarkLength);
                b.Property(x=>x.Api).HasMaxLength(TaskInfoConsts.MaxApiLength).IsRequired();
                b.Property(x=>x.Cron).HasMaxLength(TaskInfoConsts.MaxCronLength).IsRequired();
                b.Property(x=>x.Status).IsRequired();
                b.Property(x=>x.SystemInfoId).IsRequired();
                b.Property(x=>x.CreationTime).HasColumnType("datetime").HasDefaultValueSql("now()").IsRequired();
                b.Property(x=>x.LastModificationTime).HasColumnType("datetime").HasDefaultValueSql("now()").IsRequired();
                b.Property(x=>x.DeletionTime).HasColumnType("datetime").HasDefaultValueSql("now()").IsRequired();
                b.HasIndex(x=>x.SystemInfoId);
            });

 

5,dto验证

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using EasyAbp.EShop.Stores.Stores;
using Volo.Abp.ObjectExtending;

namespace EasyAbp.EShop.Orders.Orders.Dtos
{
    [Serializable]
    public class CreateOrderDto : ExtensibleObject, IMultiStore
    {
        [DisplayName("OrderStoreId")]
        public Guid StoreId { get; set; }

        [DisplayName("OrderCustomerRemark")]
        public string CustomerRemark { get; set; }

        [DisplayName("OrderLine")]
        public List<CreateOrderLineDto> OrderLines { get; set; }

        public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            base.Validate(validationContext);
            
            if (OrderLines.Count == 0)
            {
                yield return new ValidationResult(
                    "OrderLines should not be empty.",
                    new[] { "OrderLines" }
                );
            }
            
            if (OrderLines.Any(orderLine => orderLine.Quantity <= 0))
            {
                yield return new ValidationResult(
                    "Quantity should be greater than 0.",
                    new[] { "OrderLines" }
                );
            }
        }
    }
}

 

6,基于资源的授权

 public override async Task<OrderDto> CreateAsync(CreateOrderDto input)
        {
            // Todo: Check if the store is open.

            var productDict = await GetProductDictionaryAsync(input.OrderLines.Select(dto => dto.ProductId).ToList());

            await AuthorizationService.CheckAsync(
                new OrderCreationResource
                {
                    Input = input,
                    ProductDictionary = productDict
                },
                new OrderOperationAuthorizationRequirement(OrderOperation.Creation)
            );

            var productDetailIds = input.OrderLines
                .Select(dto =>
                    productDict[dto.ProductId].GetSkuById(dto.ProductSkuId).ProductDetailId ??
                    productDict[dto.ProductId].ProductDetailId)
                .Where(x => x.HasValue)
                .Select(x => x.Value)
                .ToList();
            
            var productDetailDict = await GetProductDetailDictionaryAsync(productDetailIds);

            // Todo: Can we use IProductDataScopedCache/IProductDetailDataScopedCache instead of productDict/productDetailDict?
            var order = await _newOrderGenerator.GenerateAsync(CurrentUser.GetId(), input, productDict, productDetailDict);

            await DiscountOrderAsync(order, productDict);

            await Repository.InsertAsync(order, autoSave: true);

            return await MapToGetOutputDtoAsync(order);
        }
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace EasyAbp.EShop.Orders.Orders
{
    public abstract class OrderCreationAuthorizationHandler : AuthorizationHandler<OrderOperationAuthorizationRequirement, OrderCreationResource>
    {
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, OrderOperationAuthorizationRequirement requirement,
            OrderCreationResource resource)
        {
            if (requirement.OrderOperation != OrderOperation.Creation)
            {
                return;
            }
            
            await HandleOrderCreationAsync(context, requirement, resource);
        }

        protected abstract Task HandleOrderCreationAsync(AuthorizationHandlerContext context,
            OrderOperationAuthorizationRequirement requirement, OrderCreationResource resource);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Authorization;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Products.Products;
using EasyAbp.EShop.Products.Products.Dtos;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Authorization.Permissions;

namespace EasyAbp.EShop.Orders.Orders
{
    public class BasicOrderCreationAuthorizationHandler : OrderCreationAuthorizationHandler
    {
        private readonly IPermissionChecker _permissionChecker;

        public BasicOrderCreationAuthorizationHandler(IPermissionChecker permissionChecker)
        {
            _permissionChecker = permissionChecker;
        }
        
        protected override async Task HandleOrderCreationAsync(AuthorizationHandlerContext context,
            OrderOperationAuthorizationRequirement requirement, OrderCreationResource resource)
        {
            if (!await _permissionChecker.IsGrantedAsync(OrdersPermissions.Orders.Create))
            {
                context.Fail();
                return;
            }
            
            if (!await IsProductsPublishedAsync(resource.Input, resource.ProductDictionary))
            {
                context.Fail();
                return;
            }

            if (!await IsInventoriesSufficientAsync(resource.Input, resource.ProductDictionary))
            {
                context.Fail();
                return;
            }
            
            context.Succeed(requirement);
        }

        protected virtual Task<bool> IsProductsPublishedAsync(CreateOrderDto input,
            Dictionary<Guid, ProductDto> productDictionary)
        {
            return Task.FromResult(
                input.OrderLines.Select(dto => dto.ProductId).Distinct().ToArray()
                    .All(productId => productDictionary[productId].IsPublished)
            );
        }

        protected virtual Task<bool> IsInventoriesSufficientAsync(CreateOrderDto input,
            Dictionary<Guid, ProductDto> productDictionary)
        {
            return Task.FromResult(
                !(from orderLine in input.OrderLines
                    let product = productDictionary[orderLine.ProductId]
                    let inventory = product.ProductSkus.Single(sku => sku.Id == orderLine.ProductSkuId).Inventory
                    where product.InventoryStrategy != InventoryStrategy.NoNeed && inventory < orderLine.Quantity
                    select orderLine).Any()
            );
        }
    }
}

 

7,abp sorting

  • sorting 可以是一个字符串, 如 NameName ASC 或 Name DESC. 通过使用 System.Linq.Dynamic.Core NuGet 包是可能的.

 

posted on 2021-01-26 11:14  *Hunter  阅读(268)  评论(0编辑  收藏  举报

导航

AmazingCounters.com