1

I have the following interface

    public interface ISender
    {
          void SendMessage(string msg);
    }

along with the following implementation

 public class EmailSender : ISender
 {
     private EmailSettings _emailSettings;

     EmailSender(EmailSettings _emailSettings)

     public SendMessage(string msg);
 }

public class EventLogSender : ISender
{
    void SendMessage(string msg);
}

public class QuerySetting
{
   string statementToExecute;
   int MStoWaitBeforeExecute;

   // if set to true then use email settings to perform notification through email
   bool UseEmail;
   EmailSettings EmailSettings;
}

public class EmailSettings
{
   string serverip;
   int port;
   string username;
   string password;
  string MailToAddress;
}

At runtime, a list of queries are loaded into the application. Each query contains information about how often to execute the query, information about when to fire an alarm for the query, and email information on who to email when an alarm occurs( or if not set, the alarm should just write to the event log ). How do i use dependency injection, at runtime, to know what instanation of ISender to create ( EmailSender or EventLogSender ) and also to new up the appropriate email settings for the query if they opted to use email notification instead of eventlog notification? **NOTE: Querys can have different notification methods, 1 can be evenlog notifications, while the others could be all email only.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
user2309367
  • 317
  • 2
  • 8

2 Answers2

0

I'm not sure dependency injection is what you need, take a look at

Activator.CreateInstance

http://msdn.microsoft.com/en-us/library/d133hta4.aspx

And wrap some logic in the request to determine the type. You could also use reflection.

EDIT

All that said can you not just create two object instances and pass the message based on an if/else statement?

SECOND EDIT

Upon further reflection (pun intended) I think what you need is the Factory method pattern:

http://en.wikipedia.org/wiki/Factory_method_pattern

Example:

public class ObjectFactory
{
    public static IObjectController CreateObjectController(ObjectSettings settings)
    {
        IObjectController result = (settings.PINK ? new PinkObject() : (IObjectController) new BlueObject());
        result.Initialise(settings);
        return result;
    }
}
Arjun Sol
  • 731
  • 5
  • 18
0

How do i use dependency injection, at runtime, to know what instanation of ISender to create ( EmailSender or EventLogSender ) and also to new up the appropriate email settings for the query if they opted to use email notification instead of eventlog notification?

You can inject SenderConstructor / SenderFactory that creates Sender based on runtime data.

class Controller

    private readonly SenderConstructor as func(of QuerySetting, ISender)

    public sub new(SenderConstructor as func(of QuerySetting, ISender))
        me.SenderConstructor = SenderConstructor
    end sub

    public function Send(Message as string, Settings as QuerySetting) as Unit
        dim Sender = SenderConstructor(Settings)
        Sender.SendMessage(Message)
    end function
end class

SenderConstructor is defined on the highest level of the program and is registered in IoC container (registration in container is not shown, as it is specific to the container):

dim SenderConstructor =
    function(Settings as QuerySetting) as ISender
        return if(Settings.UseEmail, new EmailSender(Settings.EmailSettings), new EventLogSender)
    end function

When Controller is created, IoC container injects SenderConstructor into it.

Lightman
  • 1,078
  • 11
  • 22