Application level design - what is your best practice?

Application-level development is not trivial. At the end of the day in each project, we write (a huge number) of so-called business methods (sometimes called service methods, although they are not related to services, as in the public APIs), which execute some business logic and usually execute some database / storage (via storage level). But designing / organizing / naming your application methods is not trivial as it sounds; people often have rules for the design of how to develop application classes and methods. I am looking for a pragmatic set of such rules for application level design, so here are a few questions:

  • Grouping methods in classes: by return type or?

    For example, a method that updates a user belongs to a class, for example UserService. Or a method that finds a user by some criteria belongs to the same class. But what happens when a method works on several types, for example registerUserForCompetition()? Should it be in UserServiceor CompetitionService? The same question for the method findUserCompetition()- where would you express it?

    I often see a rule saying that it depends on the type of method returned. In our example, if a method returns User(or a set of users, etc.), it must belong UserService.

  • Method parameters: simple types (primitives, String ...) or entity (eg, User, Competition, Registration...)?

    This question is often asked, and usually people (statistically) choose the first approach, using simple types as arguments to the service. But what if one of the methods calls another method on the same resource? Let me give you an example: it registerUser(userId, competitionId)can internally call checkCompetition(competitionId). Both methods are retrieved Competitionfrom the repository. Obviously, this is done twice, and since we already have the object Competition, we can use it checkCompetitionwith it. So you should add an overloaded method, or should we ignore this and just actually have a caching mechanism to prevent double fetching?

    , , , , , .

  • : verbose?

    . , :

    • findCompetition(userId, year)
    • findCompetitionWithVenue(userId, year) ( Competition Venue);
    • findCompetitionForUserAndYear(userId, year)
    • findCompetitionByUserIdAndYear(userId, year)
    • findUserCompetitionForYear(userId, year)?

    , , : find<return-type>By<names-of-parameters> .

  • ?

    Service? , , ? - (, ...) ?

, - , , - , . , , , .

+4
1
  • . , .. , . DDD. ,

  • (), , DTO ValueObjects. ,

  • . , , . findCompetition(user, year) , , , findCompetition(forUserId(userId), forYear(year)), , .

  • ? . , .. , DTO , /, . ( ) , "", ( , , , ..)

    /li >
+1

Source: https://habr.com/ru/post/1533417/


All Articles