Cyclic Dependencies

how to tackle a cyclic dependencies.

Don't do cyclic dependencies.

In some (rare) case you can need to use cyclic dependencies. The library allows you to specify both: factory- and init- function in factories object.

See example:

import diContainer from 'true-di';

type Node = {
  child: Node;
  parent: Node;
  name: string;
}

type Container = {
  parentItem: Node;
  childItem: Node;
}

const createNode = (name: string): Node => ({ child: null, parent: null, name });

const container = diContainer<Container>({
  parentItem: [
    () => createNode('Parent'),
    (instance, { childItem }) => {
      instance.child = childItem
    },
  ],
  childItem: [
    () => createNode('Child' cxx),
    (instance { parentItem }) => {
      instance.parent = parentItem;
    },
  ],
});

Simillary to assigning instance property you can use set* method of the instance:

To write property-assignment code in more declarative way true-di ships: assignProps utility function, and we can rewrite basic example with the one:

Last updated

Was this helpful?