开发者

Currency is value object or not

开发者 https://www.devze.com 2023-02-25 20:34 出处:网络
I have Person aggregate, which is root aggregate public class Person { private int id; private readonly PersonID personID;

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 ?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号