Domanda

I recently finished Steve Smith and Julie Lerman's excellent Pluralsight course on Domain Driven Design, and I'm applying the training to a project. I have what I think is a good candidate for a value object: MeasurementSpecification. In the problem domain, a measurement specification always has a nominal value, a minimum tolerance, and a maximum tolerance. A model may look something like this:

public class MeasurementSpecification
{
    public Decimal NominalValue { get; private set; }
    public Decimal UpperToleranceLimit { get; private set; }
    public Decimal LowerToleranceLimit { get; private set; }

    ...setters and behaviors...        
}

And entities as follows:

public class WidgetDescription // may be a candidate as another value object, but that's probably another discussion.
{
    public int Id {get; private set;}
    public String Description {get; private set;}
    public MeasurementSpecification Specification {get; private set;}
    ...setters and behaviors... 
}

public class Widget
{
    public int Id {get; private set;}
    public WidgetDescription WidgetDescription {get; private set;}
    public decimal MeasurementValue {get; private set;}
    ...setters and behaviors... 
}

I'm struggling with which class should have the responsibility of validating that a measurement value is within spec. My first inclination is to assign the behavior to the MeasurementSpecification since other entities might also have the MeasurementSpecification value object as one of its properties. An updated model might look like:

public class MeasurementSpecification
{
    ...

    public Boolean IsWithinSpec(Decimal measurementValue)
    {
        return (measurementValue >= LowerToleranceLimit && measurementValue <= UpperToleranceLimit) ? true : false;
    }
}

But I could also see why the Widget entity might have its own behavior of validating its own measurement value against a specification:

public class Widget 
{
    ...
    public Boolean IsWithinSpec() 
    {
        return this.WidgetDescription.MeasurementSpecification.IsWithinSpec(this.MeasurementValue)
    }
}

Is the approach of including the behavior in both places over-complicating things? Am I missing something?

È stato utile?

Soluzione

The suggested IsWithinSpec in Widget-Class knows too much implementation detail about the WidgetDescription-class, e.g. that it contains a MeasurementSpecification. A so called TrainWreck. You could instead create a method in WidgetDescription-class and delegate there.

public class Widget 
{
    ...
    public Boolean IsWithinSpec() 
    {
        return this.WidgetDescription.IsWithinSpec(this.MeasurementValue)
    }
}

public class WidgetDesciption
{
    ...
    public Boolean IsWithinSpec(Decimal measurementValue) 
    {
        return this.MeasurementSpecification.IsWithinSpec(measurementValue)
    }
}

This will reduce unncessecary dependencies and make your code easier to test and you will need less mocking when testing.

hth.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top