Ejemplo n.º 1
0
        public static void Register(this IRegisterableLake lake, Type t)
        {
            var attr = t.GetCustomAttribute <ComponentAttribute>();

            if (attr.Id != null)
            {
                if (attr.SingletonMode)
                {
                    lake.RegisterSingleton(attr.Id, () => new ObjectBuilder(t, lake).Build());
                }
                else
                {
                    lake.Register(attr.Id, () => new ObjectBuilder(t, lake).Build());
                }
            }
            else
            {
                if (attr.SingletonMode)
                {
                    lake.RegisterSingleton(attr.Type, () => new ObjectBuilder(t, lake).Build());
                }
                else
                {
                    lake.Register(attr.Type, () => new ObjectBuilder(t, lake).Build());
                }
            }
        }
Ejemplo n.º 2
0
#pragma warning restore CS8618 // 不可为 null 的字段未初始化。请考虑声明为可以为 null。

        /// <summary>
        /// 初始化整个框架
        /// </summary>
        /// <param name="baseAPI"></param>
        public static void Initialize(IBaseApi baseAPI)
        {
            Lake = baseAPI.GlobalLake;
            RegisterComponent(baseAPI);
            //加载拓展模块
            Lake.Get <ILibsManager>().Initialize();
        }
Ejemplo n.º 3
0
 private static void RegisterBase(this IRegisterableLake lake, Type target, ComponentFactory factory)
 {
     if (target is null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     lake.RegisterBase(GenerateIdByType(target), factory);
 }
Ejemplo n.º 4
0
        public ClassComponentsLoader(string prefix, IRegisterableLake target, params Assembly[] assemblies)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                throw new ArgumentException("message", nameof(prefix));
            }

            if (target is null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            this.prefix     = prefix;
            this.target     = target;
            this.assemblies = assemblies?.Any() == true ? assemblies : AppDomain.CurrentDomain.GetAssemblies();
        }
Ejemplo n.º 5
0
        private static void RegisterBase(this IRegisterableLake lake, string id, ComponentFactory factory)
        {
            if (lake is null)
            {
                throw new ArgumentNullException(nameof(lake));
            }

            if (id is null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (factory is null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            lake.RegisterComponent(id, factory);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 向一个湖注入一个已经被标记为组件的类
        /// </summary>
        /// <exception cref="ArgumentNullException">参数为空</exception>
        /// <exception cref="InvalidOperationException">传入的类型不是组件</exception>
        /// <param name="lake"></param>
        /// <param name="componentType"></param>
        public static void Register(this IRegisterableLake lake, Type componentType)
        {
            if (lake is null)
            {
                throw new ArgumentNullException(nameof(lake));
            }

            if (componentType is null)
            {
                throw new ArgumentNullException(nameof(componentType));
            }

            var componentAttribute = componentType.GetCustomAttribute <ComponentAttribute>();

            if (componentAttribute == null)
            {
                throw new InvalidOperationException($"{componentType.FullName} is not component");
            }
            if (componentAttribute.Id != null)
            {
                if (componentAttribute.SingletonMode)
                {
                    lake.RegisterSingleton(componentAttribute.Id, () => new ObjectBuilder(componentType, lake).Build());
                }
                else
                {
                    lake.Register(componentAttribute.Id, () => new ObjectBuilder(componentType, lake).Build());
                }
            }
            else
            {
                if (componentAttribute.SingletonMode)
                {
                    lake.RegisterSingleton(componentAttribute.Type, () => new ObjectBuilder(componentType, lake).Build());
                }
                else
                {
                    lake.Register(componentAttribute.Type, () => new ObjectBuilder(componentType, lake).Build());
                }
            }
        }
Ejemplo n.º 7
0
        private static void RegisterSingletonBase(this IRegisterableLake lake, string id, ComponentFactory factory)
        {
            if (lake is null)
            {
                throw new ArgumentNullException(nameof(lake));
            }

            if (id is null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (factory is null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            var lazy = new Lazy <object?>(() => factory());

            lake.RegisterComponent(id, () => lazy.Value);
        }
Ejemplo n.º 8
0
 public static IRegisterableLake RegisterSingleton <T>(this IRegisterableLake lake, ComponentFactory factory)
 {
     RegisterSingletonBase(lake, typeof(T), factory);
     return(lake);
 }
Ejemplo n.º 9
0
 public static IRegisterableLake Register <T, TImpl>(this IRegisterableLake lake) where TImpl : T
 {
     RegisterBase(lake, typeof(T), GetObjectBuilderOf(lake, typeof(TImpl)));
     return(lake);
 }
Ejemplo n.º 10
0
 public static IRegisterableLake Register(this IRegisterableLake lake, Type target, Type impl)
 {
     RegisterBase(lake, target, GetObjectBuilderOf(lake, impl));
     return(lake);
 }
Ejemplo n.º 11
0
 public static IRegisterableLake Register <T>(this IRegisterableLake lake, Type impl)
 {
     RegisterBase(lake, typeof(T), GetObjectBuilderOf(lake, impl));
     return(lake);
 }
Ejemplo n.º 12
0
 public static IRegisterableLake Register <T>(this IRegisterableLake lake, ComponentFactory factory)
 {
     RegisterBase(lake, typeof(T), () => factory());
     return(lake);
 }
Ejemplo n.º 13
0
 private static void RegisterSingletonBase(this IRegisterableLake lake, Type target, ComponentFactory factory)
 {
     lake.RegisterSingletonBase(GenerateIdByType(target), factory);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// 加载
 /// </summary>
 /// <param name="baseAPI"></param>
 public static void Load(IBaseApi baseAPI)
 {
     Lake = baseAPI.GlobalLake;
     RegisterComponent(baseAPI);
 }
Ejemplo n.º 15
0
 public static IRegisterableLake Register(this IRegisterableLake lake, string id, ComponentFactory factory)
 {
     lake.RegisterBase(id, factory);
     return(lake);
 }
Ejemplo n.º 16
0
 public static IRegisterableLake RegisterSingleton(this IRegisterableLake lake, string id, object value)
 {
     lake.RegisterSingletonBase(id, () => value);
     return(lake);
 }
Ejemplo n.º 17
0
 public static IRegisterableLake RegisterSingleton <T>(this IRegisterableLake lake, T value)
 {
     RegisterSingletonBase(lake, typeof(T), () => value);
     return(lake);
 }
Ejemplo n.º 18
0
 public ComponentFactoryReader(IRegisterableLake registerableLake, Type factoryType)
 {
     this.registerableLake = registerableLake ?? throw new ArgumentNullException(nameof(registerableLake));
     this.factoryType      = factoryType ?? throw new ArgumentNullException(nameof(factoryType));
 }
Ejemplo n.º 19
0
 public static IRegisterableLake Register(this IRegisterableLake lake, Type t, ComponentFactory factory)
 {
     RegisterBase(lake, t, () => factory());
     return(lake);
 }
Ejemplo n.º 20
0
 public static IRegisterableLake RegisterSingleton <T>(this IRegisterableLake lake, T value)
 {
     RegisterSingletonBase(lake, typeof(T), () => value ?? throw new NullReferenceException("Should not be NULL"));
     return(lake);
 }