如何用很多参数重构类的方法?

Ser*_*hei 8 c# refactoring

每个人我都对ASP.NET MVC应用程序中的遗留代码有疑问,在此代码中,业务逻辑层中有一个类Service.这个类有20个参数的方法,这个方法使用这20个参数创建一个对象实例.如何重构此代码,因为当更改创建的对象并且需要更改方法中的参数时,这是一个问题.此服务类用于控制器类和单元测试.帮我修改这段代码在此先感谢您.

编辑附加信息:

我可以显示该方法的签名

public Qualification CreateQualification(string achievableCode, string achievableTitle,
        string accreditationRef, bool brandingPrefix, long brand, float guidedLearningHours, 
        int creditValue, long level, long type, long gradingType, long area, int subArea,
        DateTime accreditationStartDate, DateTime accreditationEndDate,
        DateTime lastCertDate, string nameOnCert, 
        long organisationId)
Run Code Online (Sandbox Code Playgroud)

我认为需要应用Kely和Chevex aproach,例如我可以提取一些类

一个将来自参数:

 long area, int subArea
Run Code Online (Sandbox Code Playgroud)

其他

bool brandingPrefix, long brand,
Run Code Online (Sandbox Code Playgroud)

在提取子类之后我可以使用Introduce Parameter Object我正确理解了吗?

Kyl*_*man 14

创建一个对象来保存这20个参数并将该对象传递给该方法.

例如:

public void MyMethod(MyArguments args)
{
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

编辑

虽然这种模式对于一次性重构可能很有用,但如果您发现自己在多种方法中使用相同的参数,请考虑Chevex的答案.这是更好的方法.

  • 这很愚蠢,因为你必须在一个单独的对象上设置20个参数.这是同样的事情,只是抽象了一点,以便作品与原作不同.而不是将它们全部捆绑成一个块,考虑我的识别相关值并将它们分解出来的答案.IMO比把所有参数塞进一个参数对象更好.选择可能合在一起的数据,并将这些数据分别考虑到自己的对象中. (6认同)
  • 不,这被称为[Introduce Parameter Object](http://www.refactoring.com/catalog/introduceParameterObject.html)重构. (4认同)
  • 是Serghei,当您获得正在进行的项目的经验时,尝试确定哪些值组合在一起,并以单独的对象形式给予他们自己的家. (2认同)

Che*_*hev 14

您可能会尝试在参数中标识相关数据,并将它们计入自己的自定义对象中.例如,假装你有这个对象:

public class Person
{
    public Person(string firstName, string lastName, int age,
        string streetAddress, string city, string state, int zipCode)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
        this.StreetAddress = streetAddress;
        this.City = city;
        this.State = state;
        this.ZipCode = zipCode;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int ZipCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

尝试将其重构为两个类,将相关的地址信息提取到自己的类中,然后将该对象添加为原始对象的属性:

public class Person
{
    public Person(string firstName, string lastName, int age, Address address)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
        this.Address = address;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public Address(string streetAddress, string city, string state, int zipCode)
    {
         this.StreetAddress = streetAddress;
         this.City = city;
         this.State = state;
         this.ZipCode = zipCode;
    }

    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int ZipCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

没有更多信息我会说这是你最好的方法.