esportazione generico in formato CSV con le tabelle
-
25-10-2019 - |
Domanda
Obiettivo:
La nostra applicazione è costruito utilizzando più tipi (ad esempio una persona, PersonSite (ICollection), del sito - ho selezionato quelle classi perché hanno un rapporto). Quello che vogliamo fare è quello di essere in grado di esportare alcuni oggetti di primo tipo (Persona) in una tabella di Excel e quindi esportare alcuni altri oggetti di secondo tipo (PersonSite) all'interno dello stesso file excel ma in una nuova tabella sulla stessa foglio.
Il risultato dovrebbe essere simile a questo:
_________________ ________________________ ________________ | | | | | | |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| |________________________| |________________|
Quindi per ogni PersonSite contenute nella lista, vorremmo creare una tabella che verrà inserito subito dopo il tavolo della persona.
Quindi questo è come guardare la classe Person (sottoinsieme della classe):
public class Person : IObject
{
public ICollection<PersonSite> PersonSites {get;set;}
}
Ora la classe PersonSite (sottoinsieme):
public class PersonSite : IObject
{
public Person Person {get;set;}
public Site Site {get;set;}
public RelationType RelationType {get;set;}
}
La classe del sito (sottoinsieme):
public class Site : IObject
{
public ICollection<PersonSite> PersonSites {get;set;}
}
Così abbiamo deciso di scrivere una classe CSVExporter che usano espressioni per recuperare le proprietà che devono essere esportati.
Questo è lo schema che dobbiamo implementare questa:
____ | |0..* ______________ __|____|______ 1..* _______________ | CSV EXPORTER |________| CSV TABLE (T)|__________| CSV COLUMN (T)| |______________| |______________| |_______________| | |1..* ______|________ | CSV ROWS (T) | |_______________|
Quindi, l'uso CSVTable un tipo generico che è IObject (come quello usato nelle varie categorie).
Una tabella può avere più tabelle (tabella per esempio la persona ha un tavolo e un tavolo PersonSite PersonSite ha un tavolo del sito). Ma il tipo utilizzato è diverso da quando si naviga attraverso le diverse classi (quelle classi devono avere una relazione).
Quando si aggiunge un sottotabella a una tabella dovremmo fornire en espressione che catturerà le voci del tipo corretto dalle voci principali (persona => Person.PersonSite)
Così abbiamo scritto il seguente pezzo di codice per la tabella:
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;
}
}
E infine ecco la classe 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;
}
}
Qualcuno ha mai fatto una cosa del genere? O stiamo andando a una strada sbagliata? Se questo sembra essere un percorso buona, come possiamo creare il collegamento (rapporto) espressione e usarlo per recuperare gli elementi giusti da usare l'ultima chiamata?
Soluzione
Ci scusiamo per il ritardo,
Così alla fine, abbiamo ottenuto che funziona in questo modo:
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;
}
Questa applicherà il modello per qualsiasi tabella sub.
Spero che questo può essere utile per gli altri.