ComponentModel construction contributors

实现IContributeComponentConstruction接口 

意如其名,组件创建后它将完成组件最后状态的构建

不建议在construction contributor意外的地方修改ComponentModel,construction contributor构建好组建后认为他是只读的,修改会带来同步和难以跟踪的问题。

 

自定义Contributor

public class RequireLoggerProperties : IContributeComponentModelConstruction
{
    public void ProcessModel(IKernel kernel, ComponentModel model)
    {
        model.Properties
        .Where(p => p.Dependency.TargetType == typeof(ILogger))
        .All(p => p.Dependency.IsOptional = false);
    }
}

container.Kernel.ComponentModelBuilder.AddContributor(new RequireLoggerProperties());  //将自定义的Contibutor添加到容器。

上面的自定义Constructor循环组件的依赖查找类型为ILogger的依赖,设置为必须的。

 

posted @ 2019-08-08 16:40  vvf  阅读(205)  评论(0)    收藏  举报