Пример #1
0
        public bool Inserir(T novoObjeto)
        {
            string tabela = "";

            try
            {
                tabela = typeof(T).GetCustomAttributesData().FirstOrDefault().ConstructorArguments.FirstOrDefault().Value.ToString();
            }
            catch (Exception)
            {
                return(false);
            }

            string colunaId          = "";
            string colunas           = "";
            string valoresParam      = "";
            var    valoresDictionary = new Dictionary <string, object>();

            novoObjeto.Mappings.PropertyMaps.ToList().ForEach(pm =>
            {
                if (!pm.PropertyInfo.Name.Contains("READONLY"))
                {
                    if (pm.PropertyInfo.Name != "Id")
                    {
                        if (pm.PropertyInfo.PropertyType.Namespace == "NetTopologySuite.Geometries")
                        {
                            var geometria          = pm.PropertyInfo.GetValue(novoObjeto);
                            string geometriaString = geometria == null ? null : geometria.ToString();

                            valoresDictionary.Add("@" + pm.PropertyInfo.Name, geometriaString);
                        }
                        else
                        {
                            valoresDictionary.Add("@" + pm.PropertyInfo.Name, pm.PropertyInfo.GetValue(novoObjeto));
                        }

                        colunas      += pm.ColumnName + ", ";
                        valoresParam += "@" + pm.PropertyInfo.Name + ", ";
                    }
                    else
                    {
                        colunaId = pm.ColumnName;
                    }
                }
            });

            colunas      = colunas.Substring(0, colunas.Length - 2);
            valoresParam = valoresParam.Substring(0, valoresParam.Length - 2);

            string sql        = "insert into " + tabela + " (" + colunas + ") values (" + valoresParam + ") returning " + colunaId + ";";
            var    parametros = new DynamicParameters(valoresDictionary);
            int    idInserido = 0;

            using (var connection = DapperConnection.Create())
            {
                idInserido = connection.QuerySingle <int>(sql, parametros);
            }

            novoObjeto.Id = idInserido;

            return(idInserido != 0);
        }