Injector框架理解(七)

上一节我们学习了didi依赖注入框架如何创建子加载器,以及如何加载子模块,今天我们继续使用例子来说明相关的内容:

我们首先看看如下的例子:

const {Injector} = require('didi');
const injector = new Injector([
    {
      __exports__: [ 'foo' ],
      'foo': [
        'factory',
        function(bar) {
          return {
            bar: bar
          };
        }
      ],
       'bar': [ 'value', 'private-bar' ]
    }
]);

const firstChild = injector.createChild([], [ 'foo' ]);
const secondChild = injector.createChild([], [ 'foo' ]);
const fooFromFirstChild = firstChild.get('foo');
const fooFromSecondChild = secondChild.get('foo');
console.log('fooFromFirstChild:',fooFromFirstChild);
console.log('fooFromSecondChild :',fooFromSecondChild);
console.log(fooFromFirstChild === fooFromSecondChild );
console.log('fooFromFirstChild.bar :',fooFromFirstChild.bar);
console.log('fooFromSecondChild.bar :',fooFromSecondChild.bar);
console.log(fooFromFirstChild.bar === fooFromSecondChild.bar );

我们先看看输出是什么样子的:

从以上输出可以看出,子加载器加载的组件是属于不同的组件,但是这组件的依赖项来至于父模块的bar组件却是同一个,也就是说,子加载器在创建的时候会初始化一个foo组件,但是它的依赖项是共享的。这种使用方式为我们提供了一个很好的组织代码的方式。

下面我们继续看另外一种情况,这是关于加载器关于加载模块功能的,我们先看看代码:

const {Injector} = require('didi');
const { expect } = require('chai');
const otherModule = /** @type ModuleDeclaration */ ({
  'bar': [ 'value', 'bar-from-other-module' ]
});

const injector = new Injector([
  {
	__exports__: [ 'foo' ],
	__modules__: [ otherModule ],
	'foo': [
	  'factory',
	  function(bar) {
		return {
		  bar: bar
		};
	  }
	]
  }
]);
const foo = injector.get('foo');
expect(foo).to.exist;
expect(foo.bar).to.equal('bar-from-other-module');

expect(function() {
     injector.get('bar');
}).to.throw('No provider for "bar"! (Resolving: bar)');
console.log('foo:',foo)

输出如下:

在这里,我尝试引入了一个测试框架叫做chai,我们可以看出通过__modules__属性引入的其他模块,只能作为在当前加载器的组件的依赖项出现,如果直接获取依赖项的组件,那么会抛出一个错误;这里自然引出了一个问题,怎么样才能让这个依赖项的组件也可以使用呢?

我们看下面的代码:

const {Injector} = require('didi');
const { expect } = require('chai');
const otherModule = /** @type ModuleDeclaration */ ({
  'bar': [ 'value', {} ]
});

const injector = new Injector([
  otherModule,
  {
	__exports__: [ 'foo' ],
	__modules__: [ otherModule ],
	'foo': [
	  'factory',
	  function(bar) {
		return {
		  bar: bar
		};
	  }
	]
  }
]);
const foo = injector.get('foo');

expect(foo).to.exist;
expect(foo.bar).to.equal(injector.get('bar'));
console.log('foo:',foo);

我们看输出如下:

和前一个例子相比,其实就是将依赖项的模块的组件作为另一个模块使用同一个依赖加载器来进行加载就可以了。注意,前面的例子都是使用控制台来将日志信息打出来,从而说明代码的使用情况,今天尝试使用一个测试框架来说明,感觉效果还不错哈。

我们继续看下一个例子:

const {Injector} = require('didi');
const { expect } = require('chai');
const loaded = [];

const injector = new Injector([
{
  __exports__: [ 'foo' ],
  __modules__: [
	{
	  __init__: [ () => loaded.push('nested') ],
	  bar: [ 'value', 10 ]
	}
  ],
  __init__: [ (bar) => loaded.push('module' + bar) ],
  foo: [
	'factory',
	function(bar) {
	  return bar;
	}
  ]
}
]);

// when
injector.init();

// then
expect(loaded).to.eql([
'nested',
'module10'
]);

expect(function() {
injector.get('bar');
}).to.throw(/No provider for "bar"/);
console.log('loaded:',loaded);

输出如下:

从以上的代码输出结果可以看出,__init__的组件在调用injector.init()的方法可以初始化相关的组件,在这个例子中就是给loaded参数赋值。

 

 

版权声明:著作权归作者所有。

thumb_up 0 | star_outline 0 | textsms 0