Skip to main content

Introduction ๐Ÿš€โ€‹

Clawject is TypeScript Dependency Injection framework that's here to make your coding life easier. Forget about injection tokens and a huge number of decorators on and in your business classes. Use typescript features like interfaces, generics, type hierarchies in a declarative and intuitive way and let Clawject do messy work for you!

Main Featuresโ€‹

  • Ahead of Time Dependency Injection based on TypeScript types, full type safety because no injection tokens are used.
  • Can be used both in Node.js and in the browser.
  • Declarative and intuitive API.
  • Fast at runtime, all dependency-resolution work is done at compile time!
  • IDEs support, all errors and warnings are shown right in your code editor window.
  • Ahead of Time circular dependencies detection with a clear cycle path, forget about runtime loops and stack overflows!
  • No need to refer to a dependency injection library in your business-oriented classes, leave them clean and framework independent!
  • Injection scopes support and ability to create your own custom scopes.
  • Supports both experimental and stable JavaScript decorators + no dependency on reflect-metadata library.
  • Minimal runtime overhead.
  • Clawject is not modifying your classes, not adding additional runtime fields, so it's safe to use it with any other library or framework.

Let's compare Clawject with other popular frameworks that implements Dependency Injection, note how class is using interfaces with generics to declare dependencies:

interface IRepository<T> { /*...*/ }
class RepositoryImpl<T> implements IRepository<T> { /*...*/ }
class PrimitivesService {
constructor(
private stringRepository: IRepository<string>,
private numberRepository: IRepository<number>,
private booleanRepository: IRepository<boolean>,
) {}
}

@ClawjectApplication
class Application {
stringRepository = Bean(RepositoryImpl<string>);
numberRepository = Bean(RepositoryImpl<number>);
booleanRepository = Bean(RepositoryImpl<boolean>);
primitivesService Bean(PrimitivesService);
}

When it comes to the more complex scenarios, like generic inside constructors - with Clawject, it's still easy and type-safe but with other frameworks, it's getting really messy and hard to maintain, see the comparison below:

interface ICache<T> {}
class CacheImpl<T> implements ICache<T> {}
interface IRepository<T> { /*...*/ }
class RepositoryImpl<T> implements IRepository<T> {
constructor(
private cache: ICache<T>,
) {}
}
class PrimitivesService {
constructor(
private stringRepository: IRepository<string>,
private numberRepository: IRepository<number>,
private booleanRepository: IRepository<boolean>,
) {}
}

@ClawjectApplication
class Application {
stringCache = Bean(CacheImpl<string>);
numberCache = Bean(CacheImpl<number>);
booleanCache = Bean(CacheImpl<boolean>);
stringRepository = Bean(RepositoryImpl<string>);
numberRepository = Bean(RepositoryImpl<number>);
booleanRepository = Bean(RepositoryImpl<boolean>);
primitivesService = Bean(PrimitivesService);
}

As you can see - Clawject requires much less boilerplate code, not pollutes business classes and provides an intuitive, declarative and really type-safe API where you can't miss a thing. Let's clawject your codebase and make it more maintainable and easy to understand!

Inspirationโ€‹

Clawject is inspired by Spring framework spring_logo and NestJS nestjs_logo