Ejemplo n.º 1
0
        /// <summary>
        /// 引导程序
        /// </summary>
        /// <param name="bootstraps">引导程序</param>
        /// <returns>CatLib实例</returns>
        /// <exception cref="ArgumentNullException">当引导类型为null时引发</exception>
        public void Bootstrap(params IBootstrap[] bootstraps)
        {
            Guard.Requires <ArgumentNullException>(bootstraps != null);

            if (bootstrapped)
            {
                return;
            }

            process = StartProcess.Bootstrap;

            var sorting = new SortSet <IBootstrap, int>();

            foreach (var bootstrap in bootstraps)
            {
                sorting.Add(bootstrap, GetPriority(bootstrap.GetType(), "Bootstrap"));
            }

            foreach (var bootstrap in sorting)
            {
                if (bootstrap != null)
                {
                    bootstrap.Bootstrap();
                }
            }

            process      = StartProcess.Bootstraped;
            bootstrapped = true;
            Trigger(ApplicationEvents.OnBootstraped, this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 引导程序
        /// </summary>
        /// <param name="bootstraps">引导程序</param>
        /// <returns>CatLib实例</returns>
        /// <exception cref="ArgumentNullException">当引导类型为null时引发</exception>
        public virtual void Bootstrap(params IBootstrap[] bootstraps)
        {
            Guard.Requires <ArgumentNullException>(bootstraps != null);

            if (bootstrapped || Process != StartProcess.Construct)
            {
                throw new RuntimeException("Cannot repeatedly trigger the Bootstrap()");
            }

            Process = StartProcess.Bootstrap;
            Trigger(ApplicationEvents.OnBootstrap, this);
            Process = StartProcess.Bootstrapping;

            var sorting = new SortSet <IBootstrap, int>();

            foreach (var bootstrap in bootstraps)
            {
                sorting.Add(bootstrap, GetPriority(bootstrap.GetType(), "Bootstrap"));
            }

            foreach (var bootstrap in sorting)
            {
                var allow = TriggerHalt(ApplicationEvents.Bootstrapping, bootstrap) == null;
                if (bootstrap != null && allow)
                {
                    bootstrap.Bootstrap();
                }
            }

            Process      = StartProcess.Bootstraped;
            bootstrapped = true;
            Trigger(ApplicationEvents.OnBootstraped, this);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 当查找类型无法找到时会尝试去调用开发者提供的查找类型函数
 /// </summary>
 /// <param name="finder">查找类型的回调</param>
 /// <param name="priority">查询优先级(值越小越优先)</param>
 /// <returns>当前容器实例</returns>
 public IContainer OnFindType(Func <string, Type> finder, int priority = int.MaxValue)
 {
     Guard.NotNull(finder, "finder");
     lock (syncRoot)
     {
         findType.Add(finder, priority);
     }
     return(this);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 注册服务提供者
        /// </summary>
        /// <param name="provider">注册服务提供者</param>
        /// <exception cref="RuntimeException">服务提供者被重复注册时触发</exception>
        public void Register(IServiceProvider provider)
        {
            Guard.Requires <ArgumentNullException>(provider != null);

            if (serviceProviderTypes.Contains(provider.GetType()))
            {
                throw new RuntimeException("Provider [" + provider.GetType() + "] is already register.");
            }

            provider.Register();
            serviceProviders.Add(provider, GetPriority(provider.GetType(), "Init"));
            serviceProviderTypes.Add(provider.GetType());

            if (inited)
            {
                provider.Init();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 注册服务提供者
        /// </summary>
        /// <param name="provider">注册服务提供者</param>
        /// <exception cref="RuntimeException">服务提供者被重复注册时触发</exception>
        public void Register(IServiceProvider provider)
        {
            Guard.Requires <ArgumentNullException>(provider != null);

            if (IsRegisted(provider))
            {
                throw new RuntimeException("Provider [" + provider.GetType() + "] is already register.");
            }

            provider.Register();
            serviceProviders.Add(provider, GetPriority(provider.GetType(), "Init"));
            serviceProviderTypes.Add(GetProviderBaseType(provider));

            if (inited)
            {
                Trigger(ApplicationEvents.OnIniting, provider);
                provider.Init();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 如果对象实现了增强接口那么将对象装载进对应驱动器
        /// 在装载的时候会引发IStart接口
        /// </summary>
        /// <param name="obj">对象</param>
        /// <exception cref="ArgumentNullException">当装载对象为<c>null</c>时引发</exception>
        public void Load(object obj)
        {
            Guard.Requires <ArgumentNullException>(obj != null);

            if (loadSet.Contains(obj))
            {
                throw new RuntimeException("Object [" + obj + "] is already load.");
            }

            var isLoad = false;

            if (obj is IStart)
            {
                isLoad = true;
                ((IStart)obj).Start();
            }

            if (obj is IUpdate)
            {
                isLoad = true;
                var priorities = GetPriorities(obj.GetType(), "Update");
                update.Add((IUpdate)obj, priorities);
            }

            if (obj is ILateUpdate)
            {
                isLoad = true;
                var priorities = GetPriorities(obj.GetType(), "LateUpdate");
                lateUpdate.Add((ILateUpdate)obj, priorities);
            }

            if (obj is IDestroy)
            {
                isLoad = true;
                var priorities = GetPriorities(obj.GetType(), "OnDestroy");
                destroy.Add((IDestroy)obj, priorities);
            }

            if (isLoad)
            {
                loadSet.Add(obj);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 注册服务提供者
        /// </summary>
        /// <param name="provider">注册服务提供者</param>
        /// <exception cref="RuntimeException">服务提供者被重复注册时触发</exception>
        public virtual void Register(IServiceProvider provider)
        {
            Guard.Requires <ArgumentNullException>(provider != null);

            if (IsRegisted(provider))
            {
                throw new RuntimeException("Provider [" + provider.GetType() + "] is already register.");
            }

            if (Process == StartProcess.Initing)
            {
                throw new RuntimeException("Unable to add service provider during StartProcess.Initing");
            }

            if (Process > StartProcess.Running)
            {
                throw new RuntimeException("Unable to Terminate in-process registration service provider");
            }

            var allow = TriggerHalt(ApplicationEvents.OnRegisterProvider, provider) == null;

            if (!allow)
            {
                return;
            }

            provider.Register();
            serviceProviders.Add(provider, GetPriority(provider.GetType(), "Init"));
            serviceProviderTypes.Add(GetProviderBaseType(provider));

            if (inited)
            {
                Trigger(ApplicationEvents.OnIniting, provider);
                provider.Init();
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 增加一个过滤器
 /// </summary>
 /// <param name="filter">过滤器</param>
 /// <param name="priority">优先级(值越小越优先)</param>
 /// <returns>过滤器链</returns>
 public IFilterChain <TIn> Add(Action <TIn, Action <TIn> > filter, int priority = int.MaxValue)
 {
     filterList.Add(filter, priority);
     return(this);
 }