diff --git a/Models/Helpers/ModelAttributes.cs b/Models/Helpers/ModelAttributes.cs index 79db75b..760b476 100644 --- a/Models/Helpers/ModelAttributes.cs +++ b/Models/Helpers/ModelAttributes.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -9,31 +9,40 @@ namespace Models.Helpers; /// This Attribute is used to identify which entities to expose in the API /// [AttributeUsage(AttributeTargets.Class)] -public class ExposeToApi(Type dtoIn, Type dtoOut) : Attribute +public class ExposeToApi : Attribute { - public Type DtoIn = dtoIn, DtoOut = dtoOut; + public Type DtoIn { get; } + public Type DtoOut { get; } + + public ExposeToApi(Type dtoIn, Type dtoOut) + { + DtoIn = dtoIn; + DtoOut = dtoOut; + } } public static class IncludedEntities { - public static IReadOnlyList<(Type, Type, Type)> Types; + //Types are loaded with Lazy to improve performance and resource utilization + private static readonly Lazy> _types = new Lazy>(LoadTypes); - static IncludedEntities() + public static IReadOnlyList<(Type, Type, Type)> Types => _types.Value; + + private static IReadOnlyList<(Type, Type, Type)> LoadTypes() { var assembly = typeof(IncludedEntities).GetTypeInfo().Assembly; var typeList = new List<(Type, Type, Type)>(); - + //Simplified the attribute fetching using GetCustomAttribute<>() instead of GetCustomAttributes() and checking the length foreach (var type in assembly.GetLoadableTypes()) { - var attribs = type.GetCustomAttributes(typeof(ExposeToApi), true); - if (attribs.Length > 0) + var attrib = type.GetCustomAttribute(); + if (attrib != null) { - var o = (ExposeToApi)attribs[0]; - typeList.Add((type, o.DtoIn, o.DtoOut)); + typeList.Add((type, attrib.DtoIn, attrib.DtoOut)); } } - Types = typeList; + return typeList; } public static IEnumerable GetLoadableTypes(this Assembly assembly) @@ -47,4 +56,4 @@ public static IEnumerable GetLoadableTypes(this Assembly assembly) return e.Types.Where(t => t != null); } } -} \ No newline at end of file +}