2012-02-16 35 views
5

目標:通用導出爲CSV與表

我們的應用程序是使用多種類型的建(例如人,PersonSite(ICollection的),站點 - 我選擇了這些類,因爲他們有關係)。我們想要做的是能夠從Excel表格中的第一個類型(Person)中導出一些屬性,然後從同一個Excel文件中的第二個類型(PersonSite)中導出一些其他屬性,但在同一個新表格中導出其他屬性片。

結果應該是這樣的:

 
_________________ ________________________ ________________ 
|     | |      | |    | 
|PERSON PROPERTIES| | PERSONSITE PROPERTIES | |SITE PROPERTIES | 
|_________________| |________________________| |________________| 
| Name Person 1 | |Relation type for item 1| | Name for item 1| 
|_________________| |________________________| |________________| 
        |Relation type for item 2| | Name for item 2| 
        |________________________| |________________| 
        |Relation type for item 3| | Name for item 3| 
        |________________________| |________________| 
_________________ ________________________ ________________ 
| Name Person 2 | |Relation type for item 1| | Name for item 1| 
|_________________| |________________________| |________________| 
        |Relation type for item 2| | Name for item 1| 
        |________________________| |________________| 

所以對於包含在列表中的每個PersonSite,我們想創建一個將只是人的表格後插入一個表格。

因此,這是怎麼看待Person類(類的子集):

public class Person : IObject 
{ 
    public ICollection<PersonSite> PersonSites {get;set;} 
} 

現在PersonSite類(子):

public class PersonSite : IObject 
{ 
    public Person Person {get;set;} 
    public Site Site {get;set;} 
    public RelationType RelationType {get;set;} 
} 

網站類(子):

public class Site : IObject 
{ 
    public ICollection<PersonSite> PersonSites {get;set;} 
} 

因此,我們決定編寫一個使用表達式來檢索必須屬性的CSVExporter類出口。

這是我們必須實現這個計劃:

      
          ____ 
          | |0..* 
______________   __|____|______  1..* _______________ 
| CSV EXPORTER |________| CSV TABLE (T)|__________| CSV COLUMN (T)| 
|______________|  |______________|   |_______________| 
           | 
           |1..* 
         ______|________ 
         | CSV ROWS (T) | 
         |_______________| 

所以CSVTable使用一個通用的類型,它是IObject提取(如在不同的類中使用)。

表可以有多個表(例如Person表具有PersonSite表,PersonSite表具有Site表)。但是使用的類型是不同的,因爲我們瀏覽不同的類(這些類必須有關係)。

當添加一個子表,以一個表中,我們應當提供連接表達式將抓住正確類型的從主項中的項(人=> Person.PersonSite)

所以我們寫了下面的代碼段用於表:

public class CSVExportTable<T> 
    where T : IObject 
{ 

    private Matrix<string> Matrix { get; set; } 
    private ICollection<CSVExportTableColumn<T>> Columns { get; set; } 
    private ICollection<CSVExportTableRow<T>> Rows { get; set; } 
    private ICollection<CSVExportTable<IObject>> SubTables { get; set; } 
    private Expression<Func<T, object>> Link { get; set; } 


    public CSVExportTable() 
    { 
     this.Matrix = new Matrix<string>(); 
     this.Columns = new List<CSVExportTableColumn<T>>(); 
     this.SubTables = new List<CSVExportTable<IObject>>(); 
     this.Rows = new List<CSVExportTableRow<T>>(); 
    } 

    public CSVExportTable<R> AddSubTable<R>(Expression<Func<T, object>> link) where R : IObject 
    { 
     /* This is where we create the link between the main table items and the subtable items (= where we retreive Person => Person.PersonSites as an ICollection<R> since the subtable has a different type (T != R but they have the same interface(IObject))*/ 
    } 

    public void AddColumn(Expression<Func<T, object>> exportProperty) 
    { 
     this.Columns.Add(new CSVExportTableColumn<T>(exportProperty)); 
    } 

    public Matrix<string> GenerateMatrix() 
    { 
     int rowIndex= 0; 
     foreach (CSVExportTableRow<T> row in this.Rows) 
     { 
      int columnIndex = 0; 
      foreach (CSVExportTableColumn<T> column in this.Columns) 
      { 
       this.Matrix = this.Matrix.AddValue(rowIndex, columnIndex, ((string)column.ExportProperty.Compile().DynamicInvoke(row.Item))); 
       columnIndex++; 
      } 
      rowIndex++; 
     } 
     return this.Matrix; 
    } 

    public Matrix<string> ApplyTemplate(ICollection<T> items) 
    { 
     // Generate rows 
     foreach (T item in items) 
     { 
      this.Rows.Add(new CSVExportTableRow<T>(item)); 
     } 
     // Instantiate the matrix 
     Matrix<string> matrix = new Matrix<string>(); 

     // Generate matrix for every row 
     foreach (var row in this.Rows) 
     { 
      matrix = GenerateMatrix(); 
      // Generate matrix for every sub table 
      foreach (var subTable in this.SubTables) 
      { 
       // This it where we should call ApplyTemplate for the current subTable with the elements that the link expression gave us(ICollection). 
      } 
     } 
     return matrix; 
    } 
} 

最後這裏是CSVExportTableColumn類:

public class CSVExportTableColumn<T> where T : IObject 
{ 
    public Expression<Func<T, object>> ExportProperty { get; set; } 

    public CSVExportTableColumn(Expression<Func<T, object>> exportProperty) 
    { 
     this.ExportProperty = exportProperty; 
    } 
} 

有沒有人做過類似的東西?還是我們走錯了路? 如果這似乎是一條好路徑,我們如何創建鏈接(關係)表達式並使用它來檢索最後一次調用中使用的正確項目?

+1

偉大你有它的工作,你可以關閉它嗎? – 2012-02-19 21:22:56

+4

我同意@MicahArmantrout;對於搜索類似問題的其他人,您是否可以將更新發布爲答案並接受它?它也將防止將回答者試圖解決您的問題..謝謝:-) – Jonno 2012-03-13 14:07:10

+2

@Whoami請張貼您的解決方案作爲答案。你可以接受這個答案(有時你需要等待一兩天才能完成)。如果你這樣做,你會問問題和答案,因爲你已經做了一些出色的工作。現在完成步驟(: – 2012-03-22 20:24:20

回答

0

抱歉耽擱,

所以最後,我們得到了它的工作是這樣的:

public Matrix<string> ApplyTemplate(object items) 
    { 
     ICollection<T> castedItems = new List<T>(); 
     // Cast items as a List<T> 
     if (items is List<T>) 
     { 
      castedItems = (ICollection<T>)items; 
     } 
     else if (items is HashSet<T>) 
     { 
      castedItems = ((HashSet<T>)items).ToList(); 
     } 
     // Generate rows 
     foreach (T item in castedItems) 
     { 
      this.Rows.Add(new CSVExportTableRow<T>(item)); 
     } 
     // Instantiate the matrix 
     Matrix<string> matrix = new Matrix<string>(); 

     // Generate matrix for every row 
     foreach (var row in this.Rows) 
     { 
      matrix = GenerateMatrix(); 
      // Generate matrix for every sub table 
      foreach (var subTable in this.SubTables) 
      { 
       matrix = matrix.AddMatrix(subTable.ApplyTemplate(this.Link.Compile().DynamicInvoke(row.Item))); 
      } 
     } 
     return matrix; 
    } 

這將應用模板,爲任何子表。

希望這可以對其他人有用。