matt-github-acct/dotnetcore-slackbot

Have an optional paramter for calling Connect on Bot initialization

Closed this issue · 3 comments

This would allow me to create a bot before connecting to the service. I would like to do some configuration and dependency injection in my app before actually starting (Connecting) the bot.

Proposed change

public class Bot 
{
     public Bot(string token, string username, bool connect = true) 
     {
          //existing initialization 
          if(connect)
          {
              Connect();
          }
     }
    public void Start()
    {
        Connect():
    }
}

Example call

var shouldConnectNow = false;
var bot = new Bot(botName, botToken, shouldConnectNow); 
//do other things 
bot.Start(); 

@awright18 thank you for the issue!

I'd like to avoid optional parameters if possible. My thoughts on that are expressed here.

What do you think about creating a new Bot on your end and using the Bot class when you are ready to connect? Something like...

    class BotThatDoesNotConnectUntilConnectIsCalled
    {
        public string Token { get; set; }
        public string Username { get; set; }
        public Bot Bot { get; set; }

        public BotThatDoesNotConnectUntilConnectIsCalled(string token, string username)
        {
            Token = token;
            Username = username;
        }

        public void Connect()
        {
            Bot = new Bot(Token, Username);
        }
    }

We could also add a IBot interface to help with that implementation.

What do you think?

Yeah, that is what I ended up doing. Just creating another class that handled the creation of the bot.

Sounds good, closing this issue.