architecture - NetWeight 和 GrossWeight 作为单独的值对象
问题描述
创建不同重量子类型的原因是什么?我看到我们的开发人员将 NetWeight 和 GrossWeight 子类型创建为值对象。它们都有相同的实现。它有什么价值吗?为什么不在这两种情况下都使用权重值类型?我觉得它完全错了,想听听专家对此的看法......如果你觉得它错了,你会如何解释为什么它错了?
public struct NetWeight : IEquatable<NetWeight>
{
private const string DefaultMeasurementUnit = "kg";
public double Value { get; }
public string MeasurementUnit { get; }
public NetWeight(double value, string measurementUnit)
{
if (value < 0) throw new BusinessRuleValidationException("NetWeight value can't be negative");
if (string.IsNullOrWhiteSpace(measurementUnit)) throw new BusinessRuleValidationException("NetWeight measurement unit can't be null or whitespace");
Value = value;
MeasurementUnit = measurementUnit.Trim();
}
public override string ToString()
{
return $"{Value}{MeasurementUnit}";
}
public static NetWeight operator +(NetWeight left, NetWeight right)
{
if (left.MeasurementUnit != right.MeasurementUnit) throw new ArgumentException("Measurement units are not the same");
return new NetWeight(left.Value + right.Value, left.MeasurementUnit);
}
public static NetWeight operator -(NetWeight left, NetWeight right)
{
if (left.MeasurementUnit != right.MeasurementUnit) throw new ArgumentException("Measurement units are not the same");
return new NetWeight(left.Value - right.Value, left.MeasurementUnit);
}
public static NetWeight operator *(NetWeight left, Quantity right)
{
if (left.MeasurementUnit != right.MeasurementUnit) throw new ArgumentException("Measurement units are not the same");
return new NetWeight(left.Value * right.Value, left.MeasurementUnit);
}
public static NetWeight operator *(Quantity left, NetWeight right)
{
if (left.MeasurementUnit != right.MeasurementUnit) throw new ArgumentException("Measurement units are not the same");
return new NetWeight(left.Value * right.Value, left.MeasurementUnit);
}
// TODO: come up with a refactoring that prevents the use of primitive types
public static NetWeight operator *(NetWeight left, int right)
{
return new NetWeight(left.Value * right, left.MeasurementUnit);
}
#region IEquatable
public override bool Equals(object obj)
{
return obj is NetWeight weight && Equals(weight);
}
public bool Equals(NetWeight other)
{
return Value == other.Value &&
MeasurementUnit == other.MeasurementUnit;
}
public override int GetHashCode()
{
return HashCode.Combine(Value, MeasurementUnit);
}
public static bool operator ==(NetWeight left, NetWeight right)
{
return left.Equals(right);
}
public static bool operator !=(NetWeight left, NetWeight right)
{
return !(left == right);
}
#endregion
}
解决方案
创建不同重量子类型的原因是什么?
如果 GrossWeight 和 NetWeight 在您的领域中是不同的不同概念,如果用毛重代替净重是错误的,并且如果您使用的语言会标记不兼容的类型,那么将 GrossWeight 和 NetWeight 建立为不同的类型允许您可以使用该语言自己的类型检查器来标记(从而消除)某些可能会逃逸到生产环境中的错误类别。
两种不同的权重目前具有等效的实现这一事实本质上是一个实现意外。在支持它的语言中,您可以考虑让这两种类型从某个共同祖先那里继承它们的实现。
许多模型将使用类似的模式来处理标识符——即使在实现级别没有区别,区分CustomerId
类型和OrderId
类型以确保您不会无意中替换一个类型来破坏您的数据集。其他。
您将看到这种方法的另一个地方是在区分不可信输入和可信数据的模型中。当然,底层的原始表示是“只是字节”,但是它允许您利用类型系统来区分已经通过验证检查点的数据和没有通过验证检查点的数据。
推荐阅读
- c# - 如何在 C# 中为字符串数组的元素赋值?
- javascript - MIME 类型 ('text/html') 不受支持
- python - 如何根据输入列表激活类?
- python - python re.compile() 和 re.findall()
- gcc - 在 Jonesforth 中使用 GDB 显示字典单词
- azure - 使用通配符在 blob 中搜索文件名
- java - 使用基本数学运算符将二进制字符串转换为整数
- html - BeautifulSoup 匹配错误的类
- javascript - 如何使用 w3.css 作为框架以角度显示模态对话框?
- node.js - 为 Vue 应用程序前端和 node.js 后端配置 nginx