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

@@ -45,15 +45,10 @@ namespace DynamORM.Helpers
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)

View File

@@ -0,0 +1,106 @@
/*
* DynamORM - Dynamic Object-Relational Mapping library.
* Copyright (c) 2012-2026, Grzegorz Russek (grzegorz.russek@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DynamORM.Mapper;
namespace DynamORM.Helpers
{
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();
}
}
}