Skip to content

ArchMC-Development/flavor

Repository files navigation

flavor

A light-weight kotlin dependency injection & lifecycle management framework.

  • Flavor is supposed to be an easy-to-use alternative to guice.
    • Flavor also incorperates several design elements from HK2, a DI framework.
  • We use kotlin-exclusive features such as reified types & inline functions heavily.
    • Due to this, flavor is only compatible with kotlin-exclusive projects.
      • I do not plan on adding support for other JVM languages.
  • Hate my code? Despise annotations? Check out depenject by string.
    • Depenject takes a delegate-based approach, rather than annotation-based. This is achieved by using Kotlin's ReadWriteProperty.
      • Unfortunately, the delegate based approach makes it unusable in languages other than kotlin.

Features:

  • Services:
    • Searches all singletons (kotlin objects) within a specified package for objects marked with @Service.
    • Control lifecycle through the @Configure and @Close annotations.
  • DI:
    • Allows for simple yet effective dependency injection in both objects & classes.
    • Similar to almost every other DI framework, fields are injected eagerly.
      • You can create a simple work-around to this by injecting a lazy delegate.
  • Listeners:
    • Allows for a simple yet effective method to look for methods with specific annotations on startup.

Usage:

Creating a Flavor instance:

val flavor = Flavor.create<FlavorTest>(
    // This argument is optional
    FlavorOptions(logger = yourLogger)
)

Binding a class to an instance:

flavor.bind<SomeObject>()
    .annotated<Named> {
        it.value == "SomethingString"
    }
    .to(someObjectInstance)

Instantiating an injected class (with optional parameters):

flavor.injected<SomeObjectInjected>(
    // These constructor parameters are not required.
    constructorParamOne, constructorParamTwo
)

An example of a Flavor service:

@Service
// @IgnoreAutoScan - if you do not want Flavor to 
// automatically register this service
object SomeService
{
    @Inject @Named("SomethingString")
    lateinit var something: String
    
    @Configure
    fun configure()
    {
        // this method is invoked once all 
        // fields have been injected!
    }
    
    @Close
    fun close()
    {
        // this method is invoked on your 
        // platform's shutdown!
    }
}

If you want to manually register/inject into a service/class:

flavor.inject(instance)

Your Flavor instance can be started and stopped using:

flavor.startup()
flavor.close()
  • NOTE: Flavor automatically iterates through each class in the base package of the registration class, registers any available service, and injects all fields which need injection.
gg.scala.flavor.FlavorTest - registration class
gg.scala.flavor - package which flavor will scan

⚠️ There is currently no way to disable automatic scanning.

Compilation:

If you would like compile flavor yourself, use:

  • Flavor will automatically install itself to your local maven repository.
./gradlew build

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages