2011-03-03 53 views
7

大家好我在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) 

我覺得這是需要申請KELY和Chevex的形式給出,例如我可以提取一些類簽名

一個將來自參數:

long area, int subArea 

其他

bool brandingPrefix, long brand, 

,並提取子類,我可以使用引入參數對象我正確理解後?

+4

+1,但什麼時候ASP.Net MVC成爲傳統:) – 2011-03-03 21:58:25

+1

也許他指的遺產以同樣的方式邁克爾羽毛確實在他的書[修改代碼的工作(見現代解釋部分)](http://en.wikipedia.org/wiki/Legacy_code)。 – Matt 2011-03-03 22:11:03

+1

我說遺留代碼我的意思是代碼是很久以前由其他開發人員編寫的代碼在ASP.NET MVC應用程序 – Serghei 2011-03-03 22:27:15

回答

14

創建一個對象來保存這20個參數並將該對象傳遞給該方法。

例如:

public void MyMethod(MyArguments args) 
{ 
    // do stuff 
} 

編輯

雖然這種模式可能是一次重構有用的,如果你發現自己使用相同的參數在多個方法,考慮Chevex's答案。這是更好的方法。

+4

不,這被稱爲[引入參數對象](http://www.refactoring.com /catalog/introduceParameterObject.html)重構。 – Oded 2011-03-03 22:01:35

+0

我指的是將不同的參數捆綁到一個單獨的對象中,但我想不能保證它們都是從OO的角度來看屬於同一個對象。 – 2011-03-03 22:04:45

+5

這很愚蠢,因爲你必須在一個單獨的對象上設置20個參數。這是同樣的事情,只是抽象了一下,以便工作與原始對象不同。不要將它們全部捆綁在一起,考慮我確定相關價值並將其分解的答案。國際海事組織這比將所有參數填入參數對象要好。選出可能會一起使用的數據,並將這些數據分別歸入自己的對象中。 – Chev 2011-03-03 22:08:34

13

您可能會嘗試識別參數中的相關數據,並將它們歸入自己的自定義對象中。例如,假設你有這樣的對象:

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; } 
} 

嘗試重構這是兩個類,提取相關的地址信息到自己的類,然後將對象作爲原始對象的屬性:

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; } 
} 

沒有更多的信息我會說這是你最好的方法。

+0

Chevex-謝謝你提取子類是一個好主意,但問題是我不知道在不同類中究竟有多精確的組參數。 – Serghei 2011-03-03 22:25:24

+0

不客氣。我想這將是一個問題,你必須通過與項目領域專家的經驗和溝通來回答。 – Chev 2011-03-03 22:46:19

1

使用Builder模式

QualificationBuilder builder = new QualificationBuilder(); 
builder.setAchievableCode(achievableCode) 
     .setAchievableTitle(achievableTitle)... 
Qualification = builder.build(); 
+0

這是一個很好的方法,但從20個參數8是創建資格對象的必填字段。這是如何與構建器一起實現的? – Serghei 2011-03-03 22:47:15

相關問題