uioc icon indicating copy to clipboard operation
uioc copied to clipboard

循环依赖处理策略

Open Exodia opened this issue 11 years ago • 0 comments

构造函数注入时,有循环依赖

场景demo

ioc.create({
       A: {  
                creator: function(instanceB) { this.b = instanceB }
                args: [{  $ref :  'B' }]
       },

       B: {  
                creator: function(instanceA) { this.a = instanceA }
                args: [{  $ref :  'A' }]
       }
});

 ioc.getComponent('A', function(a){ //...  });

可能的策略

  1. 抛异常
  2. 将依赖设置为 null传入, 控制台丢警告

setter 注入时,有循环依赖

场景demo

ioc.create({
       A: {  
                creator: function() { // ... },
                properties: {  b: { $ref: 'B' } }
       },

       B: {  
                creator: function() { // ... },
                properties: {  a: { $ref: 'A' } }
       }
});

 ioc.getComponent('A', function(a){ //...  });

可能的策略

控制台发警告,同时创建好依赖实例后,调用对应 setter 传入实例即可,注入这块不会存在无限递归问题。

Exodia avatar May 19 '14 08:05 Exodia