Reputation: 36895
Instead of passing many arguments to a method, I was encapsulating it into an argument object.
note: simplied for demo
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 asSaveArgs
?
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.
Upvotes: 2
Views: 771
Reputation: 11371
The only pattern for encapsulating multiple parameters to a single object I've ever heard of is a refactoring pattern detailed by Martin Fowler
Upvotes: 1
Reputation: 754575
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.
Upvotes: 1
Reputation:
IIRC .NET Design Guidelines are pretty clear on this - there should be no public nested types.
Upvotes: 1
Reputation: 351476
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.
Upvotes: 1