public object GetModule(ModuleSearchKeys keys)
        {
            List <object> output = null;

            if (mModulesByType.TryGetValue(keys.Type, out output))
            {
                return(output.FirstOrDefault());
            }
            return(null);
        }
        public T GetModule <T>() where T : class
        {
            ModuleSearchKeys moduleSearchKeys = ModuleSearchKeys.Allocate <T>();
            object           module           = moduleCache.GetModule(moduleSearchKeys);

            if (module == null)
            {
                module = moduleFactory.CreateModule(moduleSearchKeys);
                moduleCache.AddModule(moduleSearchKeys, module);
            }
            moduleSearchKeys.Release2Pool();
            return(module as T);
        }
 public void AddModule(ModuleSearchKeys keys, object module)
 {
     if (mModulesByType.ContainsKey(keys.Type))
     {
         mModulesByType[keys.Type].Add(module);
     }
     else
     {
         mModulesByType.Add(keys.Type, new List <object>()
         {
             module
         });
     }
 }
        public IEnumerable <T> GetAllModules <T>() where T : class
        {
            var moduleSearchKeys = ModuleSearchKeys.Allocate <T>();
            var modules          = moduleCache.GetAllModules() as IEnumerable <object>;

            if (modules == null || !modules.Any())
            {
                modules = moduleFactory.CreateAllModules() as IEnumerable <object>;
                foreach (var module in modules)
                {
                    moduleCache.AddModule(moduleSearchKeys, module);
                }
            }
            moduleSearchKeys.Release2Pool();
            //用select对modules 里面每个元素进行转型操作
            return(modules.Select(m => m as T));
        }
        public object CreateModule(ModuleSearchKeys keys)
        {
            if (keys.Type.IsAbstract)
            {
                if (mAbstractToConcrete.ContainsKey(keys.Type))
                {
                    return(mAbstractToConcrete[keys.Type].GetConstructors().First().Invoke(null));
                }
            }
            else
            {
                if (mConcreteTypeCache.Contains(keys.Type))
                {
                    return(keys.Type.GetConstructors().First().Invoke(null));
                }
            }

            return(null);
        }
Example #6
0
        public T GetModule <T>() where T : class
        {
            // 申请对象
            var moduleSearchKeys = ModuleSearchKeys.Allocate <T>();

            var module = mCache.GetModule(moduleSearchKeys);

            if (module == null)
            {
                module = mFactory.CreateModule(moduleSearchKeys);

                mCache.AddModule(moduleSearchKeys, module);
            }

            // 回收对象
            moduleSearchKeys.Release2Pool();

            return(module as T);
        }