53 lines
2.1 KiB
C#
53 lines
2.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Prism.Ioc;
|
|
using RIZO_Application.Infrastructure.CustomAttribute;
|
|
|
|
namespace RIZO_Application.Core.Infrastructure.CustomExtensions
|
|
{
|
|
|
|
public static class ContainerRegistryExtensions
|
|
{
|
|
public static void AutoRegisterServices(this IContainerRegistry containerRegistry, Assembly assembly)
|
|
{
|
|
var types = assembly.GetTypes()
|
|
.Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute<AppServiceAttribute>() != null);
|
|
|
|
foreach (var type in types)
|
|
{
|
|
var attribute = type.GetCustomAttribute<AppServiceAttribute>();
|
|
var serviceType = attribute.ServiceType ?? type; // 默认注册为自身
|
|
|
|
switch (attribute.Lifetime)
|
|
{
|
|
case RIZO_Application.Infrastructure.CustomAttribute.ServiceLifetime.Singleton:
|
|
containerRegistry.RegisterSingleton(serviceType, type);
|
|
break;
|
|
case RIZO_Application.Infrastructure.CustomAttribute.ServiceLifetime.Scoped:
|
|
containerRegistry.RegisterScoped(serviceType, type);
|
|
break;
|
|
default: // Transient
|
|
containerRegistry.Register(serviceType, type);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void AutoRegisterViews(this IContainerRegistry containerRegistry, Assembly assembly)
|
|
{
|
|
var viewTypes = assembly.GetTypes()
|
|
.Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute<AutoRegisterViewAttribute>() != null);
|
|
|
|
foreach (var viewType in viewTypes)
|
|
{
|
|
var attribute = viewType.GetCustomAttribute<AutoRegisterViewAttribute>();
|
|
string viewName = attribute?.ViewName ?? viewType.Name; // 默认使用类名
|
|
|
|
containerRegistry.RegisterForNavigation(viewType, viewName);
|
|
}
|
|
}
|
|
}
|
|
}
|