Is it good practice to encapsulate many parameters similar to structure

Basically, I have something like the following:

public string SomeDBMethod(string server, string dbName, string userName, string password,...) 

Is it good to reorganize it into the following:

 public string SomeDbMethod(DBParams parameters, ...) 

Where DBParams is defined as follows:

 public struct DBParams { string Server {get;set;} string DbName {get;set;} string UserName {get;set;} string Password {get;set;} } 

My point is to really be able to skip fewer parameters as I find functions with long parameter lists really ugly.

I also found that there are some limitations to this approach: if SomeDbMethod should be presented as a web service method, I cannot use the DBParams structure as a parameter (as far as I know about web services ... which is not very far).

So, is this too big a problem for little good or am I something here?

+4
source share
5 answers

I always group parameters that are combined into one object - this code is cleaner in such a way, it is obvious that they are related to each other and are always used together.

However, in most cases I use a class, not a structure. The benefits of structures have never been clear to me, and in many situations they hurt in the back (I think the web services scenario is just one example).

You can make a class immutable if you do not want people to change it (if that was the reason for using the structure)

+3
source

If you really need to pass this set of parameters very often (with the same data), I don't see the need. Long lists of parameters are sometimes a sign of the need to reorganize your code, but sometimes again inevitable. (In your situation, itโ€™s more like the latter.) So, just use the simple method of passing parameters directly, if you do not need to store parameter sets (at least temporarily) - you really will not save any code otherwise, and, Of course, without increasing readability.

The structure-based method does not impose any restrictions on web services. You just need to make the type serializable ( DataContract in WPF, I believe), and there should be no problem.

+4
source

There is a template called Encapsulate Context , which talks about the problems associated with using this technique. It might be worth taking a look at this for a detailed analysis.

+2
source

I would advise reading this question about structures, styles of values โ€‹โ€‹of objects

Almost certainly creating a mutable structure is a really bad idea in this case.

Given that named parameters are included in C # 4.0, I would suggest that something like this just annoys later, and should be avoided if there is no serious need to pass this โ€œpackageโ€ of information in many different steps, as well as operations, which only make sense to them.

Honestly, if a class / structure does not support / does not apply any invariant, then it makes little sense.

+1
source

You can use Struct if you want to encapsulate related variables. It should not be an object. Since Structs are value types, unlike classes, they do not require a heap and they are also passed by value not by reference, like objects.

IMHO in this case creates a class to store this information, which does not add value, but if you plan something more complex with DBParams information (exposing it as a parameter for Webservice), then think about using an object. If you just want to pass these parameters in a compressed form, the structure will be fine.

You can get more information here:

http://discuss.joelonsoftware.com/default.asp?dotnet.12.489354.15

0
source

Source: https://habr.com/ru/post/1285732/


All Articles