Files
ReC/src/ReC.Infrastructure/Options/Shared/EntityBaseOptions.cs
TekH 07fca00344 Replace InvalidOperationException with custom config exception
Refactored DbModelOptions and EntityBaseOptions to throw DbModelConfigurationException instead of InvalidOperationException for configuration errors. Added necessary using directives for the new exception type to improve error clarity and specificity.
2025-12-10 14:54:20 +01:00

33 lines
1.1 KiB
C#

using ReC.Domain.Attributes;
using ReC.Infrastructure.Exceptions;
namespace ReC.Infrastructure.Options.Shared;
public record EntityBaseOptions()
{
public Dictionary<string, string> ColumnMappings { get; init; } = [];
public IEnumerable<string> PropertyNames => ColumnMappings.Select(col => col.Key);
public IEnumerable<string> ColumnNames => ColumnMappings.Select(col => col.Value);
public void EnsureProperties(IEnumerable<string> propertyNames)
{
var missingProperties = propertyNames.Except(PropertyNames).ToList();
if (missingProperties.Count != 0)
throw new DbModelConfigurationException($"The following properties are not configured: {string.Join(", ", missingProperties)}");
}
public void EnsureProperties(params string[] propertyNames)
=> EnsureProperties(propertyNames.AsEnumerable());
public void EnsureProperties<T>()
{
var propertyNames = typeof(T)
.GetProperties()
.Where(prop => Attribute.IsDefined(prop, typeof(MustConfiguredAttribute)))
.Select(prop => prop.Name);
EnsureProperties(propertyNames);
}
}