推荐参数顺序

发布于 2024-10-13 21:07:18 字数 1420 浏览 3 评论 0原文

我正在开发一个遗留软件系统,我的任务是将一些旧的 COM 组件迁移到 .NET 3.5。 COM 组件最初托管在 MTS 中​​,然后托管在组件服务中。在 .NET 端口中,我们使用 ADO.NET 事务处理事务,因此方法签名发生了一些变化。

我面临的困境是参数顺序。每个方法都要求您向其传递 SqlConnection 或 SqlTransaction(取决于该方法是否更新数据库)。当然,可以使用不同的参数来调用某些方法。例如:

Keyword.Load(string description, SqlTransaction transaction)

--OR--

Keyword.Load(string description, string tag, SqlTransaction transaction)

现在,框架中提供多个重载的大多数方法都按如下方式执行:

A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2, DateTime arg3)

值得注意的是,尽管有重载,参数顺序仍然是一致的。但是,我真的想强调用户传递连接或事务的要求。通常,这些是最后指定的参数。但在我看来,放置它们的最佳位置是参数 0:

A(SqlTransaction transaction)
A(SqlTransaction transaction, int arg1)

不幸的是,这种情况发生在重载的声明中,该重载既不接受连接也不接受事务,并为您创建一个:

// These overloads create a connection, open it, and start a new transaction.
A()
A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2)
A(int arg1, string arg2, DateTime arg3)

// These overloads require that the transaction be passed in, so that the method
// can take part in it.
A(SqlTransaction transaction)
A(SqlTransaction transaction, int arg1)
A(SqlTransaction transaction, int arg1, string arg2)
A(SqlTransaction transaction, int arg1, string arg2, DateTime arg3)

如您所见,它需要更多的重载以使其正确,但对交易或连接的强调对我来说似乎更清晰。

如果你是我,你会选择什么道路?是否有设计指南规定如何处理此类场景?是否有太多的过载需要处理?

I'm working on a legacy software system, and I've been tasked with migrating some old COM components to .NET 3.5. The COM components were originally hosted in MTS, then in Component Services. In the .NET port, we're handling transactions with ADO.NET transactions, so the method signatures are changing somewhat.

The dilemma I'm facing is the parameter order. Each method requires that you pass it either a SqlConnection or a SqlTransaction (depending on whether or not the method updates the database). Some methods can, naturally, be invoked with different arguments. For example:

Keyword.Load(string description, SqlTransaction transaction)

--OR--

Keyword.Load(string description, string tag, SqlTransaction transaction)

Now, most methods in the framework that provide multiple overloads do so as follows:

A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2, DateTime arg3)

Notably, the parameter order is consistent despite the overloads. However, I'd really like to emphasize the requirement for the user to pass in a connection or a transaction. Typically, these are the last arguments specified. But it seems to me that the best place to put them is parameter 0:

A(SqlTransaction transaction)
A(SqlTransaction transaction, int arg1)

Unfortunately, where this breaks down is in the declaration of the overload that takes neither a connection nor a transaction and creates one for you:

// These overloads create a connection, open it, and start a new transaction.
A()
A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2)
A(int arg1, string arg2, DateTime arg3)

// These overloads require that the transaction be passed in, so that the method
// can take part in it.
A(SqlTransaction transaction)
A(SqlTransaction transaction, int arg1)
A(SqlTransaction transaction, int arg1, string arg2)
A(SqlTransaction transaction, int arg1, string arg2, DateTime arg3)

As you can see, it requires more overloads to get it right, but the emphasis on the transaction or connection just seems more clear to me.

If you were me, what path would you choose? Is there a design guideline that stipulates how scenarios like this should be handled? Is it too many overloads to deal with?

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

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

发布评论

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

评论(1

羁〃客ぐ 2024-10-20 21:07:19

直接来自框架设计指南:

方法参数的顺序和命名要保持一致。

编辑:

如果您不想遵循指南,我会将重载分解为单独的方法,如下所示:

A()
A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2)

并且

AWithTransaction(SqlTransaction transaction)
AWithTransaction(SqlTransaction transaction, int arg1)
AWithTransaction(SqlTransaction transaction, int arg1, string arg2)
AWithTransaction(
    SqlTransaction transaction,
    int arg1,
    string arg2,
    DateTime arg3
)

特别是,我会说

Keyword.Load(string description)
Keyword.Load(string description, string tag) 

Keyword.LoadWithTransaction(SqlTransaction transaction, string description)
Keyword.LoadWithTransaction(
    SqlTransaction transaction,
    string description,
    string tag
)

Straight from The Framework Design Guidelines:

Do be consistent in the ordering and naming of method parameters.

Edit:

If you don't want to follow the guideline, I would break the overloads into separate methods like so:

A()
A(int arg1)
A(int arg1, string arg2)
A(int arg1, string arg2)

and

AWithTransaction(SqlTransaction transaction)
AWithTransaction(SqlTransaction transaction, int arg1)
AWithTransaction(SqlTransaction transaction, int arg1, string arg2)
AWithTransaction(
    SqlTransaction transaction,
    int arg1,
    string arg2,
    DateTime arg3
)

In particular, I'd say

Keyword.Load(string description)
Keyword.Load(string description, string tag) 

and

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