3

I'm thinking about an application that builds some kind of usernames depending on the category where the user belongs. The problem starts when I try to generate this username from a rule contained in a table.

Example:

Data DataTable

| ID | Name  | City   | Level  | Rule |
|----|-------|--------|--------|------|
| 1  | John  | London |   A    |  1   |
| 2  | Chris | Paris  |   C    |  1   |
| 3  | Anna  | Madrid |   B    |  3   |
| 4  | Marie | Roma   |   C    |  2   |

Rules DataTable

| Rule | Format                      |
|------|-----------------------------|
|  1   | "Name + City"               |
|  2   | "Name[0] + City[0] + Level" |
|  3   | "Name[0] + \".\" + City"    |

And I would like to get as a final result:

| ID | Username   |
|----|------------|
| 1  | JonhLondon |
| 2  | ChrisParis |
| 3  | A.Madrid   |
| 4  | MRC        |

So I was thinking if there is a nice method that can build the username using the string contained in the rules table as the template to do it like:

string Name = dtData[i].Name;
string City = dtData[i].City;
string Level = dtData[i].Level;
string u = SomeGreatMethodToEvaluate(dtRules[dtData[i].Rule].Format);
dtFinal[i].Username = u;

Sorry if I didn't explain it good enough, but it's a tricky thing for me.

Jose M Martin
  • 373
  • 1
  • 3
  • 19
  • 1
    I think you've explained your question very well, the problem is I see no way to answer this without an essay - making this entirely too broad. – Jamiec Jun 08 '16 at 15:15
  • 1
    If you need templating, Razor is very effective https://www.nuget.org/packages/RazorEngine/ , If you want to write your own templating syntax that is beyond the scope of an answer here, good luck. If the task is trivial just use a format string. – Jodrell Jun 08 '16 at 15:37

3 Answers3

2

I would try to leverage String.Format to do this. You would store the format string in the database and pass in the parameters in a consistent way, e.g. Name, City, Level, Rule. A pattern like "Name + City" becomes pretty easy, it just requires the format string "{0}{1}".

String.Format doesn't support taking parts of Strings out of the box, but you can extend its functionality with an IFormatProvider and a custom class to wrap your strings. There are good pointers in the related question: Can maximum number of characters be defined in C# format strings like in C printf?.

Community
  • 1
  • 1
bmm6o
  • 6,187
  • 3
  • 28
  • 55
  • I think I will try this in order to avoid install some external packages in the server, but I should take a look at how the patterns can be used. Anyway I will have to make my own class to wrap strings and analyze the rules before. Thank you! – Jose M Martin Jun 09 '16 at 06:49
0

DotLiquid, which is a templating engine, could fit nicely here. With DotLiquid, your rules essentially become templates, which you then feed data into, producing desired result:

1. {{ name }}{{ city }}
2. {{ name | first | upcase }}{{ city | first | upcase }}{{ level }}
3. {{ name | first | upcase }}.{{ city }}
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

When you are using your own set of rules, you have to parse them yourself. But when you use a standard set of rules, like EBNF for example, then you may find something here or here.

Community
  • 1
  • 1
Dieter Meemken
  • 1,937
  • 2
  • 17
  • 22