0

In short story, I want to make some universal generators for different formats (CSV, XLS, XLSX and JSON for now). Just to be clear, I will make different classes for all formats.

I have different models, with different fields. For example:

 public class Agents 
 {
        public int ID { get; set; }
        public string Name { get; set; }   
        public string Email {get;set;}   
 }

 public class IdCards 
 {
        public int ID { get; set; }
        public int MaleTotal { get; set; } 
        public int FemaleTotal { get; set; }       
 }

And I want to get tables that look like this for english:

Agent:

|Name |Email           |
| --- | ---            |
| Sta |sta@example.com |
|Danny|dany@example.com|
|Elle |elle@example.com|

IdCards:

|Men|Women|
|---|---  |
|12 |13   |
|11 |14   |
|120|111  | 

and for french:

Agent:

|Nom  |E-mail          |
| --- | ---            |
| Sta |sta@example.com |
|Danny|dany@example.com|
|Elle |elle@example.com|

IdCards:

|Hommes|Femmes|
| --- | ---   |
|12   |13     |
|11   |14     |
|120  |111    |

Question is, how to prepare this models so I can get variable name as a column name?

9
  • 4
    This sounds like a maintenance nightmare. If at all possible, keep field names in your data schema constant and use an I18N framework to localize displayed values where necessary. There is already a near-infinite number of solutions to this problem, do not roll our own. Nov 18 at 10:08
  • @KilianFoth Can you suggest me some of these solutions?
    – Lube
    Nov 18 at 10:09
  • This would not be a task for "generic programming". It should be possible with reflection and attributes, i.e. use reflection to list properties, and use attributes to either list translated names, or a key to a translation dictionary. But you should absolutely not translate names for something that is intended to be machine readable, i.e. json, and possibly csv.
    – JonasH
    Nov 18 at 10:45
  • I am new to this, why I should not translate these property names? I mean, it is same in json if it is called "name" or "nom" @JonasH
    – Lube
    Nov 18 at 10:53
  • 1
    Because it is generally assumed that a file saved on a computer using French can be opened on one using German. And even if you handle this issue, if anyone else parse your json files, they will most likely not take into account that there is one version per language. Json is not meant to be read by users. And computers do not care if it says "name" or "nom", but inconsistency will make everything terribly more complicated.
    – JonasH
    Nov 18 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.