When designing a class is it best to force the implementer to pass in all required parameters by making it part of the method signature or to create an overload and allow the user to set properties, then check that they are set properly?
For example, let's say we have a class Emailer with a method Send. Send requires
public bool Send(string ToAddress, string FromAddress, string subject, string body, string Attachment = null)
I also have public properties
public string To { get; set; }
public string From { get; set; }
/// <summary>
/// Full filepath to attachment
/// </summary>
public string Attachment { get; set; }
public string Subject {get;set;}
public string Body {get;set;}
Now, should I create an overload for Send?
public void Send()
And then write checks for unpopulated properties. Or, leave out the overload and possibly remove the properties?
I feel that your Emailer has too much resposibility by holding those properties. Perhaps you should look into separating that into a Message class, then you can pass that.
Have a look at the way the .NET mail classes do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With