Mehdi.
Mehdi.

Reputation: 369

Where do value objects come from in DDD?

I am currently working on side project to get a feel of DDD, and I am wondering what would be the repository-ish component to get my value objects from.

In my marketplace bounded context, a Catalog is managed by a Merchant and contains Offers organized in Categories and Universes

here, my guess is that Merchant, Catalog and Universe would be Aggregate Roots because they have global identity in the system whereas Offer would be an Entity of the Catalog Aggregate and Category would be a Value Object acting like a tag on the offers.

Then I would have Repositories to act as a facade to persistence for the ARs, but I don't get how, regarding to DDD building blocks, I am to deal with the retrieval of Categories (a Merchant should be able to define the categories within his Catalog, and then use them to tag the offers he adds to it).

Could anyone provide some guidance ?

Thank you

Upvotes: 0

Views: 150

Answers (1)

Njål Nordmark
Njål Nordmark

Reputation: 596

As you pointed out, value objects are not entities, and thus shouldn't be directly retreived from a repository. They are, however, aspects/properties of an entity.

When you wish to reference all Categories of the Catalog, you are, in principle, performing a query against the Catalog; which Categories do you contain?

Since both the Categories and the Catalog are aggregate roots, you might need to use a domain service (these exists to perform cross-aggregate operations, amongst other things).

This could result in the following code:

class CategoryListingService
{
    public ICollection<Category> GetCategoriesForCatalog(Catalog catalog)
    {
        var offers = offerRepository.GetAllOffersFrom(catalog)
        var categories = offers.Select(o => o.Category).Distinct();
        return categories;
    }
}

Note that this is sample code in C#, but the idea behind it should work, and allow you to list all the categories on offers which are present on a catalog.

Upvotes: 1

Related Questions