MattW
MattW

Reputation: 13212

C# - Design Thoughts on Passing by Reference

I have a C# Windows Service that routes incoming emails and twitter comments to available Agents (I don't really - but the app does something very similar). So, I have a list of Agents, and logic to route these emails and tweets to available agents.

How does my code look?

I have an AgentManager class that keeps track of which agents are available, their skill levels, etc. I have an EmailManager class that routes Emails to Agents. I have a TwitterManager class that routes Tweets to Agents.

So - logic pertaining to Agents, such as the list of Agents, who is available, who has capacity for a Tweet, who has capacity for an Email, etc is all in the AgentManager.

Now, when EmailManager detects that there is a new email and needs to assign it to an Agent, I want

to get my list of agents from AgentManager(rather than going back to the database or keeping a separate list of Agents in EmailManager).

My initial thought is to pass AgentManager to EmailManager by reference. The reason I want to do this is so as Agents change state, are added/removed, etc - EmailManager will always be working wit

h the latest AgentManager (and therefore the latest Agent list). Somehow - this feels dirty. I know it is out of fashion to pass by ref, but it seems to be a good way to handle this. Am I doing it wrong?

EDIT: I am passing the AgentManager by reference for storage, not to change it in the EmailManager class.

Upvotes: 1

Views: 155

Answers (1)

Steve
Steve

Reputation: 216302

From your descriptions seems more soud to go the other way.
An AgentManager process EMails and Tweets and knows everything of its Agents -
So it should have a method that receive a list of EMails/Tweets and process them.

Because we are speaking of reference-types the question about passing by ref is a bit unclear.

For example:

AgentManager ag = new AgentManager();
EMailManager eg = new EMailManager();
TweetManaget tg = new TweetManager();

eg.LoadEMail();
List<EMails> mails = eg.GetMailsList();

tg.LoadTweet();
List<Tweets> tws = tg.GetTweetsList();

ag.ProcessData(mails, tws);

EDIT: Looking at the comment from OP I have thought of another strategy
Let the EMailManager and TweetManager declare an Event to which the AgentManager subscribe-

eg.EmailReceived += ag.NotifyEmail;
tg.TweetPolled += ag.NotifyTweet;


public class EventManager
{
    public delegate void OnMailReceived(EMails m);
    public event MailReceived;

    ........

    private void GetMail()
    {
        EMails m;
        .....
        if(MailReceived != null) 
            MailReceived(m);
    }
}

public class AgentManager()
{
     public void NotifyEMail(EMails m)
     {
         .....
     }


}

Upvotes: 2

Related Questions