Kratos
Kratos

Reputation: 957

Domain layer with Use Cases in android app

I am implementing MVVM android app with domain layer. It goes standard way:

Activity -> ViewModel -> UseCase(with injected repository)

I have one architectural question that I would like to ask here. My app packaging has 2 packages "data", "domain" (it has more, but these 2 are important for the question).

I saw examples where people create in package structure "usecase" folder in both domain and data package. In these situations, the "usecase" package in domain contains interfaces for usecases and serves as Abstraction. On the other hand, the "usecase" package in data layer serves as implementation of this abstraction (here classes usually have names something like ParticularUseCaseImpl).

When I think deeper about it, it does not make much sense to have abstraction of use case. What are the benefits? You can change use case by changing what you inject inside it, but it should almost always have same implementation in the operator function, shouldn't it?

It looks like overkill. If we directly make useCase implementations in the domain layer (without abstractions), do we break any of the CLEAN architecture rules -> (https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)

In this link, I understood that domain layer (use cases) should have only abstractions and that next layer (Interface Adapters) should have their implementations.

Would be nice to understand this matter absolutely, with no any question marks above head. Would appreciate some well explained answers. Thanks!

Upvotes: 1

Views: 1393

Answers (1)

Ruslan Zhuravlov
Ruslan Zhuravlov

Reputation: 216

You should probably only create an interface if you plan to have two or more DIFFERENT implementations of the same abstraction.

A class from the "presentation" layer (for example, ViewModel) can depend on classes from the "presentation", "domain" or "data" layer.

A class from the "domain" layer (for example, UseCase) can only depend on classes from the "domain" or "data" layer.

A class from the "data" layer (for example, Repository) can only depend on classes from the "data" layer.

That is, you should not have imports of classes from the "domain" layer in a class from the "data" layer.

You can read about this here: https://developer.android.com/topic/architecture/domain-layer

Upvotes: 0

Related Questions