Refactor DbModelOptions to use indexers for lookups
Replaced GetEntity and GetColumn extension methods with indexers on DbModelOptions and EntityOptions. Updated all usages to use the new indexer syntax, improving code clarity and error handling for missing entity or column configurations.
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
using ReC.Application.Common.Exceptions;
|
||||
|
||||
namespace ReC.Application.Common.Options.DbModel;
|
||||
|
||||
public class DbModelOptions
|
||||
{
|
||||
public Dictionary<string, EntityOptions> Entities { get; set; } = [];
|
||||
|
||||
public EntityOptions this[string entityName]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Entities.TryGetValue(entityName, out var entity))
|
||||
return entity;
|
||||
|
||||
throw new DbModelConfigurationException(
|
||||
$"Entity '{entityName}' is not configured in DbModel options.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
using ReC.Application.Common.Exceptions;
|
||||
|
||||
namespace ReC.Application.Common.Options.DbModel;
|
||||
|
||||
public static class DbModelOptionsExtensions
|
||||
{
|
||||
public static EntityOptions GetEntity(this DbModelOptions options, string entityName)
|
||||
{
|
||||
if (options.Entities.TryGetValue(entityName, out var entity))
|
||||
return entity;
|
||||
|
||||
throw new DbModelConfigurationException(
|
||||
$"Entity '{entityName}' is not configured in DbModel options.");
|
||||
}
|
||||
|
||||
public static string GetColumn(this EntityOptions entity, string entityName, string propertyName)
|
||||
{
|
||||
if (entity.ColumnMappings.TryGetValue(propertyName, out var columnName))
|
||||
return columnName;
|
||||
|
||||
var viewDisplay = entity.Name ?? entityName;
|
||||
throw new DbModelConfigurationException(
|
||||
$"Column mapping for property '{propertyName}' is not configured for entity '{viewDisplay}'.");
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using ReC.Application.Common.Exceptions;
|
||||
|
||||
namespace ReC.Application.Common.Options.DbModel;
|
||||
|
||||
public class EntityOptions
|
||||
@@ -7,4 +9,17 @@ public class EntityOptions
|
||||
public string Schema { get; set; } = "dbo";
|
||||
|
||||
public Dictionary<string, string> ColumnMappings { get; set; } = [];
|
||||
|
||||
public string this[string propertyName]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ColumnMappings.TryGetValue(propertyName, out var columnName))
|
||||
return columnName;
|
||||
|
||||
var viewDisplay = Name ?? "unknown";
|
||||
throw new DbModelConfigurationException(
|
||||
$"Column mapping for property '{propertyName}' is not configured for entity '{viewDisplay}'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user