npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

usecases-kt

v1.5.0

Published

[![License](https://img.shields.io/github/license/nathanfallet/usecases)](LICENSE) [![Issues](https://img.shields.io/github/issues/nathanfallet/usecases)]() [![Pull Requests](https://img.shields.io/github/issues-pr/nathanfallet/usecases)]() [![Code Size](

Downloads

7

Readme

usecases

License Issues Pull Requests Code Size codecov

UseCase utils for all my libs.

Installation

Add dependency to your build.gradle or pom.xml:

compile 'me.nathanfallet.usecases:usecases:1.5.0'

<dependency>
    <groupId>me.nathanfallet.usecases</groupId>
    <artifactId>usecases-jvm</artifactId>
    <version>1.5.0</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:

// IMyUseCase.kt
interface IMyUseCase : IUseCase<Input, Output>
// MyUseCase.kt
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
        // ...

        // 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)

// Koin.kt
single<IMyUseCase> { MyUseCase(get(), /*...*/) }
// Somewhere else
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

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:

// MyModel.kt
data class MyModel(
    override val id: Long,
    val property1: String,
    // ...
) : IModel<Long, CreateMyModelPayload, UpdateMyModelPayload>
// CreateMyModelPayload.kt
data class CreateMyModelPayload(
    val property1: String,
    // ...
)
// UpdateMyModelPayload.kt
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 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: 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 get(id: Id): Model? {
        /* ... */
    }

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

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

    override fun delete(id: Id): Boolean {
        /* ... */
    }

}

Then, we provide default implementations for IGetModelUseCase, ICreateModelUseCase, IUpdateModelUseCase and IDeleteModelUseCase:

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

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