Base solution for your next web application
Open Closed

Complex Entities and Services #460


User avatar
0
codemonkey21 created

This question is more of a best practice than a code question. It's related to complex or nested entities and if you need a separate service class for each entity or if you should put related managers and methods in the parent/master entity service class? Let me give you some examples;

User: Entity (Parent object) UserImage: Entity (collection) UserCheckIn:Entity (collection) UserActivity:Entity (collection) UserPoints:Entity (collection) Review:Entity (collection)

A user has a collection of images, checkings, activities, points, etc. I know I should create a UserImageManager at the domain level in the Core project, but would you create a UserImageAppService OR put it in the UserAppService since it relates to the User object owns it.

Venue:Entity (Parent object) Review:Entity (collection) ReviewRating:Entity (collection)

Second example. A venue has reviews, reviews have ratings (helpful, not helpful). Would you create a separate ReviewAppService or add a method to the VenueAppService with a ReviewManager reference like _reviewManager.AddReviewToVenue(CreateReviewInput input) since ultimately a review can only belong to one venue.

Just wanted to put out the question and see what other people are doing or how Halil thinks it should work. Most of the examples / samples are too simple compared to real world to figure out best practice.

Thanks!


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This structure is exactly what Aggregate is in DDD. User is aggregate root in your case.

    So, for a best practice, I suggest to create a single UserManager and create UserManager.AddImage(...) method for example for UserImage adding. So, one manager is enough for one aggregate.

    For application services, it may be different and more flexible. If you will implement all operations of User-related entities in a single screen, then you can create a single application service.

    App services are mostly related to your presentation layer and use cases. For example; You can have 3 app services:

    • UserAppService to manage users (used by admin page to create, delete... users).
    • UserProfileAppService to manage user images, passwords etc,
    • UserActivityService to get activities, points, reviews... etc. This sample was arbitrary.

    As a brief, think domain services and app services from different view points.