2016-06-18 3 views
0

내 응용 프로그램에서 모든 ReferenceData 함께 개체를 빌드해야합니다.모델 객체를 만드는 데 좋은 패턴은 무엇입니까?

class ReferenceData{ 
{ 
     public Complex1 Prop1{ get; set; } 
     public Complex2 Prop2{ get; set; } 
     public Complex3 Prop3{ get; set; } 
     public Complex4 Prop4{ get; set; } 
} 

위의 ReferenceData에는 약 30 개의 복합 속성이있을 수 있습니다. 깨끗한 ReferenceData 객체를 만드는 가장 좋은 패턴/방법을 알고 싶습니다.

+0

? 여기에서 해결할 명확한 문제가 있는지 확신하지 못합니다. – blins

+0

이 모델은 Complex1,2,3 등을 빌드해야하는 것처럼 데이터를이 모델에로드하는 데 더 깨끗한 방법을 원하기 때문에 ~ Model 클래스 ReferenceData 만 있습니다. ReferenceData 객체를 리턴합니다. – Deepak

+0

[유창한 인터페이스] (https://en.wikipedia.org/wiki/Fluent_interface)를 참조하십시오. –

답변

2

하나의 좋은 옵션은 작성자 패턴을 사용하는 것입니다. 그것은 다음과 같이 보일 수 있습니다 귀하의 경우에는 http://www.dofactory.com/net/builder-design-pattern

:

은 여기에 패턴에 대한 몇 가지 추가 정보 링크입니다

class ReferenceDataBuilder 
{ 
    private Complex1 prop1; 
    private Complex2 prop2; 
    private Complex3 prop3; 

    public ReferenceDataBuilder setProp1 (Complex1 value) 
    { 
     this.prop1 = value; 
     return this; 
    } 
    public ReferenceDataBuilder setProp2 (Complex2 value) 
    { 
     this.prop2 = value; 
     return this; 
    } 
    public ReferenceDataBuilder setProp3 (Complex3 value) 
    { 
     this.prop3 = value; 
     return this; 
    } 

    public ReferenceData make() 
    { 
     return new ReferenceData(prop1, prop2, prop3); 
    } 
} 

class ReferenceData 
{ 
    public Complex1 Prop1{ get; } 
    public Complex2 Prop2{ get; } 
    public Complex3 Prop3{ get; } 

    public ReferenceData(Complex1 prop1, Complex2 prop2, Complex3 prop3) 
    { 
     this.Prop1 = prop1; 
     this.Prop2 = prop2; 
     this.Prop3 = prop3; 
    } 
} 

이 디자인은 이미 사용하고있을 수 있습니다 무엇을 통해 몇 가지 장점이 있습니다. 먼저이 패턴을 사용하면 ReferenceData을 유지할 수 없으며 모든 인스턴스 (ReferenceData)를 빌더 클래스로 옮길 수 있습니다.이 인스턴스는 ReferenceData의 인스턴스를 가져올 수있을만큼 길어야합니다. 또한 이제 ReferenceData 인스턴스를 만들기 전에 속성이 올바르게 설정되었는지 확인하기 위해 빌더에서보다 명확한 유효성 검사 코드를 사용할 수 있습니다. Complex1, Complex2Complex3 데이터 유형에 대한 빌더를 작성하여 ReferenceData 빌더와 함께 사용하여 인스턴스 작성을 더 쉽게 할 수 있습니다.

당신은이 같은 빌더를 사용하십시오 : 당신이 무엇을 잘못 무엇

ReferenceData refdata = (new ReferenceDataBuilder()) 
    .setProp1((new Complex1Builder()).SomeValue("a").build()) 
    .setProp2((new Complex2Builder()).SomeOtherValue("b")) 
    .setProp3("Some third value") 
    .build(); 
관련 문제