简体   繁体   English

将Dapper与Oracle用户定义类型一起使用

[英]Using Dapper with Oracle User-Defined Types

Is there a way to use Dapper with Oracle User-Defined Types returned by stored procedures? 有没有办法将Dapper与存储过程返回的Oracle用户定义类型一起使用? Dapper seems to work with stored procedures (see Using Dapper with Oracle stored procedures which return cursors ). Dapper似乎可以与存储过程一起使用 (请参阅将Dapper与返回游标的Oracle存储过程一起使用 )。

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

In my example I've added a UDT collection object as output parameter ( param_list ). 在我的示例中,我添加了一个UDT集合对象作为输出参数( param_list )。 I'm using the custom OracleDynamicParameters posted in Dapper – Micro ORM for Oracle and Microsoft .NET . 我正在使用Dapper –适用于Oracle和Microsoft .NET的Micro ORM中发布的自定义OracleDynamicParameters

The more I read ORM related stuff, the more I see Oracle UDT objects as an obstacle. 我阅读与ORM相关的内容越多,就越将Oracle UDT对象视为障碍。 In this case plain ADO.NET and generated C# entity classes seem to be the only way to go. 在这种情况下,简单的ADO.NET和生成的C#实体类似乎是唯一的方法。 Maybe automapper.org might be useful for mapping domain objects to UDT objects. 也许automapper.org对于将域对象映射到UDT对象可能有用。

Look into using OracleCustomTypeMapping attribute and the OracleObjectMapping attribute. 研究使用OracleCustomTypeMapping属性和OracleObjectMapping属性。 Can't recall where I got this idea from but... 我不记得我从哪里得到这个主意,但是...

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
    }

example usage ... 用法示例...

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 seems fussy about the schema/type/object names being in ALLCAPS Oracle似乎对ALLCAPS中的模式/类型/对象名称很挑剔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM