Skip to content

nathanfallet/usecases

Repository files navigation

usecases

License Issues Pull Requests Code Size codecov

UseCase utils for all my libs.

Installation

Add dependency to your build.gradle(.kts) or pom.xml:

api("me.nathanfallet.usecases:usecases:1.6.1")
<dependency>
    <groupId>me.nathanfallet.usecases</groupId>
    <artifactId>usecases-jvm</artifactId>
    <version>1.6.1</version>
</dependency>

Or in a JS project with:

npm install usecases-kt
yarn add usecases-kt

Usage

First UseCase

Create a new class that extends IUseCase or ISuspendUseCase:

interface IMyUseCase : IUseCase<Input, Output>
class MyUseCase(
    private val dependency1: Dependency1,
    // ...
) : IMyUseCase {

    // If you want to use suspend functions, use `ISuspendUseCase` instead
    override fun invoke(input: Input): Output {
        // Do something with dependencies and input
        // ...

        // Return output
        return Output()
    }

}

Then, you can use it like this: (example with Koin, but you can use any DI library, or even instantiate it manually)

single<IMyUseCase> { MyUseCase(get(), /*...*/) }
val useCase = get<IMyUseCase>()
val output = useCase(Input())

Variants

IUseCase and ISuspendUseCase are the base interfaces taking one input and returning one output. We made some variants to make it easier to use:

  • IUnitUseCase and IUnitSuspendUseCase for no input
  • IPairUseCase and IPairSuspendUseCase for two inputs
  • ITripleUseCase and ITripleSuspendUseCase for three inputs
  • IQuadUseCase and IQuadSuspendUseCase for four inputs

Models

A common use of UseCases is to make things with a model. That's why we made an interface for models with associated UseCases:

data class MyModel(
    override val id: Long,
    val property1: String,
    // ...
) : IModel<Long, CreateMyModelPayload, UpdateMyModelPayload>
data class CreateMyModelPayload(
    val property1: String,
    // ...
)
data class UpdateMyModelPayload(
    val property1: String?,
    // ...
)

CreateMyModelPayload and UpdateMyModelPayload are payloads used to create and update the model. In case you don't support creating or updating your model, you can use Unit instead.

Then, you can create and use associated UseCases:

class ListMyModelUseCase : IListModelUseCase<MyModel> {
    /* ... */
}
class GetMyModelUseCase : IGetModelUseCase<MyModel, Long> {
    /* ... */
}
class CreateMyModelUseCase : ICreateModelUseCase<MyModel, CreateMyModelPayload> {
    /* ... */
}
class UpdateMyModelUseCase : IUpdateModelUseCase<MyModel, Long, UpdateMyModelPayload> {
    /* ... */
}
class DeleteMyModelUseCase : IDeleteModelUseCase<MyModel, Long> {
    /* ... */
}

Expecting those interfaces can help you to make your code more generic and reusable.

Of course, you can also use suspending variants: IListModelSuspendUseCase, IGetModelSuspendUseCase, ICreateModelSuspendUseCase, IUpdateModelSuspendUseCase and IDeleteModelSuspendUseCase.

Models with Repositories

We also provide IModelRepository and IModelSuspendRepository to make repositories for your models:

class MyModelRepository(
    private val dependency1: Dependency1,
    // ...
) : IModelRepository<MyModelRepository, MyModel, Long, CreateMyModelPayload, UpdateMyModelPayload> {

    override fun list(context: IContext?): Model? {
        /* ... */
    }

    override fun list(pagination: Pagination, context: IContext?): Model? {
        /* ... */
    }

    override fun get(id: Id, context: IContext?): Model? {
        /* ... */
    }

    override fun create(payload: CreatePayload, context: IContext?): Model? {
        /* ... */
    }

    override fun update(id: Id, payload: UpdatePayload, context: IContext?): Boolean {
        /* ... */
    }

    override fun delete(id: Id, context: IContext?): Boolean {
        /* ... */
    }

}

Methods are optionals, so you can implement only what you need.

We also provide default implementations for IListModelUseCase, IGetModelUseCase, ICreateModelUseCase, IUpdateModelUseCaseand IDeleteModelUseCase:

class ListMyModelUseCase(repository: MyModelRepository) :
    ListModelFromRepositoryUseCase<MyModel>(repository)
class GetMyModelUseCase(repository: MyModelRepository) :
    GetModelFromRepositoryUseCase<MyModel, Long>(repository)
class CreateMyModelUseCase(repository: MyModelRepository) :
    CreateModelFromRepositoryUseCase<MyModel, CreateMyModelPayload>(repository)
class UpdateMyModelUseCase(repository: MyModelRepository) :
    UpdateModelFromRepositoryUseCase<MyModel, Long, UpdateMyModelPayload>(repository)
class DeleteMyModelUseCase(repository: MyModelRepository) :
    DeleteModelFromRepositoryUseCase<MyModel, Long>(repository)

Suspend variants are available too: ListModelFromRepositorySuspendUseCase, GetModelFromRepositorySuspendUseCase, CreateModelFromRepositorySuspendUseCase, UpdateModelFromRepositorySuspendUseCase and DeleteModelFromRepositorySuspendUseCase.