Method parameters versus parameter object

I am trying to create a framework that allows people to expand our core functions by implementing an interface. The following is an example of using this interface.

public interface MyInterface { IData GetData(string p1, char p2, double p3); } 

Recently, we decided to change this interface (I know that I should not violate the dependent code, but, truth, we do not have third parties implementing this interface, so we have a chance to "make" -over "to implement this the interface is correct).

We need to add 2 more parameters for this interface for our software to work correctly. The two ways we think about simply add them to the signature as follows:

 public interface MyInterface { IData GetData(string p1, char p2, double p3, bool p4, DateTime p5); } 

or by creating a parameter object like this

 public class MyParameters { public bool p4 { get; set; } public DateTime p5 { get; set; } } 

and adding them to the end of the method as follows:

 public interface MyInterface { IData GetData(string p1, char p2, double p3, MyParameters p4); } 

I am looking for some guidance on how this is the โ€œmostโ€ right way. I see the pros and cons in both categories, but I do not want my preconceptions to lead me to the wrong path.

Some of my main concerns:

  • Software extensibility - I want the user to be able to do something that I have not thought about yet, implementing the interface
  • Maintaining health. Ideally, I would never have to touch the code that is responsible for calling GetData () again
  • Pure API - I donโ€™t want the solution I came to make a third-party cringe developer

I donโ€™t even know what question to ask online to get guidance on this issue. I have a feeling that the answer "depends on" (regarding how the related p1-p5 relates to the purpose of the GetData () function), but can someone point me to a list of questions that I should ask to help me evaluate whether Is one solution better than another?

Related: Message 1 Message 2

+8
c # parameter-object
source share
2 answers

Let me try to answer that. Rate your options:

1) If the interface below

 public interface MyInterface { IData GetData(string p1, char p2, double p3); } 

:

a) and should not be used at all, then you should force developers to rewrite it. Therefore, change the definition of the method.

 public interface MyInterface { IData GetData(something else); } 

b), but you only need to encourage users to use the new definition, then you can create a new overload by indicating that the first one is out of date.

 public interface MyInterface { //xml to tell them this is deprecated. IData GetData(string p1, char p2, double p3); IData GetData(string p1, char p2, double p3, and whatever); } 

There is no decision about your new definition that no one can give without knowing about the model, but you ask yourself,

I) "Can the parameters themselves represent a single entity, if they are combined? Does it mean something in the real world to have one unit of your parameters?" If so, do so. If not, do not do this. I think you should rely on thoughts in such abstract levels when you are in such a dilemma. I'm not sure what the point is in bool p4 and DateTime p5 . You should be in the club of those parameters that themselves represent something in real life. If no combination of them makes sense, leave it as such. You will have more success in leaving such a club in such a logical way of thinking. Keep in mind that you leave your definition to other programmers, and to make their work easier, your method signature should be very logical.

II) Can a class do more things than just contain a data set? The class should be here.

III) Do I need to control only the definition of the class, and not its implementation? In this case, just define the open interface of your inputs and leave the implementation to the client.

IV) Should future modifications of my interface not violate the existing implementation? Overload is your way.

Suppose you have two options: leave the signature aligned or in the club.

1) Without clubs:

a) Less code, reduction of another layer.

b) No need to publish another class.

2) With a club:

a) Provides a better understanding of the model for entering the function - your intention is clear.

b) Scales easily in the future if you need to add additional data to build a newly created class. Let's say you have a class

 class Input { string p1; char p2; double p3; public Input(string p1, char p2, double p3){ } } 

and interface

 public interface MyInterface { IData GetData(Input p1); } 

Now you can add additional data such as bool and DateTime. Since they are optional, you do not need to force it into the constructor. You can still have the same constructor and give the user the right to change, if necessary, outside the constructor.

 class Input { string p1; char p2; double p3; bool p4; DateTime p5; public Input(string p1, char p2, double p3){ } } 

The interface may be the same. As I said, the incentive to derive something from this kind of sadly depends on your model. In short, if a grouping can mean something, make it a class or an interface (depending on who will control the logic).

+2
source share

Why not go all the way:

 public Interface IParameters { string p1 {get; set;} char p2 {get; set;} double p3 {get; set;} bool p4 { get; set; } DateTime p5 { get; set; } } public interface MyInterface { IData GetData(IParameters p); } 
+2
source share

All Articles