Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Use cases in the code with AOP

1,174 views

Published on

Slides from my http://lightning.io talk in Poznan.

  • Be the first to comment

  • Be the first to like this

Use cases in the code with AOP

  1. 1. Use cases are importantAndrzej Krzywda
  2. 2. Use casesin the code are even more important
  3. 3. class PlayTeamGameUseCase constructor: (@game, @player) -> Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  4. 4. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  5. 5. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Actors Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  6. 6. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Actors Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) Roles tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  7. 7. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Actors Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) Roles tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() Algorithm else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  8. 8. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Actors Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) Roles tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() Algorithm else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() uses domain objects if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  9. 9. Nameclass PlayTeamGameUseCase constructor: (@game, @player) -> Actors Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) Roles tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() @teachPlayerHowToPlay() Algorithm else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() uses domain objects if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here) Nothing about GUI or persistence!
  10. 10. Where is?• GUI• Persistence• API calls• events
  11. 11. A clean use case code is the main goal
  12. 12. AOP + DCI
  13. 13. Aspect Oriented Programming
  14. 14. AOP gives usmethod hooks
  15. 15. After, Before, Around
  16. 16. GUI & persistence
  17. 17. class GameUseCaseGlue constructor: (@gameUseCase, @game, @gui) -> execute: () => Around(@gameUseCase, tryToEnterGameArea, @checkFbInvitation) After (@gameUseCase, tryToEnterGameArea, @showTeamArea) After (@gameUseCase, tryToEnterGameArea, @showButtonInviteOrPostPicture) Around(@gameUseCase, tellPlayerHeIsPartOfTeam, @showTeamPopup) Around(@gameUseCase, askPlayerToLikeFanpage, @showLikePopup) Around(@gameUseCase, teachPlayerHowToPlay, @showTutorialPopup) Around(@gameUseCase, playerWantsToKnowWinnersWithPrize, @showWinnersPopup) Around(@gameUseCase, playerWantsToKnowPrizes, @showPrizesPopup) Around(@gameUseCase, askPlayerToDeclareHisFavCountry, @showDeclareCountryPopup) Around(@gameUseCase, iAcceptMyFriendInvitationToATeam, @onIAcceptMyFriendInvitationToATeam) Around(@gui, inviteClicked, @inviteFriends) # gameUseCase.inviteFriends Around(@gameUseCase, inviteFriends, @storeInvitationInTheDB) DB here Glue code
  18. 18. Around(@gameUseCase, tellPlayerHeIsPartOfTeam, @showTeamPopup)
  19. 19. (still glue code)showTeamPopup: (proceed, friendsInviting) => data = {inviting_friends: friendsInviting} popup = @popupsComponent.showPopup(team_popup, data) popup.onClose => proceed(friendsInviting))
  20. 20. DCI
  21. 21. class PlayTeamGameUseCase Use case == context constructor: (@game, @player) -> Role(@player, PlayerWithFriends) Role(@game, TeamCompetition) Roles injected runtime tryToEnterGameArea: () => if @amIEnteringGameFirstTime() if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() data objects interact with @teachPlayerHowToPlay() each other else #n-th time... if @amICommingFromInvitation() @tellPlayerHeIsPartOfTeam() if @haveNotYetPickedFavPizzaCountry() @askPlayerToDeclareHisFavPizzaCountry() (more methods here)
  22. 22. class PlayerWithFriends setup: => a role @friends = [] @invitedFriends = [] @acceptedFriends = [] setInvitedFriends: (facebookUids) => for facebookUid in facebookUids friend = new Friend({facebookUid: facebookUid}) @invitedFriends.push(friend) setFriends: (friends) => @friends = friends addFriend: (friend) => existing = @getFriendByFacebookUid(friend?.facebookUid) if not existing? @friends.push(friend)
  23. 23. not only forSingle Page Applications
  24. 24. What use cases are in your project?
  25. 25. learn more about AOP and DCI and ... Enjoy writing elegant use cases!

×