首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从命令中运行查询是否违反命令-查询分离?

问从命令中运行查询是否违反命令-查询分离?
EN

Stack Overflow用户
提问于 2016-04-12 16:25:14
回答 2查看 935关注 0票数 6

给定一个真实的匿名购物车,"AddToCart“工作流必须执行以下步骤:

  1. 从数据库中查找当前产品。从产品中获取价格或使用服务计算用户选择和其他产品属性的价格。(查询)
  2. 从数据库中查找当前购物车。(查询)
  3. 如果数据库中不存在当前购物车,则创建一个新的购物车实体(内存中)。
  4. 将新项目(产品)添加到购物车实体(内存中)以及它的价格。
  5. 对整个购物车进行任何折扣计算。(取决于查询)
  6. 对购物车进行任何销售税计算。(取决于查询)
  7. 在购物车上进行任何运输计算。(取决于查询)
  8. 如果这是一个新的购物车,将实体添加到数据库中,否则更新数据库中的购物车。(指挥)

因此,尽管"AddToCart“听起来应该是一个命令(因为它更新了系统状态),但实际上它依赖于许多查询。

我的问题

像这样处理工作流的普遍接受的方法是什么?

  1. 创建一个依赖于可能运行查询的其他服务的AddToCartCommandHandler。
  2. 创建一个facade CartService,它编排工作流,该工作流首先运行查询,然后是命令。
  3. 使控制器操作方法首先运行查询,然后运行任何命令。看起来,如果需要重用这些查询步骤,可能会遗漏一些步骤。
  4. 另一个?

我之所以找不到这方面的答案,是因为它“取决于设计”,而这是不应用它的例外之一吗?

如果命令和查询是分开的,我会把我真正的实体框架实体类传递给添加/更新购物车的命令吗?在这种情况下,DTO似乎是做不到的。

注意:--我隐含地假设实现CQS的系统是这样做的,目的是最终成为一个完整的CQRS系统。如果是这样的话,这个工作流显然无法完成转换--因此我提出了一个问题。

背景

我第一次尝试CQS。

从我所读到的关于这种模式的文档中可以清楚地看到,查询不能更改系统状态。

但是,还不清楚是否可以在命令中运行查询(我似乎在任何地方都找不到任何信息)。

有几个现实世界的案例,我可以想到,在哪里,这需要发生。但是,鉴于在网上缺乏这种模式的真实例子,我不知道该如何继续下去。网上有很多理论,但我唯一能找到的代码是here和here。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-28 10:48:20

这个问题的答案是由qujck以a comment的形式提出的。

解决方案是将应用程序分解为不同的查询类型和命令类型。每一种类型的确切用途仍然是一个谜(因为博客文章没有说明他为什么做出这种区分),但它确实明确了顶级和中级命令如何依赖于数据库查询。

命令类型

  1. 命令(顶层)
  2. 指挥策略(中层)
  3. 数据命令(直接数据访问)

查询类型

  1. 查询(顶层)
  2. 查询策略(中级)
  3. 数据查询(直接数据访问)

命令查询实现

代码语言:javascript
复制
// Commands

public interface ICommand
{
}

public interface IDataCommand
{
}

/// <summary>
/// A holistic abstraction, an abstraction that acts as the whole of each transaction 
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

public interface ICommandStrategyHandler<TCommand> where TCommand : ICommand
{
    void Handle(TCommand command);
}

/// <summary>
/// Direct database update
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public interface IDataCommandHandler<TCommand> where TCommand : IDataCommand
{
    void Handle(TCommand command);
}



// Queries

public interface IQuery<TResult>
{
}

public interface IDataQuery<TResult>
{
}

/// <summary>
/// A holistic abstraction, an abstraction that acts as the whole of each transaction 
/// </summary>
/// <typeparam name="TQuery"></typeparam>
/// <typeparam name="TResult"></typeparam>
public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
    TResult Handle(TQuery query);
}

public interface IQueryStrategyHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
    TResult Handle(TQuery query);
}

/// <summary>
/// Direct database query
/// </summary>
/// <typeparam name="TQuery"></typeparam>
/// <typeparam name="TResult"></typeparam>
public interface IDataQueryHandler<TQuery, TResult> where TQuery : IDataQuery<TResult>
{
    TResult Handle(TQuery query);
}


/// <summary>
/// Generic processor that can run any query
/// </summary>
public interface IQueryProcessor
{
    TResult Execute<TResult>(IQuery<TResult> query);

    // NOTE: Stephen recommends against using Async. He may be right that it is not
    // worth the aggrevation of bugs that may be introduced.
    //Task<TResult> Execute<TResult>(IQuery<TResult> query);

    TResult Execute<TResult>(IDataQuery<TResult> query);
}

AddToCart依赖图

使用上面的实现,AddToCart工作流依赖图的结构如下所示。

  • AddToCartCommandHandler : ICommandHandler<AddToCartCommand>
    • GetShoppingCartDetailsQueryHandler : IQueryHandler<GetShoppingCartDetailsQuery, ShoppingCartDetails>
      • GetShoppingCartQueryStrategyHandler : IQueryStrategyHandler<GetShoppingCartQueryStrategy, ShoppingCartDetails>
        • GetShoppingCartDataQueryHandler : IDataQueryHandler<GetShoppingCartDataQuery, ShoppingCartDetails>
          • ApplicationDbContext

​

代码语言:javascript
复制
        - `CreateShoppingCartDataCommandHandler : IDataCommandHandler<CreateShoppingCartDataCommand>` 
            - `ApplicationDbContext`

​

​

​

代码语言:javascript
复制
- `UpdateShoppingCartDataCommandHandler : IDataCommandHandler<UpdateShoppingCartDataCommand>`
- `SetItemPriceCommandStrategyHandler : ICommandStrategyHandler<SetItemPriceCommandStrategy>` 
    - `GetProductDetailsDataQueryHandler : IDataQueryHandler<GetProductDetailsDataQuery, ProductDetails>` 
        - `ApplicationDbContext`

​

​

代码语言:javascript
复制
- `SetTotalsCommandStrategyHandler : ICommandStrategyHandler<SetTotalsCommandStrategy>` 
    - `SetDiscountsCommandStrategyHandler : ICommandStrategyHandler<SetDiscountsCommandStrategy>` 
        - `?`

​

代码语言:javascript
复制
    - `SetSalesTaxCommandStrategyHandler : ICommandStrategyHandler<SetSalesTaxCommandStrategy>`

​

​

实现

DTO

代码语言:javascript
复制
public class ShoppingCartDetails : IOrder
{
    private IEnumerable<IOrderItem> items = new List<ShoppingCartItem>();

    public Guid Id { get; set; }
    public decimal SubtotalDiscounts { get; set; }
    public string ShippingPostalCode { get; set; }
    public decimal Shipping { get; set; }
    public decimal ShippingDiscounts { get; set; }
    public decimal SalesTax { get; set; }
    public decimal SalesTaxDiscounts { get; set; }

    // Declared twice - once for the IOrder interface
    // and once so we can get the realized concrete type.
    // See: https://stackoverflow.com/questions/15490633/why-cant-i-use-a-compatible-concrete-type-when-implementing-an-interface
    public IEnumerable<ShoppingCartItem> Items
    {
        get { return this.items as IEnumerable<ShoppingCartItem>; }
        set { this.items = value; }
    }
    IEnumerable<IOrderItem> IOrder.Items
    {
        get { return this.items; }
        set { this.items = value; }
    }

    //public IEnumerable<ShoppingCartNotification> Notifications { get; set; }
    //public IEnumerable<ShoppingCartCoupon> Coupons { get; set; } // TODO: Add this to IOrder
}

public class ShoppingCartItem : IOrderItem
{
    public ShoppingCartItem()
    {
        this.Id = Guid.NewGuid();
        this.Selections = new Dictionary<string, object>();
    }

    public Guid Id { get; set; }
    public Guid ShoppingCartId { get; set; }
    public Guid ProductId { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal PriceDiscount { get; set; }
    public IDictionary<string, object> Selections { get; set; }
}

public class ProductDetails 
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public decimal Discount { get; set; }
}

计算订单总数

我没有依赖于一串服务来执行简单的(和必需的)算法,而是选择将这种行为放入扩展方法中,这样就可以根据实际数据动态执行。由于这个逻辑需要在购物车、订单和报价之间共享,所以计算是针对IOrder和IOrderItem完成的,而不是具体的模型类型。

代码语言:javascript
复制
// Contract to share simple cacluation and other business logic between shopping cart, order, and quote
public interface IOrder
{
    decimal SubtotalDiscounts { get; set; }
    decimal Shipping { get; set; }
    decimal ShippingDiscounts { get; set; }
    decimal SalesTax { get; set; }
    decimal SalesTaxDiscounts { get; set; }
    IEnumerable<IOrderItem> Items { get; set; }
}

public interface IOrderItem
{
    Guid ProductId { get; set; }
    int Quantity { get; set; }
    decimal Price { get; set; }
    decimal PriceDiscount { get; set; }
    IDictionary<string, object> Selections { get; set; }
}

public static class OrderExtensions
{
    public static decimal GetSubtotal(this IOrder order)
    {
        return order.Items.Sum(x => x.GetTotal());
    }

    public static decimal GetSubtotalBeforeDiscounts(this IOrder order)
    {
        return order.Items.Sum(x => x.GetTotalBeforeDiscounts());
    }

    public static decimal GetTotal(this IOrder order)
    {
        var subtotal = (order.GetSubtotal() - order.SubtotalDiscounts);
        var shipping = (order.Shipping - order.ShippingDiscounts);
        var salesTax = (order.SalesTax - order.SalesTaxDiscounts);
        return (subtotal + shipping + salesTax);
    }
}

public static class OrderItemExtensions
{
    public static decimal GetTotalBeforeDiscounts(this IOrderItem item)
    {
        return (item.Price * item.Quantity);
    }

    public static decimal GetTotal(this IOrderItem item)
    {
        return (GetTotalBeforeDiscounts(item) - item.PriceDiscount);
    }

    public static decimal GetDiscountedUnitPrice(this IOrderItem item)
    {
        return (item.Quantity > 0) ? (GetTotal(item) / item.Quantity) : 0;
    }
}

ShoppingCartController

为了简洁起见,我们只显示AddToCart操作,但这也是针对购物车的其他操作(即从购物车中移除)的位置。

代码语言:javascript
复制
public class ShoppingCartController : Controller
{
    private readonly IQueryProcessor queryProcessor;
    private readonly IAnonymousIdAccessor anonymousIdAccessor;
    private readonly ICommandHandler<AddToCartCommand> addToCartHandler;

    public ShoppingCartController(
        IQueryProcessor queryProcessor, 
        IAnonymousIdAccessor anonymousIdAccessor, 
        ICommandHandler<AddToCartCommand> addToCartHandler)
    {
        if (queryProcessor == null)
            throw new ArgumentNullException("queryProcessor");
        if (anonymousIdAccessor == null)
            throw new ArgumentNullException("anonymousIdAccessor");
        if (addToCartHandler == null)
            throw new ArgumentNullException("addToCartHandler");

        this.queryProcessor = queryProcessor;
        this.anonymousIdAccessor = anonymousIdAccessor;
        this.addToCartHandler = addToCartHandler;
    }

    public ActionResult Index()
    {
        var command = new GetShoppingCartDetailsQuery
        {
            ShoppingCartId = this.anonymousIdAccessor.AnonymousID
        };

        ShoppingCartDetails cart = this.queryProcessor.Execute(command);

        return View(cart);
    }

    public ActionResult AddToCart(ItemViewModel model)
    {
        var command = new AddToCartCommand
        {
            ProductId = model.Id,
            Quantity = model.Qty,
            Selections = model.Selections,
            ShoppingCartId = this.anonymousIdAccessor.AnonymousID
        };

        this.addToCartHandler.Handle(command);

        // If we execute server side, it should go to the cart page
        return RedirectToAction("Index");
    }
}

AddToCartCommandHandler

这里是执行工作流的主要部分。这个命令将直接从AddToCart控制器操作中调用。

代码语言:javascript
复制
public class AddToCartCommandHandler : ICommandHandler<AddToCartCommand>
{
    private readonly IQueryStrategyHandler<GetShoppingCartQueryStrategy, ShoppingCartDetails> getShoppingCartQuery;
    private readonly IDataCommandHandler<UpdateShoppingCartDataCommand> updateShoppingCartCommand;
    private readonly ICommandStrategyHandler<SetItemPriceCommandStrategy> setItemPriceCommand;
    private readonly ICommandStrategyHandler<SetTotalsCommandStrategy> setTotalsCommand;

    public AddToCartCommandHandler(
        IQueryStrategyHandler<GetShoppingCartQueryStrategy, ShoppingCartDetails> getShoppingCartCommand,
        IDataCommandHandler<UpdateShoppingCartDataCommand> updateShoppingCartCommand,
        ICommandStrategyHandler<SetItemPriceCommandStrategy> setItemPriceCommand,
        ICommandStrategyHandler<SetTotalsCommandStrategy> setTotalsCommand
        )
    {
        if (getShoppingCartCommand == null)
            throw new ArgumentNullException("getShoppingCartCommand");
        if (setItemPriceCommand == null)
            throw new ArgumentNullException("setItemPriceCommand");
        if (updateShoppingCartCommand == null)
            throw new ArgumentNullException("updateShoppingCartCommand");
        if (setTotalsCommand == null)
            throw new ArgumentNullException("setTotalsCommand");

        this.getShoppingCartQuery = getShoppingCartCommand;
        this.updateShoppingCartCommand = updateShoppingCartCommand;
        this.setItemPriceCommand = setItemPriceCommand;
        this.setTotalsCommand = setTotalsCommand;
    }

    public void Handle(AddToCartCommand command)
    {
        // Get the shopping cart (aggregate root) from the database
        var shoppingCart = getShoppingCartQuery.Handle(new GetShoppingCartQueryStrategy { ShoppingCartId = command.ShoppingCartId });

        // Create a new shopping cart item
        var item = new Contract.DTOs.ShoppingCartItem
        {
            ShoppingCartId = command.ShoppingCartId,
            ProductId = command.ProductId,
            Quantity = command.Quantity,

            // Dictionary representing the option selections the user made on the UI
            Selections = command.Selections
        };

        // Set the item's price (calculated/retrieved from database query)
        setItemPriceCommand.Handle(new SetItemPriceCommandStrategy { ShoppingCartItem = item });

        // Add the item to the cart
        var items = new List<Contract.DTOs.ShoppingCartItem>(shoppingCart.Items);
        items.Add(item);
        shoppingCart.Items = items;

        // Set the shopping cart totals (sales tax, discounts)
        setTotalsCommand.Handle(new SetTotalsCommandStrategy { ShoppingCart = shoppingCart });

        // Update the shopping cart details in the database
        updateShoppingCartCommand.Handle(new UpdateShoppingCartDataCommand { ShoppingCart = shoppingCart });
    }
}

GetShoppingCartQueryStrategyHandler

代码语言:javascript
复制
public class GetShoppingCartQueryStrategyHandler : IQueryStrategyHandler<GetShoppingCartQueryStrategy, ShoppingCartDetails>
{
    private readonly IDataQueryHandler<GetShoppingCartDataQuery, ShoppingCartDetails> getShoppingCartDataQuery;
    private readonly IDataCommandHandler<CreateShoppingCartDataCommand> createShoppingCartDataCommand;

    public GetShoppingCartQueryStrategyHandler(
        IDataQueryHandler<GetShoppingCartDataQuery, ShoppingCartDetails> getShoppingCartDataQuery,
        IDataCommandHandler<CreateShoppingCartDataCommand> createShoppingCartDataCommand)
    {
        if (getShoppingCartDataQuery == null)
            throw new ArgumentNullException("getShoppingCartDataQuery");
        if (createShoppingCartDataCommand == null)
            throw new ArgumentNullException("createShoppingCartDataCommand");

        this.getShoppingCartDataQuery = getShoppingCartDataQuery;
        this.createShoppingCartDataCommand = createShoppingCartDataCommand;
    }

    public ShoppingCartDetails Handle(GetShoppingCartQueryStrategy query)
    {
        var result = this.getShoppingCartDataQuery.Handle(new GetShoppingCartDataQuery { ShoppingCartId = query.ShoppingCartId });

        // If there is no shopping cart, create one.
        if (result == null)
        {
            this.createShoppingCartDataCommand.Handle(new CreateShoppingCartDataCommand { ShoppingCartId = query.ShoppingCartId });

            result = new ShoppingCartDetails
            {
                Id = query.ShoppingCartId
            };
        }

        return result;
    }
}

GetShoppingCartDataQueryHandler

代码语言:javascript
复制
/// <summary>
/// Data handler to get the shopping cart data (if it exists)
/// </summary>
public class GetShoppingCartDataQueryHandler : IDataQueryHandler<GetShoppingCartDataQuery, ShoppingCartDetails>
{
    private readonly IAppContext context;

    public GetShoppingCartDataQueryHandler(IAppContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        this.context = context;
    }

    public ShoppingCartDetails Handle(GetShoppingCartDataQuery query)
    {
        return (from shoppingCart in context.ShoppingCarts
                where shoppingCart.Id == query.ShoppingCartId
                select new ShoppingCartDetails
                {
                    Id = shoppingCart.Id,
                    SubtotalDiscounts = shoppingCart.SubtotalDiscounts,
                    ShippingPostalCode = shoppingCart.ShippingPostalCode,
                    Shipping = shoppingCart.Shipping,
                    ShippingDiscounts = shoppingCart.ShippingDiscounts,
                    SalesTax = shoppingCart.SalesTax,
                    SalesTaxDiscounts = shoppingCart.SalesTaxDiscounts,

                    Items = shoppingCart.Items.Select(i =>
                        new Contract.DTOs.ShoppingCartItem
                        {
                            Id = i.Id,
                            ShoppingCartId = i.ShoppingCartId,
                            ProductId = i.ProductId,
                            Quantity = i.Quantity,
                            Price = i.Price,
                            PriceDiscount = i.PriceDiscount
                            // TODO: Selections...
                        })
                }).FirstOrDefault();
    }
}

CreateShoppingCartDataCommandHandler

代码语言:javascript
复制
public class CreateShoppingCartDataCommandHandler : IDataCommandHandler<CreateShoppingCartDataCommand>
{
    private readonly IAppContext context;

    public CreateShoppingCartDataCommandHandler(IAppContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        this.context = context;
    }

    public void Handle(CreateShoppingCartDataCommand command)
    {
        var cart = new ShoppingCart
        {
            Id = command.ShoppingCartId
        };

        this.context.ShoppingCarts.Add(cart);
        this.context.SaveChanges();
    }
}

UpdateShoppingCartDataCommandHandler

这将使用业务层应用的所有更改更新购物车。

目前,这个“命令”执行一个查询,以便协调数据库和内存副本之间的差异。然而,这显然违反了CQS模式。我计划做一个后续问题,以确定什么是最好的行动路线是变化跟踪,因为变化跟踪和CQS似乎是密切相关的。

代码语言:javascript
复制
public class UpdateShoppingCartDataCommandHandler : IDataCommandHandler<UpdateShoppingCartDataCommand>
{
    private readonly IAppContext context;

    public UpdateShoppingCartDataCommandHandler(IAppContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        this.context = context;
    }

    public void Handle(UpdateShoppingCartDataCommand command)
    {
        var cart = context.ShoppingCarts
            .Include(x => x.Items)
            .Single(x => x.Id == command.ShoppingCart.Id);


        cart.Id = command.ShoppingCart.Id;
        cart.SubtotalDiscounts = command.ShoppingCart.SubtotalDiscounts;
        cart.ShippingPostalCode = command.ShoppingCart.ShippingPostalCode;
        cart.Shipping = command.ShoppingCart.Shipping;
        cart.ShippingDiscounts = command.ShoppingCart.ShippingDiscounts;
        cart.SalesTax = command.ShoppingCart.SalesTax;
        cart.SalesTaxDiscounts = command.ShoppingCart.SalesTaxDiscounts;

        ReconcileShoppingCartItems(cart.Items, command.ShoppingCart.Items, command.ShoppingCart.Id);

        // Update the cart with new data

        context.SaveChanges();
    }

    private void ReconcileShoppingCartItems(ICollection<ShoppingCartItem> items, IEnumerable<Contract.DTOs.ShoppingCartItem> itemDtos, Guid shoppingCartId)
    {
        // remove deleted items
        var items2 = new List<ShoppingCartItem>(items);
        foreach (var item in items2)
        {
            if (!itemDtos.Any(x => x.Id == item.Id))
            {
                context.Entry(item).State = EntityState.Deleted;
            }
        }


        // Add/update items
        foreach (var dto in itemDtos)
        {
            var item = items.FirstOrDefault(x => x.Id == dto.Id);
            if (item == null)
            {
                items.Add(new ShoppingCartItem
                {
                    Id = Guid.NewGuid(),
                    ShoppingCartId = shoppingCartId,
                    ProductId = dto.ProductId,
                    Quantity = dto.Quantity,
                    Price = dto.Price,
                    PriceDiscount = dto.PriceDiscount
                });
            }
            else
            {
                item.ProductId = dto.ProductId;
                item.Quantity = dto.Quantity;
                item.Price = dto.Price;
                item.PriceDiscount = dto.PriceDiscount;
            }
        }
    }
}

SetItemPriceCommandStrategyHandler

代码语言:javascript
复制
public class SetItemPriceCommandStrategyHandler : ICommandStrategyHandler<SetItemPriceCommandStrategy>
{
    private readonly IDataQueryHandler<GetProductDetailsDataQuery, ProductDetails> getProductDetailsQuery;

    public SetItemPriceCommandStrategyHandler(
        IDataQueryHandler<GetProductDetailsDataQuery, ProductDetails> getProductDetailsQuery)
    {
        if (getProductDetailsQuery == null)
            throw new ArgumentNullException("getProductDetailsQuery");

        this.getProductDetailsQuery = getProductDetailsQuery;
    }

    public void Handle(SetItemPriceCommandStrategy command)
    {
        var shoppingCartItem = command.ShoppingCartItem;

        var product = getProductDetailsQuery.Handle(new GetProductDetailsDataQuery { ProductId = shoppingCartItem.ProductId });

        // TODO: For products with custom calculations, need to use selections on shopping cart item
        // as well as custom formula and pricing points from product to calculate the item price.

        shoppingCartItem.Price = product.Price;
    }
}

GetProductDetailsDataQueryHandler

代码语言:javascript
复制
public class GetProductDetailsDataQueryHandler : IDataQueryHandler<GetProductDetailsDataQuery, ProductDetails>
{
    private readonly IAppContext context;

    public GetProductDetailsDataQueryHandler(IAppContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        this.context = context;
    }

    public ProductDetails Handle(GetProductDetailsDataQuery query)
    {
        return (from product in context.Products
                where product.Id == query.ProductId
                select new ProductDetails
                {
                    Id = product.Id,
                    Name = product.Name,
                    Price = product.Price
                }).FirstOrDefault(); 
    }
}

SetTotalsCommandStrategyHandler

代码语言:javascript
复制
public class SetTotalsCommandStrategyHandler : ICommandStrategyHandler<SetTotalsCommandStrategy>
{
    private readonly ICommandStrategyHandler<SetDiscountsCommandStrategy> setDiscountsCommand;
    private readonly ICommandStrategyHandler<SetSalesTaxCommandStrategy> setSalesTaxCommand;

    public SetTotalsCommandStrategyHandler(
        ICommandStrategyHandler<SetDiscountsCommandStrategy> setDiscountsCommand,
        ICommandStrategyHandler<SetSalesTaxCommandStrategy> setSalesTaxCommand
        )
    {
        if (setDiscountsCommand == null)
            throw new ArgumentNullException("setDiscountsCommand");
        if (setSalesTaxCommand == null)
            throw new ArgumentNullException("setSalesTaxCommand");

        this.setDiscountsCommand = setDiscountsCommand;
        this.setSalesTaxCommand = setSalesTaxCommand;
    }

    public void Handle(SetTotalsCommandStrategy command)
    {
        var shoppingCart = command.ShoppingCart;

        // Important: Discounts must be calculated before sales tax to ensure the discount is applied
        // to the subtotal before tax is calculated.
        setDiscountsCommand.Handle(new SetDiscountsCommandStrategy { ShoppingCart = shoppingCart });
        setSalesTaxCommand.Handle(new SetSalesTaxCommandStrategy { ShoppingCart = shoppingCart });
    }
}

SetDiscountsCommandStrategyHandler

代码语言:javascript
复制
public class SetDiscountsCommandStrategyHandler : ICommandStrategyHandler<SetDiscountsCommandStrategy>
{
    public void Handle(SetDiscountsCommandStrategy command)
    {
        var shoppingCart = command.ShoppingCart;

        // TODO: Set discounts according to business rules

        foreach (var item in shoppingCart.Items)
        {
            item.PriceDiscount = 0;
        }

        shoppingCart.SubtotalDiscounts = 0;
        shoppingCart.SalesTaxDiscounts = 0;
        shoppingCart.ShippingDiscounts = 0;
    }
}

SetSalesTaxCommandStrategyHandler

代码语言:javascript
复制
public class SetSalesTaxCommandStrategyHandler : ICommandStrategyHandler<SetSalesTaxCommandStrategy>
{
    public void Handle(SetSalesTaxCommandStrategy command)
    {
        var shoppingCart = command.ShoppingCart;
        var postalCode = command.ShoppingCart.ShippingPostalCode;

        bool isInCalifornia = !string.IsNullOrEmpty(postalCode) ?
            // Matches 90000 to 96200
            Regex.IsMatch(postalCode, @"^9(?:[0-5]\d{3}|6[0-1]\d{2}|6200)(?:-?(?:\d{4}))?$") :
            false;

        if (isInCalifornia)
        {
            var subtotal = shoppingCart.GetSubtotal();

            // Rule for California - charge a flat 7.75% if the zip code is in California
            var salesTax = subtotal * 0.0775M;

            shoppingCart.SalesTax = salesTax;
        }
    }
}

请注意,此工作流中没有托运计算。这主要是因为传送计算可能依赖于外部API,返回可能需要一段时间。因此,我计划使AddToCart工作流在添加项时立即运行,并使CalculateShippingAndTax工作流在从其(可能是外部)源检索完总计后再次更新UI之后发生,这可能需要时间。

这能解决问题吗?是的,它确实解决了我在命令需要依赖查询时遇到的现实问题。

然而,这实际上只是在概念上将查询与命令区分开来。在物理上,它们仍然相互依赖,除非您只查看只依赖于IDataCommand和IDataQuery的抽象。我不确定这是否是qujck的意图。我也不确定这是否解决了更大的问题,即设计是否可以转移到CQRS,但由于这不是我的计划,我没有那么关心它。

票数 4
EN

Stack Overflow用户

发布于 2016-04-28 02:59:02

在相互冲突的设计原则之间总是要考虑权衡。解决这一问题的办法是研究这些原则背后的根本原因。在这种情况下,在不运行命令的情况下无法运行查询是有问题的,但是在不运行查询的情况下无法运行命令通常是无害的。只要有一种独立运行查询的方法,我就没有理由不将查询结果添加到命令中,特别是如果这样做的话:

代码语言:javascript
复制
QueryResult command()
{
   // do command stuff
   return query();
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36578997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档