So in my current code i’m working on some sort of notification manager.
The idea is that my main BL will use this notification manager per method call. Hence there will probably only be one notification manager (singleton in unity i guess).
When you use the notification manager you can send a notification via SMS\Email\Other. what actually happens is that the notification manager resolves a “INotificationProvidor” which also resides in unity container. This resolve is done by name as in “SMS”, “Email”, “Other”.
Here is a little code snippet:
var notificationProvidor =
m_Container.Resolve<INotificationProvidor<TResult>>(
typeOfNotification.ToString());
ResultMessage<TResult> notificationResult = notificationProvidor
.SendNotification(source, destination, message, subject);
As you can see the notification manager holds an instance of the container to resolve each one of the “INotificationProvidor”s.
How can i possible remove this need of holding the container in the notification manager? with the following restrictions:
- Not all types of “INotificationProvidor”‘s (SMS,email,other) might be registered in the container.
- There will be only one notification manager. (since BL using it will be alive during the course of the application and would receive it from DI)
In short…resolving dependency per method call. 🙂
I suggest the only way to do it is to use some sort of factory, to resolve
INotificationProviderinstead of Unity container, in any case you have to hold a reference to something, that will resolve dependencies in runtime.Here’s the factory’s interface:
That’s how you can use it in
NotificationManager:So you hold a reference to
IProviderResolver(the factory) only. This is the common practice. Alternatively you can:NotificationManagerfor everyINotificationProvider<TNotification>NotificationManager