Skip to content

Instantly share code, notes, and snippets.

@karelkral
Last active March 17, 2019 21:17
Show Gist options
  • Save karelkral/9db2e176b164da96177706062a26499a to your computer and use it in GitHub Desktop.
Save karelkral/9db2e176b164da96177706062a26499a to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/55203279/c-sharp-coding-assessment-need-help-understanding-specs
using System;
public interface INotification
{
void Send(string message, object payLoad);
}
public class Email : INotification
{
string _message { get; set; }
object _payLoad { get; set; }
public void Send(string message, object payLoad)
{
_message = message;
_payLoad = payLoad;
Console.WriteLine($"message \"{message}\" sent by {this.GetType()}");
}
}
public class Fax : INotification
{
string _message { get; set; }
object _payLoad { get; set; }
public void Send(string message, object payLoad)
{
_message = message;
_payLoad = payLoad;
Console.WriteLine($"message \"{message}\" sent by {this.GetType()}");
}
}
public class Report : INotification
{
string _message { get; set; }
object _payLoad { get; set; }
public void Send(string message, object payLoad)
{
_message = message;
_payLoad = payLoad;
Console.WriteLine($"message \"{message}\" sent by {this.GetType()}");
}
}
// EDI GENERATOR
public class EdiGenerator
{
public void Generate(string[] configurationData)
{
foreach (string notificatorType in configurationData)
{
// https://stackoverflow.com/questions/11107536/convert-string-to-type-in-c-sharp
Type type = Type.GetType(notificatorType, true);
INotification sender = (INotification)Activator.CreateInstance(type);
sender.Send("this is message to send", new object());
}
}
}
public static class MainClass
{
public static void Main(string[] args)
{
EdiGenerator generator = new EdiGenerator();
//"Report, ConsoleApp3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
// "Fax, ConsoleApp3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
// "EMail, ConsoleApp3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
// notificators should contains Type names of notificators
string[] notificators = new string[]
{
"Report",
"Fax",
"Email",
};
generator.Generate(notificators);
}
}
@karelkral
Copy link
Author

Depends on yours preferences. I was taken into account the parameters string[] configurationData, which does not allow many other solution.

But you can:

  • Inject a factory to a generator for creating classes which are not known in time of compilation.
  • Resolve a class from dependency container in generator. For example register all INotification instances in DI container and resolve all registered instances in generator. Depends of concrete DI container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment