Hi,
a possibly simple problem, but weird why I have no idea how to do it:
Unity (PRISM) and static methods. In this special case, an extension method. But in general, how do I access a "unity provided instance" in a static method. Think e.g. of a logging service I want to access to log some stuff I am doing inside a static method. Do I really have to pass the ref to the logging service when using it?
Example (close to actual problem)
public static void HookupPrismEvent(ref UIElement, ILogger log) {...}
Seems strange, I think I am missing somethings, like Container.Resolve (static resolve). Have not found anything, but container, unity or static are not the worlds best search terms. Perhaps I should just try it, but still, it feels kind of "strange"..
So any comments on HOW and IF to use DI in static methods?
Chris
EDIT - ok, current approach after answer: EDIT2, after thinking about it, removed container, providing "what needed"....
public static void AttachPrismEvents(this UIElement element, IEventAggregator eA)
{
var ev = eA.GetEvent<KeyPressedEvent>();
element.KeyDown += ((sender, e) => ev.Publish(e));
}
or, with logging:
public static void AttachPrismEvents(this UIElement element, ILogger log, IEventAggregator eA)
{
log.Debug("Doing stuff");
var ev = eA.GetEvent<KeyPressedEvent>();
element.KeyDown += ((sender, e) => ev.Publish(e));
}