Question

I'm building an email-monitoring framework that I'll be using for a handful of users, so I'm building a class library to wrap everything in. I'm instantiating the configuration (sender, subject, last-received, ...) in a static class. Therefore, I have something like this.

public static class MyConfig 
{
     public static int Sender { get; set; }
     // and so on and so forth

     public static void BuildMyConfig(string theSender, string theRecipient, ...) 
     {
         Sender = theSender;
         // yada yada yada...
     }
}

public class Monitoring 
{
    public delegate void DoSomethingWithEmail(EmailContents theContents);

    public void StartMonitoring() {

       //When I get an email, I call the method
       DoSomethingWithEmail(theEmailWeJustGot);
    }
}

Obviously, what we do with the email will be something completely different in each case. What I'm trying to is instantiate that delegate. Where would I do that? The MyConfig class and then invoke it from there as a static method? The instance of the Monitoring class?

An application would look like...

public class SpecificMonitor 
{
    Monitoring.BuildMyConfig("foo@bar.com", "bar@foo.com", ...);

    Monitoring m = new Monitoring();
    m.StartMonitoring();

    //But where do I build the delegate method???

}

I've gotten compiling errors with every option I've tried so far. I've also tried overriding a method instead of using a delegate, using interfaces... but I think delegation is where it's at.

Thanks in advance!

Was it helpful?

Solution

Consistent with the rest of your design (although I do not necessarily agree that the design is great) you could allow for the callback to be set in the configuration class

public static class MyConfig
{
    public static string Sender { get; set; }
    public static DoSomethingWithEmail EmailReceivedCallback { get; set; }

    public static void BuildMyConfig(string theSender, string theRecipient,
            DoSomethingWithEmail callback)
    {
        Sender = theSender;
        EmailReceivedCallback = callback;
    }
}

//  Make sure you bring the delegate outside of the Monitoring class!
public delegate void DoSomethingWithEmail(string theContents);

When an incoming email is acknowledged by your application you can now pass the email to the callback assigned to the configuration class

public class Monitoring
{
    public void StartMonitoring()
    {
        const string receivedEmail = "New Answer on your SO Question!";

        //Invoke the callback assigned to the config class
        MyConfig.EmailReceivedCallback(receivedEmail);
    }
}

Here is an example of usage

static void Main()
{
    MyConfig.BuildMyConfig("...", "...", HandleEmail);

    var monitoring = new Monitoring();
    monitoring.StartMonitoring();
}

static void HandleEmail(string thecontents)
{
    // Sample implementation
    Console.WriteLine("Received Email: {0}",thecontents);
}

OTHER TIPS

Define the constructor so that when people instantiate a Monitoring object, they must define the delegate:

public class Monitoring 
{
    public delegate void DoSomethingWithEmail(EmailContents theContents);

    public Monitoring(Delegate DoSomethingWithEmail)
    {
        this.DoSomethingWithEmail = DoSomethingWithEmail;
    }

    public void StartMonitoring() {

       //When I get an email, I call the method
       DoSomethingWithEmail(theEmailWeJustGot);
    }
}

Then pass in the delegate you want when you instantiate each Monitoring:

Monitoring m = new Monitoring(delegate(EmailContents theContents) 
{ 
    /* Do stuff with theContents here */
});
m.StartMonitoring();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top