个人未完成的网站

abp学习日志五(领域服务)

应用 Application

这一层更多的是逻辑运算,把Dto转化为实体,聚合根等。

Dto是一个非常不错的分层,关于Dto,Vo,Do,Po的详解在第一篇已经介绍abp学习日记 初记

ProductService

using LY.Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LY.Shop
{
    public class ProductService : CrudAppService<Product, ProductDto, Guid, PageProductDto, CreateProductDto, UpdateProductDto>, IProductService
    {
        private readonly IRepository<Product, Guid> _productRepository;
        public ProductService(IRepository<Product, Guid> productRepository) : base(productRepository)
        {
            _productRepository = productRepository;
        }

        public Task<ProductDto> AddAsync(ProductDto product)
        {
            var result = _productRepository.InsertAsync(new Product(GuidGenerator.Create()) { ProductPrice = 1, ProductUnit = "个", ProductName = product.Name }).ContinueWith(task =>
                         {
                             return product;
                         });
            return result;
        }

        public Task<ProductDto> GetAsync()
        {
            var result = _productRepository.FirstOrDefault();
            return Task.FromResult(new ProductDto()
            {
                Name = result.ProductName
            });
        }

        public Task<ProductDto> GetAuthorizedAsync()
        {
            return Task.FromResult(
                new ProductDto
                {
                    Name = "aaa"
                }
            );
        }
    }
}

ICrudAppService 接口

public interface ICrudAppService<
    TEntityDto,
    in TKey,
    in TGetListInput,
    in TCreateInput,
    in TUpdateInput>
    : IApplicationService
    where TEntityDto : IEntityDto<TKey>
{
    Task<TEntityDto> GetAsync(TKey id);

    Task<PagedResultDto<TEntityDto>> GetListAsync(TGetListInput input);

    Task<TEntityDto> CreateAsync(TCreateInput input);

    Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input);

    Task DeleteAsync(TKey id);
}

对常用的CURD操作做了基本约束

为了方便使用,框架还给做了实现 CrudAppService

遇到了麻烦

在使用过程中遇到了麻烦,不是技术问题,是工作量的问题,根据接口的泛型和泛型约束,要创建多个Dto,确实麻烦的要死,当然可以只用一个Dto。但是这个工作量也很大,如果网上没有工具,最好自己开发一个自动生成代码的工具。

posted on 2020-03-15 23:34  我是小虫  阅读(210)  评论(0编辑  收藏  举报

导航