货币是否是价值对象

发布于 2024-11-01 16:10:37 字数 1507 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-11-08 16:10:37

如果您所说的“货币”是指应用程序中的货币定义,例如美元、埃及镑、欧元等,那么它应该是可重用的实体。如果您指的是货币金额的价值,例如 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.

回眸一遍 2024-11-08 16:10:37

以下引用来自 Eric Evans,他描述了值对象是什么:

代表一个对象
域的描述性方面
没有概念上的同一性被称为
价值对象。值对象是
实例化来表示元素
我们只关心的设计
他们是什么,而不是他们是谁或哪些
是。

关于此的另一个参考是关于域驱动设计的MSDN文章戴夫·拉里比 (Dave Laribee) 说道:

值对象是描述符或
在您的领域中重要的属性
正在建模。与实体不同,它们确实
没有身份;他们只是
描述已有的事物
身份。您是否正在更改实体
称为“三十五美元”或
您增加了余额
帐户?

使用这两个引用,我会说货币应该是一个值对象而不是一个实体。随着时间的推移,货币没有任何形式的身份——它是个人实体的描述性属性——我猜他们更喜欢用这种货币来计费。

在两个不同的聚合中使用相同的值对象完全没有问题。

另一篇可能对您有帮助的好文章是由 撰写的Jimmy Bogard


在您提供附加信息后:

我仍然会说货币最好建模为值对象 - 它看起来仍然是不可变的。

当您加载 Person 聚合时,您需要该查询的一部分来加载货币值对象,它是主要商店货币。

为了更新数据库中的货币(例如更改主商店货币)或列出可用货币,您不需要进行聚合,聚合并不是所有数据访问所必需的 - 它们仅用于协调实体之间以可管理的方式建立关系。

The following quote is from Eric Evans where he describes what a Value Object is:

An object that represents a
descriptive aspect of the domain with
no conceptual identity is called a
VALUE OBJECT. VALUE OBJECTS are
instantiated to represent elements of
the design that we care about only for
what they are, not who or which they
are.

Another reference on this is the MSDN article on Domain Driven Design written by Dave Laribee where he says:

Value objects are descriptors or
properties important in the domain you
are modeling. Unlike entities, they do
not have an identity; they simply
describe the things that do have
identities. Are you changing an entity
called "Thirty-Five Dollars" or are
you incrementing the balance of an
account?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文