English 中文(简体)
请求标的是什么样子?
原标题:Request object, what are the pros and cons?

请允许我说,我有以下方法:

public Stream GetMusic(string songTitle, string albumName) { ... }

一位我的同事相信,这种坏的方法签名是正确的。 他想使用请求标语,把签名方法变成:

public Stream GetMusic(SongRequest request) { ... }

我确实看不到这一点。 我看到的唯一好处是,今后增加参数将比较容易。 我们胜过,不得不改变方法签名,但请求标语仍需改变。

我个人认为,这不是一个好的想法。 利用参数,明确了这种方法所需要的操作方式。 此外,这迫使我们制造另一个目标。

使用请求物体的利弊如何? 您的项目是否使用过这一工具?

最佳回答

你们正在使用<代码>GetMusic(......)方法获取数据。 如果是的话,它会作出太多的努力,在没有实际需要的情况下利用另一个实体。

事实上,在只有一个投入参数的情况下,你可以使用一种习俗。 但是,如果该类别是唯一使用的地方,那么,如果按类别名称所示,<代码>SongSignature就必须在这一类别中具体使用,这是使用 参数袋”的一种不良做法,因为它使易懂。

此外,如果有人说: SongSignature 必须是一个结构,在这一结构中,有一些数据要作内部改动,该名点人将永远不会发生真正的变化,因为每次打上,将使用copy

即便是一类,你也必须将这一类的出入口改为<>条码<>公开<>/代码>,而且一般来说,这并不是把论点推向一种功能并从某种功能中取得结果的最佳方法,因为你已经从这一方法中获得了分流。

Let s assume the following situation:

If in a team one programmer replaces the parameters with a class SongRequest, second programmer did not find that it is used as a parameter to a functions (because it lucks info in a name of a class), and changed it to a structure on next iteration, third programmer used this method in such a way, that it have to be a class (for example have used class references inside SongRequest)... As a result no one did really knowns why something is not working because each of them have dome right thing... There is no excuse to use a class for a local usage instead of implicit declaration of parameters.

Generally you have a good chances to get such a situation in a future, because:

  • you are not the one who changes your code (i.e. GetMusic)
  • someone can review the code and find the class SongReqest useful (so situation goes even worse - from a local usage to a global usage of a class)
  • adding the SongReuest class can add an additional dependencies for you method (is someone changes this class, most likely you founction will not compile)
  • using SongRequest as a property bag locks it usage only as as a class, as mentioned before.
  • using this class, you method would probably never share it parameters with other function calls (for what reason?)
  • finally, using SongRequest class only for passing parameters for a specific function, gives additional memory overhead footprint, because if this method is called often, at one hand, it will create a lot of unnecessary objects in memory have to be garbage collected, in the other hand, if such a method is used rarely, it will be simply not practical to create a class to pass several variables to a single call

只有一个真正的理由使用类别,而不是两个令人信服的论点:你像这种呼吁一样,希望使“比以前更加美好”的所有法典更加 mo,尽管这并不非常实际和有用。

我永远不会建议你制定这样的法典,直到你想做得更好。

Generally, I suppose that using a custom class for passing an arguments for a function is a bad practice.

问题回答

通过一项物体具有重大优势:

If you have an object used as the parameter, such as SongRequest, the object can be responsible for its own validation. This allows you to dramatically simplify the validation in each method that uses the object, as you pretty much only need to check for null, instead of checking each and every parameter.

此外,如果你有多种参数,那么生成单一物体往往更简单。 如果你发现需要多载荷来管理不同的参数,情况尤其如此。

That being said, each situation is unique. I would not recommend one approach over the other for every situation.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签