/// <summary>
        /// 加载模块服务
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns></returns>
        public virtual IServiceCollection LoadPacks(IServiceCollection services)
        {
            IOsharpPackTypeFinder packTypeFinder =
                services.GetOrAddTypeFinder<IOsharpPackTypeFinder>(assemblyFinder => new OsharpPackTypeFinder(assemblyFinder));
            Type[] packTypes = packTypeFinder.FindAll();
            _sourcePacks.Clear();
            _sourcePacks.AddRange(packTypes.Select(m => (OsharpPack)Activator.CreateInstance(m)));

            IOsharpBuilder builder = services.GetSingletonInstance<IOsharpBuilder>();
            List<OsharpPack> packs;
            if (builder.AddPacks.Any())
            {
                packs = _sourcePacks.Where(m => m.Level == PackLevel.Core)
                    .Union(_sourcePacks.Where(m => builder.AddPacks.Contains(m.GetType()))).Distinct().ToList();
                IEnumerable<Type> dependModuleTypes = packs.SelectMany(m => m.GetDependModuleTypes());
                packs = packs.Union(_sourcePacks.Where(m => dependModuleTypes.Contains(m.GetType()))).Distinct().ToList();
            }
            else
            {
                packs = _sourcePacks.ToList();
                packs.RemoveAll(m => builder.ExceptPacks.Contains(m.GetType()));
            }
            packs = packs.OrderBy(m => m.Level).ThenBy(m => m.Order).ToList();
            LoadedPacks = packs;

            foreach (OsharpPack pack in LoadedPacks)
            {
                services = pack.AddServices(services);
            }

            return services;
        }
        /// <summary>
        /// 添加核心模块
        /// </summary>
        internal static IOsharpBuilder AddCorePack(this IOsharpBuilder builder)
        {
            builder.AddPack<OsharpCorePack>()
                .AddPack<DependencyPack>()
                .AddPack<EventBusPack>();

            return builder;
        }
        /// <summary>
        /// 初始化一个<see cref="OsharpPackManager"/>类型的新实例
        /// </summary>
        public OsharpPackManager()
        {
            _builder = Singleton <IOsharpBuilder> .Instance;
            IAllAssemblyFinder allAssemblyFinder = Singleton <IAllAssemblyFinder> .Instance;

            _typeFinder  = new OsharpPackTypeFinder(allAssemblyFinder);
            _sourcePacks = new List <OsharpPack>();
            LoadedPacks  = new List <OsharpPack>();
        }
示例#4
0
        /// <summary>
        /// 加载模块服务
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns></returns>
        public virtual IServiceCollection LoadPacks(IServiceCollection services)
        {
            IOsharpPackTypeFinder packTypeFinder =
                services.GetOrAddTypeFinder <IOsharpPackTypeFinder>(assemblyFinder => new OsharpPackTypeFinder(assemblyFinder));

            Type[] packTypes = packTypeFinder.FindAll();
            _sourcePacks.Clear();
            _sourcePacks.AddRange(packTypes.Select(m => (OsharpPack)Activator.CreateInstance(m)));

            IOsharpBuilder    builder = services.GetSingletonInstance <IOsharpBuilder>();
            List <OsharpPack> packs;

            if (builder.AddPacks.Any())
            {
                packs = _sourcePacks.Where(m => m.Level == PackLevel.Core)
                        .Union(_sourcePacks.Where(m => builder.AddPacks.Contains(m.GetType()))).Distinct()
                        .OrderBy(m => m.Level).ThenBy(m => m.Order).ToList();
                List <OsharpPack> dependPacks = new List <OsharpPack>();
                foreach (OsharpPack pack in packs)
                {
                    Type[] dependPackTypes = pack.GetDependPackTypes();
                    foreach (Type dependPackType in dependPackTypes)
                    {
                        OsharpPack dependPack = _sourcePacks.Find(m => m.GetType() == dependPackType);
                        if (dependPack == null)
                        {
                            throw new OsharpException($"加载模块{pack.GetType().FullName}时无法找到依赖模块{dependPackType.FullName}");
                        }
                        dependPacks.AddIfNotExist(dependPack);
                    }
                }
                packs = packs.Union(dependPacks).Distinct().ToList();
            }
            else
            {
                packs = _sourcePacks.ToList();
                packs.RemoveAll(m => builder.ExceptPacks.Contains(m.GetType()));
            }

            // 按先层级后顺序的规则进行排序
            packs       = packs.OrderBy(m => m.Level).ThenBy(m => m.Order).ToList();
            LoadedPacks = packs;
            foreach (OsharpPack pack in LoadedPacks)
            {
                services = pack.AddServices(services);
            }

            return(services);
        }
示例#5
0
        /// <summary>
        /// 创建OSharp构建器,开始构建OSharp服务
        /// </summary>
        public static IOsharpBuilder AddOSharp(this IServiceCollection services, Action <OsharpOptions> optionAction = null)
        {
            Check.NotNull(services, nameof(services));

            //初始化所有程序集查找器
            services.GetOrAddSingletonInstance(() => new StartupLogger());

            IOsharpBuilder builder = services.GetOrAddSingletonInstance <IOsharpBuilder>(() => new OsharpBuilder(services));

            builder.AddCorePack();

            optionAction?.Invoke(builder.Options);

            return(builder);
        }
        public IActionResult Index()
        {
            List <string>  list    = new List <string>();
            IOsharpBuilder builder = _provider.GetRequiredService <IOsharpBuilder>();

            list.Add("已加载的Pack模块:");
            var packs = builder.Packs;

            list.AddRange(packs.Select(m => m.GetType().FullName));
            list.Add(NewLine);

            list.Add("自动依赖注入:");
            var singleton1 = _provider.GetRequiredService <ISingletonService1>();

            list.Add($"单例:{nameof(ISingletonService1)} - {singleton1.GetType().Name},HashCode:{singleton1.GetHashCode()}");
            var scope1 = _provider.GetRequiredService <IScopeService1>();

            list.Add($"区域1:{nameof(IScopeService1)} - {scope1.GetType().Name},HashCode:{scope1.GetHashCode()}");
            scope1 = _provider.GetRequiredService <IScopeService1>();
            list.Add($"区域2:{nameof(IScopeService1)} - {scope1.GetType().Name},HashCode:{scope1.GetHashCode()}");
            var transient1 = _provider.GetRequiredService <ITransientService1>();

            list.Add($"即时1:{nameof(ITransientService1)} - {transient1.GetType().Name},HashCode:{transient1.GetHashCode()}");
            transient1 = _provider.GetRequiredService <ITransientService1>();
            list.Add($"即时2:{nameof(ITransientService1)} - {transient1.GetType().Name},HashCode:{transient1.GetHashCode()}");
            list.Add(NewLine);

            list.Add("手动依赖注入:");
            var singleton2 = _provider.GetRequiredService <ISingletonService2>();

            list.Add($"单例:{nameof(ISingletonService2)} - {singleton2.GetType().Name},HashCode:{singleton2.GetHashCode()}");
            var scope2 = _provider.GetRequiredService <IScopeService2>();

            list.Add($"区域1:{nameof(IScopeService2)} - {scope2.GetType().Name},HashCode:{scope2.GetHashCode()}");
            scope2 = _provider.GetRequiredService <IScopeService2>();
            list.Add($"区域2:{nameof(IScopeService2)} - {scope2.GetType().Name},HashCode:{scope2.GetHashCode()}");
            var transient2 = _provider.GetRequiredService <ITransientService2>();

            list.Add($"即时1:{nameof(ITransientService2)} - {transient2.GetType().Name},HashCode:{transient2.GetHashCode()}");
            transient2 = _provider.GetRequiredService <ITransientService2>();
            list.Add($"即时2:{nameof(ITransientService2)} - {transient2.GetType().Name},HashCode:{transient2.GetHashCode()}");

            return(Content(list.ExpandAndToString(NewLine)));
        }
示例#7
0
        /// <summary>
        /// 创建OSharp构建器,开始构建OSharp服务
        /// </summary>
        public static IOsharpBuilder AddOSharp(this IServiceCollection services)
        {
            Check.NotNull(services, nameof(services));

            IConfiguration configuration = services.GetConfiguration();

            Singleton <IConfiguration> .Instance = configuration;

            //初始化所有程序集查找器
            services.TryAddSingleton <IAllAssemblyFinder>(new AppDomainAllAssemblyFinder());

            IOsharpBuilder builder = services.GetSingletonInstanceOrNull <IOsharpBuilder>() ?? new OsharpBuilder(services);

            services.TryAddSingleton <IOsharpBuilder>(builder);

            builder.AddCorePack();

            return(builder);
        }
示例#8
0
        /// <summary>
        /// 将OSharp服务,各个<see cref="OsharpPack"/>模块的服务添加到服务容器中
        /// </summary>
        public static IServiceCollection AddOSharp <TOsharpPackManager>(this IServiceCollection services, Action <IOsharpBuilder> builderAction = null)
            where TOsharpPackManager : IOsharpPackManager, new()
        {
            Check.NotNull(services, nameof(services));

            //初始化所有程序集查找器
            services.TryAddSingleton <IAllAssemblyFinder>(new AppDomainAllAssemblyFinder());

            IOsharpBuilder builder = services.GetSingletonInstanceOrNull <IOsharpBuilder>() ?? new OsharpBuilder();

            builderAction?.Invoke(builder);
            services.TryAddSingleton <IOsharpBuilder>(builder);

            TOsharpPackManager manager = new TOsharpPackManager();

            services.AddSingleton <IOsharpPackManager>(manager);
            manager.LoadPacks(services);
            return(services);
        }
示例#9
0
 /// <summary>
 /// 添加CorePack
 /// </summary>
 public static IOsharpBuilder AddCorePack(this IOsharpBuilder builder)
 {
     return(builder.AddPack <OsharpCorePack>());
 }