Fajarmf
Fajarmf

Reputation: 2273

Where to put validation of value object that require database lookup?

I have an entity Coupon with some attributes and one of them is platform. Platform is a value object with single attribute:name. For example, there is Coupon for web platform and there is also another one for Android app platform.

In the admin dashboard, an admin can specify platform for the new Coupon she is creating. Since our business is growing I decided to put list of valid/supported platform in a table in database. So when we rollout our new iPhone app, I can simply add new entry to the platform table (I haven't had time to create dashboard for it but it's possible in the future, but for now adding directly to database is simple enough).

My current implementation is as follows:

CouponApplicationService {
  createCoupon(percentage, platformName) {
    platform = platformRepository.getByName(platform name)
    coupon = new Coupon(percentage,platform)
    couponRepository.save(coupon)
  }
}

However, I am feeling weird that I have to create repository for a ValueObject. Is it actually okay to do so? Or maybe I was mistaken to think that Platform is a ValueObject and it's actually an entity that require an ID?

Thank you

Upvotes: 3

Views: 692

Answers (1)

theDmi
theDmi

Reputation: 18024

Having a repository for value objects is indeed a code smell, although not a very serious one as long as you only perform lookup operations like the ones above.

Still, I think it would make sense to reconsider your domain. Maybe there is an entity SupportedPlatforms or even a bigger concept that contains the list of platforms? If this is the case, this would be the best solution.

If not, consider defining an application configuration object. Often, systems have these kinds of configuration objects in the database if such a flexibility is needed. Note that this is usually not a business concept. Still you can implement a repository to load that config. In your case, it could contain the list of all supported platforms.

If you decide to go down that path, there may be an opportunity for performance improvement if the app config object rarely changes. You may be able to cache it in the application. (But in any case, be sure not to end up with premature optimization).

Upvotes: 4

Related Questions