如果 with 关键字不好(对于 C#),这是一个不错的选择吗?

发布于 2024-07-10 19:54:17 字数 1065 浏览 7 评论 0 原文

var insInvoice = new NpgsqlCommand(
    @"INSERT INTO invoice_detail(
    invoice_id,
    invoice_detail_id,
    product_id,
    qty,
    price,
    amount)
    VALUES (
    :_invoice_id,
    :_invoice_detail_id,
    :_product_id,
    :_qty,
    :_price,
    :_qty * :_price)", c);


with(var p = insInvoice.Parameters)
{
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
    p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
    p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
    p.Add("_price", NpgsqlDbType.Numeric, 0, "price");
}

kludge:

for(var p = insInvoice.Parameters; false;)
{       
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
    p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
    p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
    p.Add("_price", NpgsqlDbType.Numeric, 0, "price");      
}
var insInvoice = new NpgsqlCommand(
    @"INSERT INTO invoice_detail(
    invoice_id,
    invoice_detail_id,
    product_id,
    qty,
    price,
    amount)
    VALUES (
    :_invoice_id,
    :_invoice_detail_id,
    :_product_id,
    :_qty,
    :_price,
    :_qty * :_price)", c);


with(var p = insInvoice.Parameters)
{
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
    p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
    p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
    p.Add("_price", NpgsqlDbType.Numeric, 0, "price");
}

kludge:

for(var p = insInvoice.Parameters; false;)
{       
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
    p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
    p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
    p.Add("_price", NpgsqlDbType.Numeric, 0, "price");      
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-07-17 19:54:17

由于您已经获得了具有正确形式的“Add”方法,因此您可以使用集合初始值设定项作为参数:

var insInvoice = new NpgsqlCommand(sql)
{
    Parameters = 
    {
        { "_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id" },
        { "_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id" },
        { "_qty", NpgsqlDbType.Integer, 0, "qty" },
        { "_price", NpgsqlDbType.Numeric, 0, "price" }
    }
};

有关对象和集合初始值设定项的更多信息,您可以下载 C# 深入了解,免费。

As you've got an "Add" method with the right form, you can use a collection initialiser for the parameters:

var insInvoice = new NpgsqlCommand(sql)
{
    Parameters = 
    {
        { "_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id" },
        { "_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id" },
        { "_qty", NpgsqlDbType.Integer, 0, "qty" },
        { "_price", NpgsqlDbType.Numeric, 0, "price" }
    }
};

For more on object and collection initializers, you can download chapter 8 of C# in Depth for free.

薄凉少年不暖心 2024-07-17 19:54:17

确定“p”变量的范围有什么意义?

在这种情况下,只需创建一个带有花括号的作用域并一起删除 with 语句,将“p”的变量声明移动到新作用域中。 这样,“p”变量仅在花括号的范围内可用。 您今天就可以做到这一点,而不需要任何语法糖。 我不确定这是否有助于提高可读性,只需将大括号全部删除并仅使用变量即可。

//with(var p = insInvoice.Parameters)
{
  var p = insInvoice.Parameters;
  p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
  p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
  p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
  p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
  p.Add("_price", NpgsqlDbType.Numeric, 0, "price");
}

What's the point of it, to scope the "p" variable?

In that case just create a scope with curly braces and remove the with-statement all together, move the variable declaration of "p" into the new scope. This way the "p" variable is only available within the scope of the curly braces. This you can do today without needing any syntactic sugar. I'm not sure this helps in readability though, just remove the braces all together and go with just the variable.

//with(var p = insInvoice.Parameters)
{
  var p = insInvoice.Parameters;
  p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
  p.Add("_invoice_detail_id", NpgsqlDbType.Uuid, 0, "invoice_detail_id");
  p.Add("_product_id", NpgsqlDbType.Uuid, 0, "product_id");
  p.Add("_qty", NpgsqlDbType.Integer, 0, "qty");
  p.Add("_price", NpgsqlDbType.Numeric, 0, "price");
}
森末i 2024-07-17 19:54:17

我做过类似的事情:

var insInvoice = new NpgsqlCommand(...);
insInvoice.Parameters.With(p => {
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    ...
});

I have done something similar:

var insInvoice = new NpgsqlCommand(...);
insInvoice.Parameters.With(p => {
    p.Add("_invoice_id", NpgsqlDbType.Uuid, 0, "invoice_id");
    ...
});
深巷少女 2024-07-17 19:54:17

如果您真的有兴趣通过消除代码中的噪音和“仪式”来将代码简化为更具可读性,那么在您的示例中还有更大的鱼要煎。

在这种情况下,使用扩展方法、一些静态只读元数据、参数数组等的组合,您应该能够为该插入语句自动生成 SQL,而不必手动编码。 为什么任何给定的插入看起来都比这更复杂:

db.Insert(Invoices.Table,
      new Value(Invoices.ID, "{something-something}"),
      new Value(Invoices.Quantity, 52),
      /*... and so on*/);

不需要手动编写大的 SQL 插入。 根据列名称自动选择参数名称。 名称类型和尺寸信息全部绑定在 Invoices 的静态成员中,因此您不会弄错。 在调用时相邻指定的名称和值。 这一切都可以是你的!

或者,以某种简洁的 XML 结构声明数据库的结构,并编写一个工具,从该 XML 生成两件事:1. 用于设置数据库的 DDL,2. 每个表及其列的 C# 元数据。 现在,要扩展数据库结构,只需编辑 XML 并重新运行该工具,它就可以让您从代码中即时轻松地访问架构。

在您不知不觉中,您已经拥有了自己的廉价且令人愉快的 Linq to SQL 预算版本!

(显然这已经偏离主题了,但是为什么不在今年圣诞节用一种简单的方式来编写插入语句呢?)

If you're really interested in reducing your code down to something more readable by removing the noise and "ceremony" from it, there are bigger fish to fry in your example.

In this case, using a combination of extension methods, some static readonly metadata, params arrays, and so on, you should be able to auto-generate the SQL for that insert statement rather than having to code it by hand. Why should any given insert look more complex than this:

db.Insert(Invoices.Table,
      new Value(Invoices.ID, "{something-something}"),
      new Value(Invoices.Quantity, 52),
      /*... and so on*/);

No need to write a big SQL insert by hand. Parameter names chosen automatically based on column names. Name type and size information all bound together in those static members of Invoices so you can't get them wrong. Names and values specified next to each other at the point of the call. All this could be yours!

Optionally, declare the structure of your database in some neat XML structure, and write a tool which generates two things from that XML: 1. the DDL to set up the database and 2. the C# metadata for every table and its columns. Now to extend the DB structure, just edit the XML and re-run the tool, and it gives you instant easy access to the schema from your code.

Before you know it, you've got your own cheap-and-cheerful budget version of Linq to SQL!

(Obviously this has strayed way off-topic, but why not treat yourself to an easy way of writing insert statements this Christmas?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文