0 votes

I am interested in mail.dll, I tried the template sample and it worked.

But the sample has a fixed data structure (order and items), I need a solution where the data can be read from a database table or query, so the structure of the data is flexible.

Is it possible to build the data object dynamically based on a SQL query?

Hans

by (200 points)

1 Answer

0 votes

Generally templates are designed to work with already defined models:

Contact contact = new Contact { Name = "John" };

string html = Template
     .From("Hi [Name]!")
     .DataFrom(contact)
     .Render();

You can use dictionaries:

var dict = new Dictionary<string, string>();
dict.Add("Name", "John");

string html = Template
     .From("Hi [Name]!")
     .DataFrom(data)
     .Render();

You can use anonymous objects as well:

var contact = new { Name = "John" };

string html = Template
     .From("Hi [Name]!")
     .DataFrom(contact)
     .Render();

I would suggest creating a model that is closely resembles your template, so that it already contains data in the exact form you plan to display them.

Then you can populate such model from your database entities.

This approach is easy to test, it is also immune to database changes.

by (301k points)
Hi

Thank you, this looks like a good solution:

var dict = new Dictionary<string, string>();
dict.Add("Name", "John");

Hans
...