Tutorial :Create a regular class or an Inner class for a Parameter Object?



Question:

Instead of passing many arguments to a method, I was encapsulating it into an argument object.

note: simplied for demo
alt text

For such a case, what would be a better practice?

• Create a class and name it as InventorySaveArgs?
-- or --
• Create a nested class and name it as SaveArgs?

And would you also explain why one would choose one or the other?

[EDIT]: That argument type will be used in another assemblies, as well.

side question: Just curious, if there is a pattern name for encapsulating multiple parameters to a single object by chance.

[UPDATE]: Found Nested Type Usage Guidelines on MSDN
InventorySaveArgs should be available from another assemblies, so I am going with a regular class.


Solution:1

IIRC .NET Design Guidelines are pretty clear on this - there should be no public nested types.


Solution:2

I would name it InventorySaveArgs just in case you ever want to make the type available to other types to use. If you name it InventorySaveArgs from the beginning then you will have a name that is meaningful in all contexts if you ever need to refactor.


Solution:3

I would choose the first, create an outer class and name it InventorySaveArgs. If the particular class is used in a public method, the only argument for not including it outside the class is namespace pollution.

Having the inner class is quite frankly annoying in C# because you must constantly prefix the type name with the name of the class. As stated, once it's public the only motivation for donig this is reduction of namespace pollution. But if your namespace is so big that the InventorySaveArgs class is making it too big you probably need to break up your namespace anyways.


Solution:4

The only pattern for encapsulating multiple parameters to a single object I've ever heard of is a refactoring pattern detailed by Martin Fowler


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »