如何使用SubSonic编写此代码?

时间:2022-01-21 19:18:32

I have some legacy code that I'm rewriting using SubSonic to help the future maintainers. For the most part it's been relatively simple, as everything makes a stored procedure call. But now I'm having some difficulty with some tightly-coupled ADO.NET code.

我有一些遗留代码,我正在使用SubSonic重写以帮助未来的维护者。在大多数情况下,它都相对简单,因为所有内容都会进行存储过程调用。但是现在我在使用一些紧密耦合的ADO.NET代码时遇到了一些困难。

The code depends on the SqlDataAdapter to decide when to call an INSERT or UPDATE stored proc, which I understand. How can I rewrite this code in a SubSonic way?

代码依赖于SqlDataAdapter来决定何时调用INSERT或UPDATE存储过程,我理解。如何以SubSonic方式重写此代码?

public void SaveInvoice(InvoiceItems invoiceItems)
{
    // extraneous code removed
    // invoiceItems is a dataset

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "InvoiceItem_INSERT";
    cmd.Connection = conn;

    updateCmd.CommandType = CommandType.StoredProcedure;
    updateCmd.CommandText = "InvoiceItem_UPDATE";
    updateCmd.Connection = conn;

    SqlCommandBuilder.DeriveParameters(cmd);
    SqlCommandBuilder.DeriveParameters(updateCmd);

    adapter.InsertCommand = cmd;
    adapter.UpdateCommand = updateCmd;

    adapter.Update(invoiceItems._InvoiceItemTable);
}

I'm new to SubSonic, so any help is appreciated. All helpful answers will be gleefully upvoted.

我是SubSonic的新手,所以对任何帮助表示赞赏。所有有用的答案都会兴高采烈地投票。

1 个解决方案

#1


There isn't going to be an exact representation of that kind of code using SubSonic because the automagic insert/update is done specifically by the SqlDataAdapter.Update() method. If there somehow is, buried somewhere within the SubSonic namespace, I would also like to know how to use it!

使用SubSonic无法准确表示这种代码,因为自动插入/更新是由SqlDataAdapter.Update()方法专门完成的。如果有某种方式,埋在SubSonic命名空间内的某个地方,我也想知道如何使用它!

If you're dealing with one table (e.g. InvoiceItems), you can do the test yourself (this uses SubSonic's automatically generated active record implementation):

如果您正在处理一个表(例如InvoiceItems),您可以自己进行测试(这使用SubSonic自动生成的活动记录实现):

int key = InvoiceItems.ID; // not sure what your primary key identifier is called
InvoiceItem item = new InvoiceItem(key);
if (item != null)
{
    SPs.InvoiceItem_UPDATE(param1, param2, etc).Execute();
}
else 
{
    SPs.InvoiceItem_INSERT(param1, param2, etc).Execute();
}

Hopefully this gets you started.

希望这能让你开始。

As a totally unrelated sidenote, don't rely on the DeepSave() method to try to save across multiple tables, because it is not implemented. I found that out the hard way...

作为一个完全不相关的旁注,不要依赖于DeepSave()方法来尝试保存多个表,因为它没有实现。我发现困难的方式......

#1


There isn't going to be an exact representation of that kind of code using SubSonic because the automagic insert/update is done specifically by the SqlDataAdapter.Update() method. If there somehow is, buried somewhere within the SubSonic namespace, I would also like to know how to use it!

使用SubSonic无法准确表示这种代码,因为自动插入/更新是由SqlDataAdapter.Update()方法专门完成的。如果有某种方式,埋在SubSonic命名空间内的某个地方,我也想知道如何使用它!

If you're dealing with one table (e.g. InvoiceItems), you can do the test yourself (this uses SubSonic's automatically generated active record implementation):

如果您正在处理一个表(例如InvoiceItems),您可以自己进行测试(这使用SubSonic自动生成的活动记录实现):

int key = InvoiceItems.ID; // not sure what your primary key identifier is called
InvoiceItem item = new InvoiceItem(key);
if (item != null)
{
    SPs.InvoiceItem_UPDATE(param1, param2, etc).Execute();
}
else 
{
    SPs.InvoiceItem_INSERT(param1, param2, etc).Execute();
}

Hopefully this gets you started.

希望这能让你开始。

As a totally unrelated sidenote, don't rely on the DeepSave() method to try to save across multiple tables, because it is not implemented. I found that out the hard way...

作为一个完全不相关的旁注,不要依赖于DeepSave()方法来尝试保存多个表,因为它没有实现。我发现困难的方式......