gpt4 book ai didi

c# - 将 Dapper 与 Oracle 用户定义类型结合使用

转载 作者:行者123 更新时间:2023-11-30 12:30:29 35 4
gpt4 key购买 nike

有没有办法将 Dapper 与存储过程返回的 Oracle 用户定义类型一起使用?Dapper 似乎可以使用存储过程(参见 Using Dapper with Oracle stored procedures which return cursors)。

var p = new OracleDynamicParameters();
p.Add("param_list", null, OracleDbType.Object, ParameterDirection.Output);
p.Add("param_reco", null, OracleDbType.Object, ParameterDirection.Output);

在我的示例中,我添加了一个 UDT 集合对象作为输出参数 (param_list)。我正在使用 Dapper – Micro ORM for Oracle and Microsoft .NET 中发布的自定义 OracleDynamicParameters .

我阅读与 ORM 相关的内容越多,我就越将 Oracle UDT 对象视为一个障碍。在这种情况下,纯 ADO.NET 和生成的 C# 实体类似乎是唯一的出路。也许automapper.org可能对将域对象映射到 UDT 对象很有用。

最佳答案

研究使用 OracleCustomTypeMapping 属性和 OracleObjectMapping 属性。不记得我从哪里得到这个想法但是......

public interface INullableOracleCustomType: INullable,  IOracleCustomType
{
}

[OracleCustomTypeMapping("<YOUR_SCHEMA_NAME>.<UDT_OBJECT_NAME>")]
public class ParameterObject : INullableOracleCustomType
{
private bool objectIsNull;

#region constructor

public ParameterObject()
{ }

public ParameterObject(string parameterName, string parameterValue)
{
this.ParameterName = parameterName;
this.ParameterValue = parameterValue;
}

#endregion

#region properties
[OracleObjectMappingAttribute("PARAMETERNAME")]
public string ParameterName { get; set; }

[OracleObjectMappingAttribute("PARAMETERVALUE")]
public string ParameterValue { get; set; }

public static ParameterObject Null
{
get
{
ParameterObject parameterObject = new ParameterObject();
parameterObject.objectIsNull = true;
return parameterObject;
}
}

#endregion

#region INullable Members

public bool IsNull
{
get { return objectIsNull; }
}

#endregion

#region IOracleCustomType
public void FromCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
// Convert from the Custom Type to Oracle Object
if (!string.IsNullOrEmpty(ParameterName))
{
OracleUdt.SetValue(con, pUdt, "PARAMETERNAME", ParameterName);
}
if (!string.IsNullOrEmpty(ParameterValue))
{
OracleUdt.SetValue(con, pUdt, "PARAMETERVALUE", ParameterValue);
}
}

public void ToCustomObject(Oracle.DataAccess.Client.OracleConnection con, IntPtr pUdt)
{
ParameterName = (string)OracleUdt.GetValue(con, pUdt, "PARAMETERNAME");
ParameterValue = (string)OracleUdt.GetValue(con, pUdt, "PARAMETERVALUE");
}

#endregion
}

示例用法...

public static OracleParameter CreateCustomTypeArrayInputParameter<T>(string name, string oracleUDTName, T value)
where T : INullableOracleCustomType
{
OracleParameter parameter = new OracleParameter();
parameter.ParameterName = name;
parameter.OracleDbType = OracleDbType.Array;
parameter.Direction = ParameterDirection.Input;
parameter.UdtTypeName = oracleUDTName;
parameter.Value = value;
return parameter;
}

Oracle 似乎对 ALLCAPS 中的模式/类型/对象名称很挑剔

关于c# - 将 Dapper 与 Oracle 用户定义类型结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15943389/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com