推荐参数顺序
我正在开发一个遗留软件系统,我的任务是将一些旧的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接来自框架设计指南:
编辑:
如果您不想遵循指南,我会将重载分解为单独的方法,如下所示:
并且
特别是,我会说
和
Straight from The Framework Design Guidelines:
Edit:
If you don't want to follow the guideline, I would break the overloads into separate methods like so:
and
In particular, I'd say
and