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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top