In clean architecture, the main core is usecase layer which contains business logics. Now in Android we will use case and pass it to viewmodel as param in constructor. For example
class AddNoteUseCase(noteRepository:INoteRepository):IUseCase{
override fun invoke(note:Note){
noteRepository.addNote(note)
}
}
I saw two type of code to pass usecase for viewmodel
- Usecase is tightly coupled to viewmodel
class NoteViewModel(addNoteUseCase:AddNoteUseCase){
// usecase code
}
- Usecase is loosely coupled to viewmodel
class NoteViewModel(addNoteUseCase:IUseCase){
// usecase code
}
If we go with first approach then we cant fake and also it would be difficult for future changes. If we go with second approach then we able to fake for unit testing
My question is which approach will be helpful. Because some says one approach because there is no need of faking the usecase as it only contains one method some says without faking how will you test the usecase
I would say that the second variant is right. From my experience, if u have a base usecase class/interface, ususally it looks like this:
And let's suppose u have a subclass
AddNoteUseCase:If u pass this usecase as a param to VM in this way:
it still makes your code readable(since u understand by the name, what this usecase does) and testable (since u can pass any implementation u want in unit tests). Imho, there's no need to create a separate abstraction for each usecase, since all of them have the same interface - one public method invoke and that's all