【问题标题】:Best advice to Implement REST API for entities with Table Per Hierarchy model design (MVCCore + EF 2.2)使用 Table Per Hierarchy 模型设计 (MVCCore + EF 2.2) 为实体实现 REST API 的最佳建议
【发布时间】:2019-02-07 14:18:33
【问题描述】:

我有几个派生类Shippable 和Downloadable 继承自同一个基类Product,它们每个都有自己的属性:

    public abstract class Product : Entity<Guid>
    {
        public ProductType ProductType{ get; set; }

        public string Name { get; set; }
    }

    public class Downloadable : Product
    {
        public Guid DownloadId { get; set; }
    }

    public class Shippable : Product
    {
        public Guid ShippingInfo { get; set; }
    }

我为我的 DataTable 使用了 EF Core TBH 设计,因此该实体的所有属性都存储在一个表中:

            builder.Entity<Product>(
                b =>
                {
                    b.ToTable(
                        options.TablePrefix + "Products",
                        options.Schema
                    );

                    b.HasKey(
                        x => x.Id
                    );

                    b.HasDiscriminator<ProductType>(
                         nameof(Product.ProductType)
                     )
                     .HasValue<Downloadable>(
                         ProductType.Downloadable
                     )
                     .HasValue<Shippable>(
                         ProductType.Shippable
                     );
                }

我的项目是 NLayered DDD,所以我有一个应用程序层,它为 UI/REST API 方法生成业务逻辑并包含应用程序服务和 DTO。

我的问题是应用这个的最佳实践是什么。您对此选项的看法:

1- 每个具有派生 DTO 的派生类的业务服务

2- 只有一项服务可以通过共享 DTO 为所有服务提供服务(包含派生类的可为空属性

3- 你的建议

提前致谢。

【问题讨论】:

    标签: c# asp.net-core asp.net-core-webapi dto table-per-hierarchy


    【解决方案1】:

    我不完全确定您在寻找什么,但从 API 视图来看,我会使用一个抽象产品控制器,然后从中为每个特定产品类型派生一个控制器:

    [Route("api/[controller]")]
    [ApiController]
    public abstract class ProductController<TProduct, TProductDTO> : ControllerBase
        where TProduct : Product, new()
        where TProductDTO : class, new()
    {
    }
    
    public class DownloadableController : ProductController<Downloadable, DownloadableDTO>
    {
    }
    
    public class ShippableController : ProductController<Shippable, ShippableDTO>
    {
    }
    

    然后,您在抽象产品控制器上构建所有端点,使用通用类型 TProduct 和 TProductDTO。例如,您可能有以下内容:

    [HttpPost("")]
    public async Task<ActionResult<TProductResource>> Create(TProductDTO dto)
    {
        var product = _mapper.Map<TProduct>(dto);
        _context.Add(product);
        await _context.SaveChangesAsync();
        return CreatedAt("Get", new { product.Id }, _mapper.Map<TProductDTO>(product));
    }
    

    由于是泛型类型,此方法适用于所有派生控制器,无需重新定义甚至覆盖,尽管您可以通过添加 virtual 关键字来允许它被覆盖。不过,通常最好添加占位符方法。例如,您可以执行以下操作:

    private virtual Task BeforeCreateAsync(TProduct product, TProductDTO dto) =>
    

    Task.CompletedTask;

    private virtual Task AfterCreateAsync(TProduct product, TProductDTO dto) =>
    

    Task.CompletedTask;

    然后包装你的SaveChangesAsync 电话:

    _context.Add(product);
    await BeforeCreateAsync(product, dto);
    await _context.SaveChangesAsync();
    await AfterCreateAsync(product, dto);
    

    然后,在您的派生控制器中,您可以覆盖这些以存根附加功能:

    private override async Task BeforeCreateAsync(Downloadable product, DownloadableDTO dto)
    {
        // do something specific to downloadable products
    }
    

    这样,您的大部分逻辑仍然是独立的。您可以添加对您的应用程序有意义的尽可能多的此类占位符方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多