Add typed procedure contract result handling

This commit is contained in:
2026-02-27 15:18:11 +01:00
parent 79cce3d1f4
commit c9a41adef3
8 changed files with 564 additions and 49 deletions

View File

@@ -5358,7 +5358,8 @@ namespace DynamORM
CallInfo info = binder.CallInfo;
// Get generic types
IList<Type> types = binder.GetGenericTypeArguments();
IList<Type> types = binder.GetGenericTypeArguments() ?? new List<Type>();
Type declaredResultType = null;
Dictionary<string, int> retParams = null;
@@ -5374,6 +5375,7 @@ namespace DynamORM
int alen = args.Length;
bool retIsAdded = false;
declaredResultType = alen == 1 ? DynamicProcedureResultBinder.GetDeclaredResultType(args[0]) : null;
if (alen > 0)
{
@@ -5539,7 +5541,13 @@ namespace DynamORM
object mainResult = null;
if (types.Count > 0)
if (types.Count == 0 && DynamicProcedureResultBinder.CanReadResults(declaredResultType))
{
using (IDataReader rdr = cmd.ExecuteReader())
using (IDataReader cache = rdr.CachedReader())
mainResult = DynamicProcedureResultBinder.ReadDeclaredResult(declaredResultType, cache);
}
else if (types.Count > 0)
{
mainResult = types[0].GetDefaultValue();
@@ -5695,9 +5703,15 @@ namespace DynamORM
else
result = res.ToDynamic();
}
else if (declaredResultType != null)
result = DynamicProcedureResultBinder.BindPayload(declaredResultType, binder.Name, mainResult, res.Where(x => x.Key != binder.Name).ToDictionary(x => x.Key, x => x.Value), mainResult != null && declaredResultType.IsInstanceOfType(mainResult) ? mainResult : null);
else
result = res.ToDynamic();
}
else if (declaredResultType != null && mainResult != null && declaredResultType.IsInstanceOfType(mainResult))
result = mainResult;
else if (declaredResultType != null)
result = DynamicProcedureResultBinder.BindPayload(declaredResultType, binder.Name, mainResult, null);
else
result = mainResult;
@@ -6835,6 +6849,22 @@ namespace DynamORM
#endregion IExtendedDisposable Members
}
/// <summary>Marks an object as an explicit stored procedure parameter contract.</summary>
public interface IProcedureParameters
{
}
/// <summary>Marks an object as a stored procedure parameter contract with a declared typed result model.</summary>
/// <typeparam name="TResult">Typed result model.</typeparam>
public interface IProcedureParameters<TResult> : IProcedureParameters
{
}
/// <summary>Allows typed procedure result models to consume multiple result sets directly.</summary>
public interface IProcedureResultReader
{
/// <summary>Reads all required result sets from the procedure reader.</summary>
/// <param name="reader">Procedure result reader, usually a cached reader.</param>
void ReadResults(IDataReader reader);
}
/// <summary>Declares metadata for object-based stored procedure parameters.</summary>
[AttributeUsage(AttributeTargets.Property)]
public class ProcedureParameterAttribute : ColumnAttribute
@@ -14482,15 +14512,10 @@ namespace DynamORM
}
internal static bool CanBind(object item)
{
if (item == null)
if (!DynamicProcedureResultBinder.IsProcedureContract(item))
return false;
Type type = item.GetType();
if (type.IsPrimitive || type.IsEnum || type == typeof(string) || type == typeof(decimal) || type == typeof(DateTime) || type == typeof(Guid) || type == typeof(TimeSpan))
return false;
return GetBindableProperties(type).Any();
return GetBindableProperties(item.GetType()).Any();
}
internal static BindingResult Bind(DynamicDatabase db, IDbCommand cmd, object item)
{
@@ -14600,6 +14625,70 @@ namespace DynamORM
return 0;
}
}
internal static class DynamicProcedureResultBinder
{
internal static bool IsProcedureContract(object item)
{
return item is IProcedureParameters;
}
internal static Type GetDeclaredResultType(object item)
{
if (item == null)
return null;
Type iface = item.GetType().GetInterfaces()
.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IProcedureParameters<>));
return iface == null ? null : iface.GetGenericArguments()[0];
}
internal static bool CanReadResults(Type resultType)
{
return resultType != null && typeof(IProcedureResultReader).IsAssignableFrom(resultType);
}
internal static object CreateDeclaredResult(Type resultType)
{
if (resultType == null)
return null;
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(resultType);
if (mapper != null)
return mapper.Creator();
return Activator.CreateInstance(resultType);
}
internal static object ReadDeclaredResult(Type resultType, IDataReader reader)
{
if (!CanReadResults(resultType))
throw new InvalidOperationException(string.Format("Type '{0}' does not implement IProcedureResultReader.", resultType == null ? "<null>" : resultType.FullName));
object instance = CreateDeclaredResult(resultType);
((IProcedureResultReader)instance).ReadResults(reader);
return instance;
}
internal static object BindPayload(Type resultType, string mainResultName, object mainResult, IDictionary<string, object> returnValues, object existing = null)
{
if (resultType == null)
return existing ?? returnValues.ToDynamic();
Dictionary<string, object> payload = new Dictionary<string, object>();
if (mainResultName != null)
payload[mainResultName] = mainResult == DBNull.Value ? null : mainResult;
if (returnValues != null)
foreach (KeyValuePair<string, object> item in returnValues)
payload[item.Key] = item.Value == DBNull.Value ? null : item.Value;
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(resultType);
if (mapper != null)
return mapper.Map(payload.ToDynamic(), existing ?? mapper.Creator());
if (existing != null)
return existing;
return payload.ToDynamic();
}
}
/// <summary>Framework detection and specific implementations.</summary>
public static class FrameworkTools
{