货币是否是价值对象
我有 Person 聚合,它是根聚合
public class Person
{
private int id;
private readonly PersonID personID;
private readonly string email;
private readonly string firstName;
private readonly string lastName;
private readonly string username;
private readonly string password;
private readonly Address BillingAddress;
}
public class Currency : IValueObject<Currency>
{
private string name;
private string currencyCode;
private decimal rate;
private string displayLocale;
private string customFormatting;
private int displayOrder;
private bool primaryExchangeRateCurrency;
private bool primaryStoreCurrency;
//<summary>
//Gets or a value indicating whether the currency is primary exchange rate currency
//</summary>
public bool IsPrimaryExchangeRateCurrency
{
get
{
return primaryExchangeRateCurrency;
}
}
/// <summary>
/// Gets or a value indicating whether the currency is primary store currency
/// </summary>
public bool IsPrimaryStoreCurrency
{
get
{
return primaryStoreCurrency;
}
}
}
和Currency 类,它将在 Person 类中引用。
所以现在如果创建了一个 Person 实体,我们也需要将它关联到一种货币。但是在创建的所有货币中,我想知道哪个是默认的主要商店货币。我不想通过 Person 了解它,因为它只包含单一货币。我想从所有创建的人的货币中获取一种货币 PrimaryStoreCurrency 。
我想在下拉列表中绑定货币,以便用户可以从下拉列表中选择其货币并在我们的系统中注册。
那么,我是否将货币创建为单独的聚合?
I have Person aggregate, which is root aggregate
public class Person
{
private int id;
private readonly PersonID personID;
private readonly string email;
private readonly string firstName;
private readonly string lastName;
private readonly string username;
private readonly string password;
private readonly Address BillingAddress;
}
public class Currency : IValueObject<Currency>
{
private string name;
private string currencyCode;
private decimal rate;
private string displayLocale;
private string customFormatting;
private int displayOrder;
private bool primaryExchangeRateCurrency;
private bool primaryStoreCurrency;
//<summary>
//Gets or a value indicating whether the currency is primary exchange rate currency
//</summary>
public bool IsPrimaryExchangeRateCurrency
{
get
{
return primaryExchangeRateCurrency;
}
}
/// <summary>
/// Gets or a value indicating whether the currency is primary store currency
/// </summary>
public bool IsPrimaryStoreCurrency
{
get
{
return primaryStoreCurrency;
}
}
}
and Currency class, which is to be referenced in Person class.
So now if a Person entity is created , we need to associate it a currency too.But among all Currencies created, i want to know which is the default primary store currency. I don't want to know it through Person because it contains only single currency. I want to get a currency which is PrimaryStoreCurrency from all created currencies of persons.
I want to bind currency in drop down so that user can select its currency from dropdown and register in our system.
So, do i create Currency as seperate aggregate ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您所说的“货币”是指应用程序中的货币定义,例如美元、埃及镑、欧元等,那么它应该是可重用的实体。如果您指的是货币金额的价值,例如 1000 美元,那么它是一个封装金额和货币类型的值对象。
If you mean by Currency the currency definition in the application like USD, EGP, EUR, .. and so on, it should be reusable entity. If you mean the value of money amounts, like 1000 USD, it's a value object encapsulating the amount and the currency type.
以下引用来自 Eric Evans,他描述了值对象是什么:
关于此的另一个参考是关于域驱动设计的MSDN文章戴夫·拉里比 (Dave Laribee) 说道:
使用这两个引用,我会说货币应该是一个值对象而不是一个实体。随着时间的推移,货币没有任何形式的身份——它是个人实体的描述性属性——我猜他们更喜欢用这种货币来计费。
在两个不同的聚合中使用相同的值对象完全没有问题。
另一篇可能对您有帮助的好文章是由 撰写的Jimmy Bogard
在您提供附加信息后:
我仍然会说货币最好建模为值对象 - 它看起来仍然是不可变的。
当您加载 Person 聚合时,您需要该查询的一部分来加载货币值对象,它是主要商店货币。
为了更新数据库中的货币(例如更改主商店货币)或列出可用货币,您不需要进行聚合,聚合并不是所有数据访问所必需的 - 它们仅用于协调实体之间以可管理的方式建立关系。
The following quote is from Eric Evans where he describes what a Value Object is:
Another reference on this is the MSDN article on Domain Driven Design written by Dave Laribee where he says:
Using these two references I'd say that Currency should be a Value Object and not an entity. Currency doesn't have any sort of identity through time - it is descriptive property of the person entity - the currency that they prefer to be billed in I guess.
And there is no problem at all in using the same Value Object in two different aggregates.
Another nice post that may help you was written by Jimmy Bogard
After your additional information:
I would still say that Currency is best modelled as a Value Object - it still appears to be immutable.
When you load up your Person aggregate you require part of that query to be loading the Currency Value Object which is the primary store currency.
For updating the currency in the database (changing which is the primary store currency for example) or for listing the available currencies, your do not need to go through an aggregate, aggregates are not mandatory for all data access - they only serve for coordinating the relationships between entities in a managable way.