Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice - when to overload?

Tags:

c#

.net-4.0

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?

like image 539
P.Brian.Mackey Avatar asked Feb 23 '26 06:02

P.Brian.Mackey


1 Answers

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.

like image 110
Daniel A. White Avatar answered Feb 25 '26 20:02

Daniel A. White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!