Example #1
0
        internal virtual void SetupCompletedInternal(IDictionary items)
        {
            var ctx = new ObjectServiceContext(this, items)
            {
                GlobalObjectNamespace = this.globalObjectNamespace,
                CoreAttributes        = this.CoreAttributes
            };

            this.OnSetupCompleted(ctx);
        }
Example #2
0
        internal virtual void InitCompletedInternal(IDictionary items)
        {
            foreach (var container in this.globalObjectNamespace.ObjectAliases.Values)
            {
                container.InitCompleted();
            }
            var ctx = new ObjectServiceContext(this, items)
            {
                GlobalObjectNamespace = this.globalObjectNamespace,
                CoreAttributes        = this.CoreAttributes
            };

            this.OnInitCompleted(ctx);
        }
Example #3
0
        protected void OnInit(IConfigSetting configSetting, IDictionary items)
        {
            this.ConfigSetting = configSetting;

            //定义全局容器,所有的对象/类型均会存在此容器中
            this.globalObjectNamespace = new ObjectNamespace(GlobalObjectNamespaceName);

            //把自己IObjectService添加到全局容器中
            var objectServiceType     = typeof(IObjectService);
            var objectServiceLifetime = AddSingletonObjectToObjectNamespace(null, objectServiceType, this, this.globalObjectNamespace);

            //从程序集扫描定义了CoreAttribute的类型,缓存以供后续处理
            //Key为CoreAttribute派生类型,Value为CoreAttribute派生类型实例列表
            var attributes = this.CoreAttributes = new Dictionary <Type, IList <CoreAttribute> >();

            ScanCoreAttributes(attributes);

            //处理RemoveAttribute
            var removeAttributes = TypeHelper.GetAttributeFromAssembly <RemoveAttribute>(null);

            if (removeAttributes != null && removeAttributes.Length > 0)
            {
                foreach (var removeAttribute in removeAttributes)
                {
                    attributes.TryGetValue(removeAttribute.AttributeType, out var list);
                    if (list != null && list.Count > 0)
                    {
                        var ilist = (List <CoreAttribute>)list;
                        if (!string.IsNullOrEmpty(removeAttribute.Name))
                        {
                            ilist.RemoveAll(x => x.Name == removeAttribute.Name || x?.OwnerType.FullName == removeAttribute.Name);
                        }
                        if (removeAttribute.OwnerType != null)
                        {
                            ilist.RemoveAll(x => x.OwnerType == removeAttribute.OwnerType);
                        }
                    }
                }
            }

            //创建默认的对象创建器
            var objectBuilder = new ObjectBuilderInternal(this);

            this.ObjectBuilder = objectBuilder;
            //把对象创建器添加到全局容器中
            AddSingletonObjectToObjectNamespace(null, typeof(IObjectBuilder), objectBuilder, this.globalObjectNamespace);

            //创建初始化时的上下文
            items = items ?? new HybridDictionary();
            var ctx = new ObjectServiceContext(this, items)
            {
                GlobalObjectNamespace = this.globalObjectNamespace,
                CoreAttributes        = attributes,
                ConfigSetting         = configSetting
            };

            //增加对异常的处理
            AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
                this.OnError(ctx, e.ExceptionObject as Exception);
                if (e.IsTerminating)
                {
                    this.OnDisposing(ctx);
                }
            };

            //增加对卸载的处理
            AppDomain.CurrentDomain.ProcessExit += (sender, e) => {
                this.OnDisposing(ctx);
            };

            //从缓存中处理ObjectService的扩展器
            ExtenderAttributesHandle(ctx);
            //构建IObjectBuilder对象
            ObjectBuilderAttributeHandle(ctx);
            //发出即将初始化的消息
            this.OnPreInit(ctx);
            //从缓存中构建类型别名
            TypeAliaseAttributesHandle(ctx);
            //从缓存中构建对象容器内容
            ObjectAttributesHandle(ctx);

            //从文件配置里构建内容
            var setting   = this.ConfigSetting.ToSetting <ObjectServiceSetting>(ConfigHelper.RootSettingName);
            var container = setting?.Container;

            if (container != null)
            {
                //初始化类型别名
                InitTypeAliases(this.globalObjectNamespace, container);
                //初始化常量
                this.InitConstAliases(this.globalObjectNamespace, container);
                //初始化对象
                this.InitObjectAliases(this.globalObjectNamespace, container, x => {
                    if (objectServiceType.IsAssignableFrom(x))                      //保证IObjectService实例唯一
                    {
                        return(objectServiceLifetime);
                    }
                    return(null);
                });
            }

            //处理CoreAttribute
            CoreAttributeHandle(ctx);
        }