模式以评估对象组合

模式以评估对象组合

本文介绍了如何调整“规范”模式以评估对象组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道规范模式描述了如何使用实现 ISpecification< T> 的类的层次结构来评估类型T的候选对象是否与某个规范匹配(=

I know that the Specification pattern describes how to use a hierarchy of classes implementing ISpecification<T> to evaluate if a candidate object of type T matches a certain specification (= satisfies a business rule).

我的问题:我要实现的业务规则需要评估多个对象(例如,客户和合同)。

My problem : the business rule I want to implement needs to evaluate several objects (for example, a Customer and a Contract).

我的双重问题:


  • 规范模式是否具有典型的适应性实现这一目标?我只能考虑删除规范类中的 ISpecification< T> 的实现,并在 isSatisfiedBy()中获取所需的尽可能多的参数。 方法。但是这样做会导致我无法将此规范与其他规范组合。

  • Are there typical adaptations of the Specification patterns to achieve this ? I can only think of removing the implementation of ISpecification<T> by my specification class, and taking as many parameters as I want in the isSatisfiedBy() method. But by doing this, I lose the ability to combine this specification with others.

这个问题是否揭示了我的设计中的缺陷? (即我需要使用客户和合同评估的内容应该在另一个对象(例如订阅)上评估,其中可能包含所有必要的信息)? b $ b

Does this problem reveal a flaw in my design ? (i.e. what I need to evaluate using a Customer and a Contract should be evaluated on another object, like a Subscription, which could contain all the necessary info) ?

推荐答案

在这种情况下(根据规范的确切要求,我将其中一个对象用作规范主题,而将另一个用作规范主题

In that case (depending on what the specification precisely should do, I would use one of the objects as specification subject and the other(s) as parameter.

示例:

public class ShouldCreateEmailAccountSpecification : ISpecification<Customer>
{
    public ShouldCreateEmailAccountSpecification(Contract selectedContract)
    {
       SelectedContract = selectedContract;
    }

    public Contract SelectedContract { get; private set; }

    public bool IsSatisfiedBy(Customer subject)
    {
        return false;
    }
}

这篇关于如何调整“规范”模式以评估对象组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:05