English 中文(简体)
建议参数命令
原标题:Recommended Parameter Order

I m从事遗产软件系统的工作,我的任务是将一些旧的COM部件迁移到。 NET 3.5。 COM构成部分最初设在多边贸易体系,然后设在构成部分服务。 在NET港口,我们重新处理与ADO公司的交易。 NET交易,因此,方法签名正在发生变化。

我所面临的困境是参数顺序。 每个方法都要求你通过SqlConnection或Sql Transaction(取决于该方法是否更新数据库)。 某些方法自然可以用不同的论据加以援引。 例如:

Keyword.Load(string description, SqlTransaction transaction)

纽约总部

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

目前,提供多载荷的框架中的多数方法如下:

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

值得注意的是,尽管超载,但参数订单还是一致的。 然而,我真的要强调,要求用户通过连接或交易。 通常,这些是最后提出的论点。 但是,在我看来,将其放在首位的最佳位置是:

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)

你可以看到,这要求增加超负荷,使之正确,但我似乎更清楚强调交易或联系。

如果是我,你会选择什么道路? 是否有一条设计准则规定如何处理类似情况? 处理超载荷是否太多?

最佳回答

从业 《框架设计准则》:

在方法参数的排序和命名方面是否一致。

Edit:

如果你不希望遵循该准则,我将把超负荷工作分成不同的方法,如:

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
)
问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签