@@ -56,6 +56,11 @@ public class Container : IContainer
5656 /// </summary>
5757 private readonly List < Action < IBindData , object > > resolving ;
5858
59+ /// <summary>
60+ /// 在服务构建修饰器之后的修饰器
61+ /// </summary>
62+ private readonly List < Action < IBindData , object > > afterResloving ;
63+
5964 /// <summary>
6065 /// 静态服务释放时的修饰器
6166 /// </summary>
@@ -146,6 +151,7 @@ public Container(int prime = 64)
146151 instancesReverse = new Dictionary < object , string > ( prime * 4 ) ;
147152 binds = new Dictionary < string , BindData > ( prime * 4 ) ;
148153 resolving = new List < Action < IBindData , object > > ( ( int ) ( prime * 0.25 ) ) ;
154+ afterResloving = new List < Action < IBindData , object > > ( ( int ) ( prime * 0.25 ) ) ;
149155 release = new List < Action < IBindData , object > > ( ( int ) ( prime * 0.25 ) ) ;
150156 extenders = new Dictionary < string , List < Func < object , IContainer , object > > > ( ( int ) ( prime * 0.25 ) ) ;
151157 resolved = new HashSet < string > ( ) ;
@@ -686,14 +692,13 @@ public object Instance(string service, object instance)
686692 {
687693 throw new LogicException ( $ "Service [{ service } ] is not Singleton(Static) Bind.") ;
688694 }
689- instance = ( ( BindData ) bindData ) . TriggerResolving ( instance ) ;
690695 }
691696 else
692697 {
693698 bindData = MakeEmptyBindData ( service ) ;
694699 }
695700
696- instance = TriggerOnResolving ( bindData , instance ) ;
701+ instance = TriggerOnResolving ( ( BindData ) bindData , instance ) ;
697702
698703 if ( instance != null
699704 && instancesReverse . TryGetValue ( instance , out string realService )
@@ -787,12 +792,7 @@ public IContainer OnFindType(Func<string, Type> finder, int priority = int.MaxVa
787792 /// <returns>当前容器实例</returns>
788793 public IContainer OnRelease ( Action < IBindData , object > closure )
789794 {
790- Guard . NotNull ( closure , nameof ( closure ) ) ;
791- lock ( syncRoot )
792- {
793- GuardFlushing ( ) ;
794- release . Add ( closure ) ;
795- }
795+ AddClosure ( closure , release ) ;
796796 return this ;
797797 }
798798
@@ -803,12 +803,18 @@ public IContainer OnRelease(Action<IBindData, object> closure)
803803 /// <returns>当前容器对象</returns>
804804 public IContainer OnResolving ( Action < IBindData , object > closure )
805805 {
806- Guard . NotNull ( closure , nameof ( closure ) ) ;
807- lock ( syncRoot )
808- {
809- GuardFlushing ( ) ;
810- resolving . Add ( closure ) ;
811- }
806+ AddClosure ( closure , resolving ) ;
807+ return this ;
808+ }
809+
810+ /// <summary>
811+ /// 解决服务时事件之后的回调
812+ /// </summary>
813+ /// <param name="closure">解决事件</param>
814+ /// <returns>服务绑定数据</returns>
815+ public IContainer OnAfterResolving ( Action < IBindData , object > closure )
816+ {
817+ AddClosure ( closure , afterResloving ) ;
812818 return this ;
813819 }
814820
@@ -838,6 +844,7 @@ public IContainer OnRebound(string service, Action<object> callback)
838844 {
839845 rebound [ service ] = list = new List < Action < object > > ( ) ;
840846 }
847+
841848 list . Add ( callback ) ;
842849 }
843850 return this ;
@@ -1700,9 +1707,23 @@ private string AliasToService(string service)
17001707 /// <param name="bindData">服务绑定数据</param>
17011708 /// <param name="instance">服务实例</param>
17021709 /// <returns>被修饰器修饰后的服务实例</returns>
1703- private object TriggerOnResolving ( IBindData bindData , object instance )
1710+ private object TriggerOnResolving ( BindData bindData , object instance )
17041711 {
1705- return Trigger ( bindData , instance , resolving ) ;
1712+ instance = bindData . TriggerResolving ( instance ) ;
1713+ instance = Trigger ( bindData , instance , resolving ) ;
1714+ return TriggerOnAfterResolving ( bindData , instance ) ;
1715+ }
1716+
1717+ /// <summary>
1718+ /// 触发全局解决修饰器之后的修饰器回调
1719+ /// </summary>
1720+ /// <param name="bindData">服务绑定数据</param>
1721+ /// <param name="instance">服务实例</param>
1722+ /// <returns>被修饰器修饰后的服务实例</returns>
1723+ private object TriggerOnAfterResolving ( BindData bindData , object instance )
1724+ {
1725+ instance = bindData . TriggerAfterResolving ( instance ) ;
1726+ return Trigger ( bindData , instance , afterResloving ) ;
17061727 }
17071728
17081729 /// <summary>
@@ -1865,7 +1886,7 @@ private object Resolve(string service, params object[] userParams)
18651886
18661887 instance = bindData . IsStatic
18671888 ? Instance ( bindData . Service , instance )
1868- : TriggerOnResolving ( bindData , bindData . TriggerResolving ( instance ) ) ;
1889+ : TriggerOnResolving ( bindData , instance ) ;
18691890
18701891 resolved . Add ( bindData . Service ) ;
18711892 return instance ;
@@ -2017,5 +2038,21 @@ private Func<ParameterInfo, object> MakeParamsMatcher(IParams[] tables)
20172038 return null ;
20182039 } ;
20192040 }
2041+
2042+ /// <summary>
2043+ /// 增加一个闭包到指定的列表
2044+ /// </summary>
2045+ /// <param name="closure">闭包</param>
2046+ /// <param name="list">指定的列表</param>
2047+ private void AddClosure ( Action < IBindData , object > closure , List < Action < IBindData , object > > list )
2048+ {
2049+ Guard . NotNull ( closure , nameof ( closure ) ) ;
2050+
2051+ lock ( syncRoot )
2052+ {
2053+ GuardFlushing ( ) ;
2054+ list . Add ( closure ) ;
2055+ }
2056+ }
20202057 }
20212058}
0 commit comments