You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CaiQie/DB/MappingHelper.cs

57 lines
1.6 KiB
C#

3 months ago
using Chloe.Entity;
using Chloe.Infrastructure.Interception;
using Chloe.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DB
{
public class MappingHelper
{
public static IEntityTypeBuilder[] FindMapsFromAssembly(Assembly assembly)
{
var typeOf_IMap = typeof(IEntityTypeBuilder);
var mapTypes = assembly.GetTypes().Where(a => a.IsClass && !a.IsAbstract && typeOf_IMap.IsAssignableFrom(a))
.ToList();
var maps = mapTypes.Select(a => (IEntityTypeBuilder)Activator.CreateInstance(a));
return maps.ToArray();
}
}
public static class DbInfo
{
public static void Init()
{
IDbCommandInterceptor interceptor = new DbCommandInterceptor();
DbInterception.Add(interceptor);
}
public static void Init(Assembly assembly)
{
Init();
var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly);
DbConfiguration.UseTypeBuilders(findMapsFromAssembly);
}
public static void Init(List<Assembly> assemblys)
{
List<IEntityTypeBuilder> list = new List<IEntityTypeBuilder>();
foreach (var assembly in assemblys)
{
var findMapsFromAssembly = MappingHelper.FindMapsFromAssembly(assembly);
list.AddRange(findMapsFromAssembly);
}
DbConfiguration.UseTypeBuilders(list.ToArray());
}
}
}