owja / ioc

:unicorn: lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
MIT License
287 stars 13 forks source link

Suggestion: Configurable Cache #17

Closed hbroer closed 5 years ago

hbroer commented 5 years ago

This is a suggestion of a configurable cache. It is made with extensibility in mind (Tags).

Without caching a dependency, configured without the .inSingeltonScope() option, will resolve new data on every get of the property. With caching it will only resolved once per instance.

container.bind<IMyService>(TYPE.MyService).to(MyService);

class Something {

    @inject(Type.MyService, NOCACHE)
    private notCached!: IMyService;

    @inject(Type.MyService)
    private cached!: IMyService;

}

In this example getting this.notCached will create each call a new instance of MyService which is the current behavior. On the contrary, calling this.cached will resolve only once.

The alternative of the configurable cache, a static cache.

Bundle Sizes

Question

Which implementation whould you prefer? Or keep the current state? Or any other suggestion?

Links

Issue: Cache None-Singletons Suggestion: Static Cache

codecov[bot] commented 5 years ago

Codecov Report

Merging #17 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #17   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines          48     52    +4     
  Branches        8      9    +1     
=====================================
+ Hits           48     52    +4
Impacted Files Coverage Δ
src/index.ts 100% <100%> (ø) :arrow_up:
src/ioc/inject.ts 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e3a634f...17d8de0. Read the comment docs.