Nueva mejoras Y estabilidad
This commit is contained in:
63
MicroORM/OrmMetadata.cs
Normal file
63
MicroORM/OrmMetadata.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MicroORM;
|
||||
|
||||
public class OrmMetadata
|
||||
{
|
||||
public static string GetTableName<T>()
|
||||
{
|
||||
var table = typeof(T).GetCustomAttribute<TableAttribute>();
|
||||
if (table == null)
|
||||
throw new InvalidOperationException("La entidad no tiene [Table]");
|
||||
|
||||
return string.IsNullOrEmpty(table.Schema)
|
||||
? table.Name
|
||||
: $"{table.Schema}.{table.Name}";
|
||||
}
|
||||
|
||||
public static PropertyInfo GetKeyProperty<T>()
|
||||
{
|
||||
var key = typeof(T)
|
||||
.GetProperties()
|
||||
.FirstOrDefault(p => p.GetCustomAttribute<KeyAttribute>() != null);
|
||||
|
||||
if (key == null)
|
||||
throw new InvalidOperationException("La entidad no tiene [Key]");
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public static string GetColumnName(PropertyInfo prop)
|
||||
{
|
||||
var columnAttr = prop.GetCustomAttribute<ColumnAttribute>();
|
||||
return columnAttr?.Name ?? prop.Name;
|
||||
}
|
||||
|
||||
public static bool IsIdentity(PropertyInfo prop)
|
||||
{
|
||||
var key = prop.GetCustomAttribute<KeyAttribute>() != null;
|
||||
var dbGenerated = prop.GetCustomAttribute<DatabaseGeneratedAttribute>();
|
||||
|
||||
return key &&
|
||||
dbGenerated?.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity;
|
||||
}
|
||||
|
||||
public static IEnumerable<PropertyInfo> GetScaffoldProperties<T>()
|
||||
{
|
||||
var type = typeof(T);
|
||||
var key = GetKeyProperty<T>();
|
||||
|
||||
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p =>
|
||||
p.CanWrite && // tiene setter
|
||||
p.GetCustomAttribute<NotMappedAttribute>() == null &&
|
||||
(
|
||||
p.GetCustomAttribute<ColumnAttribute>() != null ||
|
||||
p == key
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user