Cleaner Code With Use Cases - The Full Guide

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ส.ค. 2024
  • This video will show you how you can use use cases (interactors) in your Android project to make your code a cleaner place.
    ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/premium-courses...
    💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:
    elopage.com/s/philipplackner/...
    Subscribe to my newsletter for regular Android, Kotlin & Architecture advice!
    pl-coding.com/newsletter
    Join this channel to get access to perks:
    / @philipplackner
    Regular live codings on Twitch:
    / philipplackner
    Join my Discord server:
    / discord
    Regular programming advice on my Instagram page: / _philipplackner_
    Checkout my GitHub: github.com/philipplackner
    You like my free content? Here you can buy me a coffee:
    www.buymeacoffee.com/philippl...
    00:00 - How Use Cases work
    09:00 - Why we use Use Cases
    12:39 - When not to use Use Cases

ความคิดเห็น • 110

  • @marinbeslo7841
    @marinbeslo7841 2 ปีที่แล้ว +6

    Great explanation! I already used use cases on some smaller projects, so this video has filled the gaps in my knowledge. Vielen Dank, Philipp! Du machst das prima :)

  • @calvindeceuster878
    @calvindeceuster878 2 ปีที่แล้ว +5

    Great video! The clean architecture video was super overwhelming for me, so breaking down some of those things is super helpful, maybe one day i'll be writing code as clean as you.

  • @tonnie7079
    @tonnie7079 2 ปีที่แล้ว +6

    Learning tons of stuff in just a short video, Thanks Phillie and keep more of these coming.

  • @user-gi4no6gn7m
    @user-gi4no6gn7m 2 ปีที่แล้ว +23

    Hey Philipp, please make a video on how to use proguard and R8 with clean architecture based project.

  • @techmarinar
    @techmarinar 2 ปีที่แล้ว

    Thanks man , you always come up with the best

  • @benchakalaka
    @benchakalaka 2 ปีที่แล้ว

    So helpful, thank you very much!!! This is the only later required to understand to build proper mvvm for me!

  • @Christopher-pe6zj
    @Christopher-pe6zj 2 ปีที่แล้ว

    Love the explanation videos.. good to see how it all works! cheers mate!

  • @jackli1924
    @jackli1924 ปีที่แล้ว

    Awesome video!Great and concise explaination of use cases!Thank you so much Philipp!

  • @ebrahimmansur9815
    @ebrahimmansur9815 2 ปีที่แล้ว +3

    Appreciate the work mate... a use case is one of the functional requirements of the app ...it follows the idea of separating the domain logic from the rest of the app but developers are fixated with the name 😅 ...its is just a component(in this context a class) which handle a specific function very well [think of it as a service that provides to other components]
    small tip .....i like my domain functionality to be separate from any layer[does not depend on data layers]........so i will have my state management layer reference the domain function interfaces and having event-handler component implement the interface .....and the same time having infrastructure layer[in this case the data layer] reference an interface from domain and have an component to implement it in the data layer ......basically having both view-model in the this case and data layer depend on domain for the bushiness functionality through the interfaces it provides

  • @MaulikDodiaHollywoodMovieLover
    @MaulikDodiaHollywoodMovieLover 2 ปีที่แล้ว

    Very well explained. Thanks for making this video.

  • @JohanAlbrectsen
    @JohanAlbrectsen 2 ปีที่แล้ว

    Amazing that these videos are free!

  • @brianryan4053
    @brianryan4053 2 ปีที่แล้ว +1

    Nobody and never explained such complicated things so clearly

  • @damercy
    @damercy 2 ปีที่แล้ว

    Great explanation. Thank you!

  • @swaggerlife
    @swaggerlife 2 ปีที่แล้ว

    Great video. Keep up the good work.

  • @AndroidDeveloperTips
    @AndroidDeveloperTips 2 ปีที่แล้ว

    Nice job Philipp 👌

  • @AbelTarazona
    @AbelTarazona 2 ปีที่แล้ว

    Gracias Philipp, muy claro todo :D

  • @hacenezerrouk3770
    @hacenezerrouk3770 2 ปีที่แล้ว

    Love it , thanks man

  • @tanjimahmed213
    @tanjimahmed213 2 ปีที่แล้ว +1

    Thank you ❤️❤️❤️ for your awesome videos

  • @go_better
    @go_better 2 ปีที่แล้ว

    Man, I'm watching one video and want to watch another! It's so addictive!)

    • @Wisiperify
      @Wisiperify 2 ปีที่แล้ว

      It is really super addictive :)

  • @DmitriMolchanov
    @DmitriMolchanov 4 หลายเดือนก่อน

    Every time I watch your videos, I find myself reaching for the "like" button just to realize I've already pushed it. 👍

  • @safionweb
    @safionweb 2 ปีที่แล้ว

    Really helpful video ❤️

  • @bautp
    @bautp 2 ปีที่แล้ว +6

    I have a question if the use case has a multi-action -> example get data from the server after that save the response to the database locally, and display it on UI. We should be defined all in 1 user case or defined in ViewModel. Thanks

  • @frozencoder
    @frozencoder 2 ปีที่แล้ว

    Very understandable, thank you.

  • @ahmedm.abdalla4634
    @ahmedm.abdalla4634 2 ปีที่แล้ว

    very useful, keep going

  • @Sherali_Juraev
    @Sherali_Juraev ปีที่แล้ว

    Great video 👍👍👍

  • @princepatel4864
    @princepatel4864 2 ปีที่แล้ว

    It's very useful video for me thank you sir. I'm really inspired to learn jatpack compose form your vidoe

  • @leavemealone2006
    @leavemealone2006 2 ปีที่แล้ว +8

    I love your content Philip! My only concern with this style of architecture is that is "smells" too much like what Juval Lowy refers to as "Functional Decomposition".
    I do like that using the wrapper class and treating "use cases" collectively as a single component encapsulates the business logic much better than many separate viewModels do. That has always been a concern for me with standard MVVM architecture.
    I can definitely see this as better than typical Android architecture.

  • @algierm2329
    @algierm2329 2 ปีที่แล้ว +1

    Nice information! Maybe for the next topic you could do how to modularize app and separate them in actual module on Android Studio

  • @ahmednashwan8111
    @ahmednashwan8111 2 ปีที่แล้ว +1

    Greate video man!
    I implement usecase on small flutter project and that make project more complex unfortunately and take a long time without any progress😂
    I agree with you that you must implement usecase on big projects android or flutter ..etc.
    Clean arceticture, clean code and SOLID principle make a very good developer but a lot of developers do not know about that or know without any implementation.

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +2

      yea you really have to build some projects with it to understand it fully

  • @ruangaldowork1936
    @ruangaldowork1936 ปีที่แล้ว

    Hi Philip, I like your video. I have a question for you. If I want to get data from API and collect the data that I want. Do I put the mapper in a use case or repository?

  • @mouzinu
    @mouzinu 2 ปีที่แล้ว +1

    Philipp, where should I handle the try/catch blocks? Should I put inside my invoke fun and send the errors to the VM? or wrap the invoke call in VM with the try/cach block?

  • @markli2171
    @markli2171 2 ปีที่แล้ว

    This is very good advice, I hope to see how to modularize app according to feature

  • @saadbiniqbal9024
    @saadbiniqbal9024 2 ปีที่แล้ว

    Great One

  • @yuvrajsharma6803
    @yuvrajsharma6803 ปีที่แล้ว

    Thank you

  • @augustorojas9996
    @augustorojas9996 2 ปีที่แล้ว

    Thanks, Amazing

  • @Sc2MooDy
    @Sc2MooDy 2 ปีที่แล้ว

    Thank you for this video, very useful. Can you please make a tutorial on how to effectively modularise your android app?

  • @backupproducer
    @backupproducer 5 หลายเดือนก่อน +1

    Omg it’s so bizarre how much boiler plate code in jetpack compose. As an iOS swift developer I’m in shock you end up having 25 files and 200 lines of code for an app with 2 buttons and 3 functions. Nuts.

    • @justmeagain9302
      @justmeagain9302 2 หลายเดือนก่อน

      Hahahaha I'm a beginner in app development and im still trying to understand android clean architecture, are you saying that developing apps in ios is better?

  • @husseinchahine9456
    @husseinchahine9456 10 หลายเดือนก่อน

    Great video! i would like to see a video that shows how to retrofit api with query/path using usecase and dependency injection small video if possible. Thanks alot!

  •  2 ปีที่แล้ว

    I used years ago, using MVP and Java, now getting back to them on kotlin

  • @radenyaqien
    @radenyaqien 2 ปีที่แล้ว

    great video

  • @openparachute17
    @openparachute17 2 ปีที่แล้ว

    I like before I watch.

  • @HarshKumar-qw5ou
    @HarshKumar-qw5ou 2 ปีที่แล้ว

    very helpful

  • @GeertBerkers
    @GeertBerkers 2 ปีที่แล้ว +3

    I like this video, but doesn't the ProfileUseCase need @Inject annotation for it to be provided?

  • @coffeebytez
    @coffeebytez 2 ปีที่แล้ว

    This is gold

  • @gevorgmovsisyan5153
    @gevorgmovsisyan5153 8 หลายเดือนก่อน

    What font do you use in the editor? It looks perfect

  • @rajbopche7992
    @rajbopche7992 2 ปีที่แล้ว +3

    What if a single use-case is used across two features?
    Let's say, we have feature_ profile & feature_login as two separate features.
    And say they want to make use of profile_use_case, how to make it available to both of them?

  • @mohamedaliadelabdulhadi876
    @mohamedaliadelabdulhadi876 2 ปีที่แล้ว +1

    hi i relly need you to tell what the place you get this information and this knowladge

  • @arvindsingh34
    @arvindsingh34 ปีที่แล้ว

    Hi Philipp, I am from India and recently saw some videos of you that was very good to learn. How can i get full video of this series please send me the link.
    Thank yoy

  • @Merkost
    @Merkost 2 ปีที่แล้ว

    Hey Phillip! What is the UiText class?

  • @germenwong
    @germenwong 11 หลายเดือนก่อน

    Excellent video. How can I obtain past live broadcasts or tutorials of this project

  • @aminovicnouri227
    @aminovicnouri227 2 ปีที่แล้ว

    thank you very much

  • @vahidgarousii
    @vahidgarousii 2 ปีที่แล้ว +2

    So Cool 😍

  • @7xFuryPlayz
    @7xFuryPlayz ปีที่แล้ว +1

    for smaller projects , which is usually the case for mostly developers now a days , thats will be an over kill ,

  • @erickweil4580
    @erickweil4580 หลายเดือนก่อน

    I'm in a backend project using MVC, not really exactly the scenario there, but if I want to implement a layer beteween the Controller (ViewModel?) and the Model (Repository?) would be correct to call it Service instead of use cases? the idea is to store all the bussiness logic and behaviour in this layer, so that the controller only does in/out of the request and the Model only saves/query the database.
    Also, in practice how often your Use Cases actually become shared by different callers? is it the norm or is somewhat 90% of the time a 1 to 1 relation to the ViewModel?

  • @freedomchuks
    @freedomchuks 2 ปีที่แล้ว

    just my opinion usecases are an optional layer and only not redundant when used to tie buinesslogic that are use multiple times across app or creating an action that requires the combination of two repository or more to create

  • @stonedcodingtom9097
    @stonedcodingtom9097 8 หลายเดือนก่อน

    Can one view model use UseCase wrapper classes from different features? Will it be clean code?

  • @mattgraves3709
    @mattgraves3709 2 ปีที่แล้ว

    You rock!!!!

  • @DerCheckerzeigts
    @DerCheckerzeigts 2 ปีที่แล้ว +11

    Wouldn't creating a default interface for these UseCases be helpful? For example:
    Inteface IUseCase {
    operator fun invoke(input: I): O
    }
    and
    Inteface IAsyncUseCase {
    suspend operator fun invoke(input: I): O
    }

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      You could do that, yes

    • @user-ke3hg9ww4i
      @user-ke3hg9ww4i 2 ปีที่แล้ว +1

      Could you please explain to me why is it helpful?

    • @DerCheckerzeigts
      @DerCheckerzeigts 2 ปีที่แล้ว +3

      @@user-ke3hg9ww4i Because later you can inject the usecases easier. For example, one UseCase could be:
      class MyUseCase : IUseCase
      which you inject in your viewmodel
      class ViewModel(val useCase: IUseCase)
      Later, when you want to change the implementation of IUseCase (e.g you want to delete class MyUseCase and create a new one that implements IUseCase

    • @user-ke3hg9ww4i
      @user-ke3hg9ww4i 2 ปีที่แล้ว

      @@DerCheckerzeigts OK, now I understand, thank you:)

    • @timofeytenushko9946
      @timofeytenushko9946 2 ปีที่แล้ว +2

      @@DerCheckerzeigts I would say that having interface for use-case is more about simple testing of presenter or viewmodel. I haven't had any project that contains more than one implementation of use-case, usually if business logic changes then use-case is just updated accordingly - but that is mine experience.

  • @pqsk
    @pqsk ปีที่แล้ว

    So basically it's the service/repository pattern or the UoW pattern. A junior dev told me about this recently and I thought he was talking about real use cases. I don't really like the name because it can be confusing, but love the architecture. I've used this pattern for 13+ years

  • @ghostjkeee6641
    @ghostjkeee6641 2 ปีที่แล้ว

    Why is ProfileUseCases data class ? Do you use equals() or hashCode() ?

  • @abdulkarimalbaik3873
    @abdulkarimalbaik3873 2 ปีที่แล้ว

    Thanks bro , can you make a video how implement mvvm and mvi and use-cases and pagination together ???

  • @azmo_
    @azmo_ ปีที่แล้ว

    where are all the twitch streams saved? Can't find them

  • @amateur_gamer101
    @amateur_gamer101 2 ปีที่แล้ว

    Please Can we get the link to this repo? I would like to study it. The socialmediaTwitch app on your repo doesn't have these Use Cases. Pls i need this. Can i get it?

  • @paulsoja2732
    @paulsoja2732 2 ปีที่แล้ว +1

    What about kotlin Result instead of own class Resource?

  • @kazimd.saidul4212
    @kazimd.saidul4212 2 ปีที่แล้ว

    Can you share some code how to Save data and get data in shared preference from usecase ?

  • @asadsatti5354
    @asadsatti5354 ปีที่แล้ว

    yes

  • @anvarzaripboyev5730
    @anvarzaripboyev5730 ปีที่แล้ว

    That is great job!
    But in my app, I cannot decide to put my RecyclerView adapters anywhere! Where are adapters place is, I mean directory structure for adapters! Thanks beforehand
    !

  • @SoulWithoutFaith
    @SoulWithoutFaith ปีที่แล้ว

    SetSkillSelectedUseCase uses UiText.StringResource (also mentions toggling), doesn't this mean that it violates layer separation? Presentation details are in domain layer and it should not be like that.

  • @vishalthapa1440
    @vishalthapa1440 2 ปีที่แล้ว

    can we or should we use usecases in simple mvvm project if i dont want to use clean architecture ?

  • @Guilo583
    @Guilo583 2 ปีที่แล้ว

    Hi Phillip Thx for the video. It is possible to share the source code?

  • @aksonsoftware8856
    @aksonsoftware8856 11 หลายเดือนก่อน

    Why you use copy() to change only one property of the object? I suppose it would be faster and more efficient (less CPU ticks) to just assign only changed field like _passwordSate.value = event.value instead _password.value = _passwordState.value.copy( text = event.value ). However, possible I understood you wrong, so please forgive and give me explanation. I am new in Kotlin. Thank you for this great video guide.

  • @wavecycle
    @wavecycle 2 ปีที่แล้ว

    When I do this example I need to add the empty injected constructor to the use case, in order to inject in successfully into the ViewModel, else Hilt won't compile. You're not doing that tho, how come? Thank you :)

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      I provide the use case in the AppModule. I try to have as little @Inject constructors as possible. Even though it's very unlikely it can happen that you decide to change the DI library. Then I'd prefer not needing to change a lot. But your approach is definitely also not wrong or bad

  • @bitwisedevs469
    @bitwisedevs469 2 ปีที่แล้ว

    You can also use them for domain model mapping

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      No, thats the job of the data layer

    • @bitwisedevs469
      @bitwisedevs469 2 ปีที่แล้ว

      @@PhilippLackner ohh yeah, mappers are injected in repository implementation which is part of data layer.

  • @coffemilk652
    @coffemilk652 3 หลายเดือนก่อน

    What if use case has a own state ?

  • @MinhNguyen-hf5yh
    @MinhNguyen-hf5yh 2 ปีที่แล้ว

    Thank you for your great content. I have a question. If the logic of the use case have to return context.getString(id) depend on data from server. But I can not put the context to ViewModel or UseCase. Where can I put them?

  • @ashokdhiv
    @ashokdhiv 2 ปีที่แล้ว

    hello philipp i cant upgrade my level in your channel membership,help me..I joined yesterday ..Cant access all videos

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว

      What's the issue?

    • @ashokdhiv
      @ashokdhiv 2 ปีที่แล้ว

      @@PhilippLackner Upgrade plan option is disabled

  • @alexanderparra7889
    @alexanderparra7889 2 ปีที่แล้ว

    I think that you forgot to mention that usecases are important for business logic test. Also I would suggest to use interface in use cases in order to make uses cases testables

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว

      An interface is not really needed to test use cases since they only contain business logic. You only need interfaces for the dependencies the use case needs such as repository

    • @alexanderparra7889
      @alexanderparra7889 2 ปีที่แล้ว

      @@PhilippLackner ok, are you saying that is not need it to test usecases because they contain business logic?. sorry bro this doesn't make sense to me

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว

      Let's take a use case that validates registration fields and then makes an api call using the repository. Why would you need an interface there for the use case?

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      If you want to test a lot in your viewmodel that could be helpful, but since the viewmodel doesn't contain business logic anymore there isn't much to test. It only calls use cases and maps state

  • @angco9022
    @angco9022 ปีที่แล้ว

    what is the fastest way to learn android development. joining youtube? twich? your courses?

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว

      TH-cam + my courses + my mentorship program👍🏼

  • @stonedagecooking2748
    @stonedagecooking2748 2 ปีที่แล้ว +3

    Hey Philipp Lackner, I don't see a lot of value in creating Usecases when our Viewmodel just calls it. For the example of reusing business logic, I think we can just put the same business logic in the viewmodel, and reuse that business logic on different views. For example, if we have a BasicProfileView and an AdvanceProfileView, we can just have 1 ProfileViewModel that shares the same business logic and re-use that ProfileViewModel across two different views. I am not sure if adding useCases will really simplify things here. Most business logics on mobile is just calling some CRUD rest endpoints, and if nested queries are required, its usually best done on the server side or using technologies like GraphQL. In comparison to Interactors which usually bridge the gap between systems that has different interfaces (different programming languages for example), I don't think having that layer in a domain that uses the same programming language is really needed.

  • @saniksun
    @saniksun ปีที่แล้ว

    Move your usecases to separate gradle java module to make it reusable

  • @keepgoingman5829
    @keepgoingman5829 2 ปีที่แล้ว +1

    Masterpiece Masterpiece Masterpiece Masterpiece Masterpiece Masterpiece

  • @siva_subramaniam
    @siva_subramaniam ปีที่แล้ว

    11:30 - God ViewModels, lmfaoo

  • @avtaar3041
    @avtaar3041 2 ปีที่แล้ว

    you look like henry cavill (Man of steel ).

  • @vanhieuao4154
    @vanhieuao4154 11 หลายเดือนก่อน

    I see use case classes are singleton. Why don't u make it is object class instead of normal class?

  • @saniksun
    @saniksun ปีที่แล้ว

    Interactors != UseCases

  • @McRookworst
    @McRookworst 2 ปีที่แล้ว

    This pattern seems overly verbose

  • @itsmecdnm47
    @itsmecdnm47 2 ปีที่แล้ว

    Great explanation!
    I have one doubt.
    Consider a login module
    If the login is successful, I need to save the member id.
    So I need to save the member id into SharedPreference.
    Do i need to save this from viewmodel
    try {
    val memberId = loginUseCases.verifyLoginUseCase(LoginRequest(phoneNumber.value.text, password.value.text))
    if(memberId != null) {
    //Save to Shared Preference
    _eventFlow.emit(UiEvent.LoginVerified)
    }
    else _eventFlow.emit(UiEvent.ShowSnackBarError("Invalid username or password"))
    }catch (e: InvalidLoginException){
    _eventFlow.emit(UiEvent.ShowSnackBarError(e.message?:"Couldn't authenticate"))
    }
    or
    In use case
    After making the call to verify a login, do I need to save the member id to shared preference and throw an exception in case the login credentials mismatch?