I have a scenario where we are receiving in xml messages. I need to look at the header of the message to determine what message type / version it is (coming from legacy system). I would like to be able to Register the message types Then pass in a param to resolve the specific type. I am new to DI / Unity (If you haven't noticed) so I might be trying to apply a bad practice or approaching this incorrectly. Want to move a way from an existing factory pattern we are utilizing to accomplish this.
This obviously works:
XDocument xml = XDocument.Load(@"D:\Demos\XmlMessages\AllMessages\LogEvent.xml");
var container = new UnityContainer();
//Base Works
container.RegisterType<IMessage, LogEvent>(new InjectionProperty("XmlDoc", xml));
var baseMessage = container.Resolve<BaseMessage>();
baseMessage.ParseDocument();
Not sure how to apply dynamic resolution here?:
XDocument xml = XDocument.Load(@"D:\Demos\XmlMessages\AllMessages\LogEvent.xml");
var container = new UnityContainer();
container.RegisterType<IMessage, LogEvent>("LogEvent", new InjectionProperty("XmlDoc", xml));
var baseMessage = container.Resolve(typeof(IMessage), "LogEvent");
baseMessage.ParseDocument(); //Just returning type so obviously can't run this
Is registering my messages the right approach? If so then how can I accomplish resolving to my BaseMessage so I can call ParseDocument?
Any info / suggestions greatly appreciated.
EDIT: I already have a class that will parse the header of the XML and feed me the version and type (So for instance it will tell me that this is a "LogEvent" message). That is why I was considering utilizing DI is that I could simply pass the parsed value ( in this case "LogEvent" to the Resolver. Just wanted to update based on Sebastian's response. Even if this scenario is not applicable and I should stick with the factory method. I would like to understand how this type of dynamic call would be possible.
Thanks,
S