I am writing a small email templating engine using Razor syntax to fill in the tokens. I have a few email types:
public enum EmailType { Welcome, Reminder }
These types have corresponding templates and models, so for example Welcome email has a template:
<p>Welcome, @Model.Name</p>
and a corresponding model:
public class WelcomeModel { public string Name { get; set; } }
Now I wanted to create a method that will force a correct model for given enum, I would imagine something like this:
public ITemplate<T> GenerateTemplate(EmailType emailType)
{
switch (emailType)
{
case EmailType.Welcome:
return new EmailTemplate<WelcomeModel>();
case EmailType.Reminder:
return new EmailTemplate<ReminderModel>();
// ...
}
where EmailTemplate<T> : ITemplate<T>, so then I could chain the method:
engine
.GenerateTemplate(EmailType.Welcome)
.WithModel(new WelcomeModel()) // this knows it wants WelcomeModel
// and should complain with compiler error otherwise
The code that I've shown here does not compile because T is unknown. However this T cannot be infered:
public ITemplate<T> GenerateTemplate<T>(EmailType emailType)
which leaves me with:
engine
.GenerateTemplate<WelcomeModel>(EmailType.Welcome)
.WithModel(new WelcomeModel());
That works, but I feel like I'm passing redundant information - both enum and model, while you can deduce one from another. I am not sure if I'm missing something from C# or maybe my whole concept is not good. I think I'm in a dead end since I think I cannot return two separate, strongly typed classes from one method.
Is it possible to return generic model based on enum input?