98 lines
3.6 KiB
C#
98 lines
3.6 KiB
C#
using CloudBuilder.Core.Application.Builders;
|
||
using CloudBuilder.Core.Application.Packs;
|
||
using CloudBuilder.Core.DatabaseAccessor;
|
||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||
using CloudBuilder.Core.DependencyInjection.EFCore;
|
||
using CloudBuilder.Core.DependencyInjection.Request;
|
||
using CloudBuilder.Core.Extensions;
|
||
using CloudBuilder.Core.Service;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace CloudBuilder.Core.DependencyInjection.Form
|
||
{
|
||
[PackLevel(40)]
|
||
public class FormPack : CloudBuilderPack
|
||
{
|
||
/// <summary>
|
||
/// 获取 模块级别
|
||
/// </summary>
|
||
public override PackLevel Level => PackLevel.Business;
|
||
|
||
/// <summary>
|
||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||
/// </summary>
|
||
public override int Order => 2;
|
||
|
||
/// <summary>
|
||
/// 将窗体和用户控件添加到依赖注入服务容器中
|
||
/// </summary>
|
||
/// <param name="services">依赖注入服务容器</param>
|
||
/// <returns></returns>
|
||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||
{
|
||
//注入窗体
|
||
var baseType = typeof(IForm);
|
||
|
||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||
|
||
var types = allAssemblies
|
||
.SelectMany(a => a.DefinedTypes)
|
||
.Select(type => type.AsType())
|
||
.Where(type => type != baseType && type.BaseType?.Name != "AbstractPage" && type.HasImplementedRawGeneric(baseType))
|
||
.Where(x => x.IsClass).ToList();
|
||
|
||
|
||
foreach (var implementType in types)
|
||
{
|
||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||
}
|
||
|
||
//注入Page
|
||
baseType = typeof(IForm);
|
||
|
||
allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||
|
||
types = allAssemblies
|
||
.SelectMany(a => a.DefinedTypes)
|
||
.Select(type => type.AsType())
|
||
.Where(type => type != baseType && type.BaseType?.Name == "AbstractPage" && type.HasImplementedRawGeneric(baseType))
|
||
.Where(x => x.IsClass).ToList();
|
||
|
||
|
||
foreach (var implementType in types)
|
||
{
|
||
//主界面窗口及弹出的窗口,要使用Transient注入,每次打开都要创建新的实例
|
||
//注意要清空之前的自定义属性。BindingContextAttributeProvider
|
||
services.TryAddKeyedTransient(implementType.BaseType, implementType.Name, implementType);
|
||
//services.TryAddKeyedScoped(implementType.BaseType, implementType.Name, implementType);
|
||
}
|
||
|
||
//注入用户控件
|
||
baseType = typeof(IUserControl);
|
||
types = allAssemblies
|
||
.SelectMany(a => a.DefinedTypes)
|
||
.Select(type => type.AsType())
|
||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||
.Where(x => x.IsClass).ToList();
|
||
|
||
foreach (var implementType in types)
|
||
{
|
||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||
}
|
||
|
||
//services.TryAddSingleton<ApplicationServiceContext>();
|
||
services.TryAddScoped<ApplicationServiceContext>();
|
||
|
||
return services;
|
||
}
|
||
|
||
|
||
}
|
||
} |