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。但是這個工作量也很大,如果網上沒有工具,最好自己開發一個自動生成代碼的工具。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章